- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
80 lines (69 loc) · 2.11 KB
/
Copy pathProgram.cs
File metadata and controls
80 lines (69 loc) · 2.11 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Runtime.ExceptionServices;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespaceLoadBalancer
{
classProgram
{
staticvoidMain(string[]args)
{
varloads=newList<int[]>();
loads.Add(newint[]{1,1,1,1});
loads.Add(newint[]{1,1,1,1,1});
loads.Add(newint[]{1,2,3,4,5,6});
loads.Add(newint[]{1,2,9,3,5,1,1,1});
loads.Add(newint[]{2,4,5,3,3,9,2,2,2});
// Expected output:
// false
// true
// false
// true
// true
foreach(varloadinloads)
{
Console.WriteLine(LoadBalancer(load).ToString());
}
Console.ReadKey();
}
staticboolLoadBalancer(int[]numbers)
{
varl=0;
intr;
while(l<numbers.Length-1)
{
r=l+1;
while(r<numbers.Length)
{
if(IsBalancedForPAndQPositions(numbers,l,r))
{
returntrue;
}
r++;
}
l++;
}
returnfalse;
}
staticboolIsBalancedForPAndQPositions(int[]numbers,intp,intq)
{
if(p==0||q==numbers.Length-1||q-p==1)
returnfalse;
varfirstSum=SumElements(numbers,0,p-1);
varsecondSum=SumElements(numbers,p+1,q-1);
varthirdSum=SumElements(numbers,q+1,numbers.Length-1);
returnfirstSum==secondSum&&firstSum==thirdSum;
}
staticintSumElements(int[]numbers,intstartIndex,intendIndex)
{
varsum=0;
for(vari=startIndex;i<=endIndex;i++)
{
sum+=numbers[i];
}
returnsum;
}
}
}