Skip to content

Commit

Permalink
Create November-07.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunterdii authored Nov 6, 2024
1 parent 7752ffe commit cfb29c9
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions November 2024 GFG SOLUTION/November-07.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCases = Integer.parseInt(br.readLine().trim());

while (testCases-- > 0) {
String[] str = br.readLine().trim().split(" ");
int[] arr = new int[str.length];
for (int i = 0; i < str.length; i++) {
arr[i] = Integer.parseInt(str[i]);
}

Solution ob = new Solution();
List<Integer> result = ob.findSplit(arr);

if (result.get(0) == -1 && result.get(1) == -1) {
System.out.println("false");
} else {
System.out.println("true");
}
System.out.println("~");
}
}
}

// } Driver Code Ends


// User function Template for Java
class Solution {
public List<Integer> findSplit(int[] arr) {
int n = arr.length;
int totalSum = 0;
for (int i = 0; i < n; i++) {
totalSum += arr[i];
}

if (totalSum % 3 != 0)
return Arrays.asList(-1, -1);

int target = totalSum / 3;
int currentSum = 0;
int countFirst = 0;

for (int i = 0; i < n; i++) {
currentSum += arr[i];

if (currentSum == 2 * target && countFirst > 0)
return Arrays.asList(countFirst - 1, i);

if (currentSum == target) {
countFirst++;
}
}

return Arrays.asList(-1, -1);
}
}

0 comments on commit cfb29c9

Please sign in to comment.