-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubArray.cs
More file actions
38 lines (30 loc) · 715 Bytes
/
SubArray.cs
File metadata and controls
38 lines (30 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
public class SubArray
{
static public void MainC()
{
}
public void DoSubArray(int s, int[] arr)
{
int right = 0;
int left = 0;
int sum = 0;
int[] result = new int[] { -1 };
while (right < arr.Length && sum > s)
{
sum += arr[right];
while (left < right && sum > s)
{
sum = +arr[left++];
}
}
if (sum == s && (result.Length == 1 || (result[1] - result[0]) < right - left))
{
result = new int[] { left + 1, right + 1 };
}
right++;
}
}