Find partitions to maximize even and odd count sum in left and right part
Given an array arr[] having N positive integers, the task is to find all the indices i (1 ≤ i ≤ N – 1) such that, the sum of the count of even numbers in the left subarray [0, i – 1] and the count of odd numbers in the right subarray [i, n – 1] is the maximum among all i’s.
Example:
Input: arr[] = {1, 3, 4, 5, 6}
Output: 1 3
Explanation: Count of even integers in the range 0 to 0 is 0 and count of odd integers in the range 1 to 4 is 2.
Total = 0 + 2 = 2 (which is maximum for all i’s).
Count of even integers in the range 0 to 2 is 1 and count of odd integers in the range 3 to 4 is 1.
Total = 1 + 1 = 2 (which is maximum for all i’s).Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 2 4 6
Explanation: Count of even integers in the range 0 to 1 is 1 and count of odd integer in the range 2 to 6 is 3.
Total = 1 + 3 = 4 (which is maximum for all i’s).
Count of even integers in the range 0 to 3 is 2 and Count of odd integers in the range 4 to 6 is 2.
Total = 2 + 2 = 4 (which is maximum for all i’s).
Count of even integers in the range 0 to 5 is 3 and Count of odd integers in the range 6 to 6 is 1.
Total = 3 + 1 = 4 (which is maximum for all i’s).
Naive Approach: For each index check number of even integers to the left and the number of odd integers to the right. Find the maximum value among these and the indices which result in the maximum value. Follow the steps mentioned below:
- For each index i:
- Iterate in the left subarray [0, i – 1] using a nested loop and count the number of even integers in the left subarray.
- Similarly, in another nested loop, iterate in the right subarray [i, N – 1] and count the number of odd integers in this subarray.
- Use an integer variable that will keep the track of the maximum sum of counts.
- Compare the sum with the previous max sum
- If the current sum is greater than the previous one then update the max sum and put i in the result array.
- if the current sum is equal to the previous maximum one then in the previous result array push this index i.
- Return the resultant vector.
Below is the implementation of the above approach
C++
// C++ code to implement the approach#include <bits/stdc++.h>using namespace std; // Funcion to find the answer vectorvector<int> solve(vector<int>& vect){ // To keep the track // of final answer int maximumSum = 0; // Size of nums int n = vect.size(); // It keeps the track // of final answer vector<int> ans; // Iterate over the indices for (int i = 1; i < n; i++) { // Stores the count of even // numbers in the left subarray int countEven = 0; // Iterate in the left subarray for (int j = i - 1; j >= 0; j--) { if (vect[j] % 2 == 0) countEven++; } // Stores the count of even // numbers in the left subarray int countOdd = 0; // Iterate in the right subarray for (int j = i; j < n; j++) { if (vect[j] % 2 == 1) countOdd++; } // Stores the sum for current i int sum = countEven + countOdd; // If current score // is greater than // previous then push // in the ans array. if (sum > maximumSum) { ans = { i }; maximumSum = sum; } // If sum is equal to // maximum sum then // consider the index i // with previous max else if (sum == maximumSum) ans.push_back(i); } return ans;} // Function to print answervoid print(vector<int>& ans){ int n = ans.size(); for (int i = 0; i < n; i++) cout << ans[i] << ' ';} // Driver codeint main(){ // Given vector vector<int> vect = { 1, 2, 3, 4, 5, 6, 7 }; // Function call vector<int> ans = solve(vect); print(ans); return 0;} |
2 4 6
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: We can use the prefix sum technique to trade off the time. The approach is discussed below:
- To keep the track of the count of even elements from 0 to i – 1 for any i, declare an array of countEven. Initialize it with zeroes initially.
- Also, to keep the track of the count of odd elements from i to n – 1 for any i, declare an array of countOdd. Initialize it with zeroes initially.
- Then for each i the sum will be countEven[i-1]+countOdd[i-1]
- Compare the sum with previous max sum
- If current sum is greater than the previous one then update the max sum and put i in the result array .
- if current sum is equal to the previous max one then in previous result array push this index i.
- Return the resultant vector after complete traversal.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach#include <bits/stdc++.h>using namespace std; vector<int> solve(vector<int>& vect){ // The size of the vector int n = vect.size(); // It keeps the track of the maximumSum int maximumSum = 0; // Initialize vectors vector<int> countEven(n, 0); vector<int> countOdd(n, 0); // Traverse and update countEven vector for (int i = 0; i < n; i++) { if (i == 0) { if (vect[i] % 2 == 0) countEven[i] = 1; else countEven[i] = 0; } else { if (vect[i] % 2 == 0) countEven[i] = countEven[i - 1] + 1; else countEven[i] = countEven[i - 1]; } } // Traverse and update countOdd vector for (int i = n - 1; i >= 0; i--) { if (i == n - 1) { if (vect[i] % 2 == 1) countOdd[i] = 1; else countOdd[i] = 0; } else { if (vect[i] % 2 == 1) countOdd[i] = countOdd[i + 1] + 1; else countOdd[i] = countOdd[i + 1]; } } // ans will store the indices vector<int> ans; for (int i = 1; i < n; i++) { // Calculate current sum int sum = countEven[i - 1] + countOdd[i]; maximumSum = max(maximumSum, sum); } // Iterate over the indices for (int i = 1; i < n; i++) { int sum = countEven[i - 1] + countOdd[i]; // If the value of sum is // equal to maximumSum then // consider the index i if (sum == maximumSum) ans.push_back(i); } // Return the ans vector return ans;} // Function to print ans elementsvoid print(vector<int>& ans){ // Number of elements in the answer vector int n = ans.size(); // Print values for (int i = 0; i < n; i++) cout << ans[i] << ' ';} // Driver codeint main(){ // Input vector vector<int> nums = { 1, 2, 3, 4, 5, 6, 7 }; // Calling solve function vector<int> ans = solve(nums); // Print ans elements print(ans); return 0;} |
2 4 6
Time Complexity: O(N)
Auxiliary Space: O(N)


