Write short, human-like code so that it will not get detected by AI plagiarism and using…
- User
Write short, human-like code so that it will not get detected by AI plagiarism and using exactly these variable names (a, b, c, d, r, s, etc). Use a unique, common approach that’s optimized and free of explanations or comments for the following question: Given an array, arr[], determine if arr can be split into three consecutive parts such that the sum of each part is equal. If possible, return any index pair(i, j) in an array such that sum(arr[0..i]) = sum(arr[i+1..j]) = sum(arr[j+1..n-1]), otherwise return an array {-1,-1}.
Note: Driver code will print true if arr can be split into three equal sum subarrays, otherwise, it is false.
Examples :
Input: arr[] = [1, 3, 4, 0, 4] Output: true Explanation: [1, 2] is valid pair as sum of subarray arr[0..1] is equal to sum of subarray arr[2..3] and also to sum of subarray arr[4..4]. The sum is 4. Input: arr[] = [2, 3, 4] Output: false Explanation: No three subarrays exist which have equal sum. Input: arr[] = [0, 1, 1] Output: false Constraints: 3 ≤ arr.size() ≤ 106 0 ≤ arr[i] ≤ 106
// User function Template for C++ // Class Solution to contain the method for solving the problem. class Solution { public: // Function to determine if array arr can be split into three equal sum sets. vector findSplit(vector& arr) { // code here } };
- Assistant
class Solution { public: vector<int> findSplit(vector<int>& arr) { long long total = 0; for(auto a: arr) total += a; if(total % 3 != 0) return {-1,-1}; long long target = total / 3, sum = 0; int i = -1, j = -1; for(int k=0; k<arr.size(); k++) { sum += arr[k]; if(i==-1 && sum == target) { i = k; } else if(i != -1 && sum == 2*target) { j = k; break; } } if(i != -1 && j != -1 && j < arr.size()-1) return {i,j}; return {-1,-1}; } };