-
Notifications
You must be signed in to change notification settings - Fork 21
/
TapeEquilibrium_1.cs
59 lines (45 loc) · 1.18 KB
/
TapeEquilibrium_1.cs
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
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution
{
public int solution(int[] A)
{
// write your code in C# 6.0 with .NET 4.5 (Mono)
int N = A.Length;
int index1 = 0;
int index2 = N-1;
int sum1 = A[index1];
int sum2 = A[index2];
int sum1_next = sum1+A[index1+1];
int sum2_next = sum2+A[index2-1];
int diff = Math.Abs(sum1-sum2);
while (index1 < index2)
{
diff = Math.Abs(sum1-sum2);
sum1_next = sum1+A[index1+1];
sum2_next = sum2+A[index2-1];
if ( Math.Abs(sum1 - sum2_next) > Math.Abs(sum1_next - sum2) )
{
index1++;
sum1 += A[index1];
}
else
{
index2--;
sum2 += A[index2];
}
}
return diff;
}
}
/*
Task Score
75%
Correctness
83%
Performance
66%
*/