-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLittleChefAndSums.java
More file actions
58 lines (50 loc) · 1.41 KB
/
LittleChefAndSums.java
File metadata and controls
58 lines (50 loc) · 1.41 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* Solution to Codechef's Sept Long Challenge problem.
* Problem Link : https://www.codechef.com/SEPT17/problems/CHEFSUM
*
* @author Monic Bhanushali
*
*/
public class LittleChefAndSums {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int nCases = Integer.parseInt(st.nextToken());
int inArray[];
int arraySize;
while(nCases>0){
arraySize = Integer.parseInt(br.readLine());
inArray = new int[arraySize];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < arraySize; i++) {
inArray[i] = Integer.parseInt(st.nextToken());
}
System.out.println(newApproach(inArray));
nCases--;
}
}
private static int newApproach(int[] array){
int result=-1;
long minSum = Long.MAX_VALUE;
long totalSum = 0;
for (int i = 0; i < array.length; i++) {
totalSum =totalSum + array[i];
}
long previousPrefixSum =0;
for (int i = 0; i < array.length; i++) {
long suffixSum = totalSum - previousPrefixSum;
long prefixSum = previousPrefixSum + array[i];
previousPrefixSum = prefixSum;
long sum = prefixSum + suffixSum;
if(sum<minSum){
minSum = sum;
result = i+1;
}
}
return result;
}
}