Count ways to make sum of odd and even indexed elements equal by removing an array element
Last Updated :
27 Jun, 2021
Given an array, arr[] of size N, the task is to find the count of array indices such that removing an element from these indices makes the sum of even-indexed and odd-indexed array elements equal.
Examples:
Input: arr[] = { 2, 1, 6, 4 }
Output: 1
Explanation:
Removing arr[1] from the array modifies arr[] to { 2, 6, 4 } such that, arr[0] + arr[2] = arr[1].
Therefore, the required output is 1.
Input: arr[] = { 1, 1, 1 }
Output: 3
Explanation:
Removing arr[0] from the given array modifies arr[] to { 1, 1 } such that arr[0] = arr[1]
Removing arr[1] from the given array modifies arr[] to { 1, 1 } such that arr[0] = arr[1]
Removing arr[2] from the given array modifies arr[] to { 1, 1 } such that arr[0] = arr[1]
Therefore, the required output is 3.
Naive Approach: The simplest approach to solve this problem is to traverse the array and for each array element, check if removing the element from the array makes the sum of even-indexed and odd-indexed array elements equal or not. If found to be true, then increment the count. Finally, print the count.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the observation that removing any element from the given array makes even indices of succeeding elements as odd and odd indices of the succeeding elements as even. Follow the steps below to solve the problem:
- Initialize two variables, say evenSum and oddSum, to store the sum of odd-indexed and even-indexed elements of the given array respectively.
- Traverse the array using variable i.
- Remove every ith element of the array and update the sum of the remaining even-indexed elements and the odd-indexed elements based on the above observation. Check if the sums are equal or not. If found to be true, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntIndexesToMakeBalance( int arr[], int n)
{
if (n == 1) {
return 1;
}
if (n == 2)
return 0;
int sumEven = 0;
int sumOdd = 0;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
sumEven += arr[i];
}
else {
sumOdd += arr[i];
}
}
int currOdd = 0;
int currEven = arr[0];
int res = 0;
int newEvenSum = 0;
int newOddSum = 0;
for ( int i = 1; i < n - 1; i++) {
if (i % 2) {
currOdd += arr[i];
newEvenSum = currEven + sumOdd
- currOdd;
newOddSum = currOdd + sumEven
- currEven - arr[i];
}
else {
currEven += arr[i];
newOddSum = currOdd + sumEven
- currEven;
newEvenSum = currEven + sumOdd
- currOdd - arr[i];
}
if (newEvenSum == newOddSum) {
res++;
}
}
if (sumOdd == sumEven - arr[0]) {
res++;
}
if (n % 2 == 1) {
if (sumOdd == sumEven - arr[n - 1]) {
res++;
}
}
else {
if (sumEven == sumOdd - arr[n - 1]) {
res++;
}
}
return res;
}
int main()
{
int arr[] = { 1, 1, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntIndexesToMakeBalance(arr, n);
return 0;
}
|
Java
class GFG {
static int cntIndexesToMakeBalance( int arr[], int n)
{
if (n == 1 )
{
return 1 ;
}
if (n == 2 )
return 0 ;
int sumEven = 0 ;
int sumOdd = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (i % 2 == 0 )
{
sumEven += arr[i];
}
else
{
sumOdd += arr[i];
}
}
int currOdd = 0 ;
int currEven = arr[ 0 ];
int res = 0 ;
int newEvenSum = 0 ;
int newOddSum = 0 ;
for ( int i = 1 ; i < n - 1 ; i++)
{
if (i % 2 != 0 )
{
currOdd += arr[i];
newEvenSum = currEven + sumOdd
- currOdd;
newOddSum = currOdd + sumEven
- currEven - arr[i];
}
else
{
currEven += arr[i];
newOddSum = currOdd + sumEven
- currEven;
newEvenSum = currEven + sumOdd
- currOdd - arr[i];
}
if (newEvenSum == newOddSum)
{
res++;
}
}
if (sumOdd == sumEven - arr[ 0 ])
{
res++;
}
if (n % 2 == 1 )
{
if (sumOdd == sumEven - arr[n - 1 ])
{
res++;
}
}
else
{
if (sumEven == sumOdd - arr[n - 1 ])
{
res++;
}
}
return res;
}
public static void main (String[] args)
{
int arr[] = { 1 , 1 , 1 };
int n = arr.length;
System.out.println(cntIndexesToMakeBalance(arr, n));
}
}
|
Python3
def cntIndexesToMakeBalance(arr, n):
if (n = = 1 ):
return 1
if (n = = 2 ):
return 0
sumEven = 0
sumOdd = 0
for i in range (n):
if (i % 2 = = 0 ):
sumEven + = arr[i]
else :
sumOdd + = arr[i]
currOdd = 0
currEven = arr[ 0 ]
res = 0
newEvenSum = 0
newOddSum = 0
for i in range ( 1 , n - 1 ):
if (i % 2 ):
currOdd + = arr[i]
newEvenSum = (currEven + sumOdd -
currOdd)
newOddSum = (currOdd + sumEven -
currEven - arr[i])
else :
currEven + = arr[i]
newOddSum = (currOdd + sumEven -
currEven)
newEvenSum = (currEven + sumOdd -
currOdd - arr[i])
if (newEvenSum = = newOddSum):
res + = 1
if (sumOdd = = sumEven - arr[ 0 ]):
res + = 1
if (n % 2 = = 1 ):
if (sumOdd = = sumEven - arr[n - 1 ]):
res + = 1
else :
if (sumEven = = sumOdd - arr[n - 1 ]):
res + = 1
return res
if __name__ = = "__main__" :
arr = [ 1 , 1 , 1 ]
n = len (arr)
print (cntIndexesToMakeBalance(arr, n))
|
C#
using System;
class GFG {
static int cntIndexesToMakeBalance( int [] arr, int n)
{
if (n == 1)
{
return 1;
}
if (n == 2)
return 0;
int sumEven = 0;
int sumOdd = 0;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
sumEven += arr[i];
}
else
{
sumOdd += arr[i];
}
}
int currOdd = 0;
int currEven = arr[0];
int res = 0;
int newEvenSum = 0;
int newOddSum = 0;
for ( int i = 1; i < n - 1; i++)
{
if (i % 2 != 0)
{
currOdd += arr[i];
newEvenSum = currEven + sumOdd
- currOdd;
newOddSum = currOdd + sumEven
- currEven - arr[i];
}
else
{
currEven += arr[i];
newOddSum = currOdd + sumEven
- currEven;
newEvenSum = currEven + sumOdd
- currOdd - arr[i];
}
if (newEvenSum == newOddSum)
{
res++;
}
}
if (sumOdd == sumEven - arr[0])
{
res++;
}
if (n % 2 == 1)
{
if (sumOdd == sumEven - arr[n - 1])
{
res++;
}
}
else
{
if (sumEven == sumOdd - arr[n - 1])
{
res++;
}
}
return res;
}
public static void Main ()
{
int [] arr = { 1, 1, 1 };
int n = arr.Length;
Console.WriteLine(cntIndexesToMakeBalance(arr, n));
}
}
|
Javascript
<script>
function cntIndexesToMakeBalance(arr, n)
{
if (n == 1) {
return 1;
}
if (n == 2)
return 0;
let sumEven = 0;
let sumOdd = 0;
for (let i = 0; i < n; i++) {
if (i % 2 == 0) {
sumEven += arr[i];
}
else {
sumOdd += arr[i];
}
}
let currOdd = 0;
let currEven = arr[0];
let res = 0;
let newEvenSum = 0;
let newOddSum = 0;
for (let i = 1; i < n - 1; i++) {
if (i % 2) {
currOdd += arr[i];
newEvenSum = currEven + sumOdd
- currOdd;
newOddSum = currOdd + sumEven
- currEven - arr[i];
}
else {
currEven += arr[i];
newOddSum = currOdd + sumEven
- currEven;
newEvenSum = currEven + sumOdd
- currOdd - arr[i];
}
if (newEvenSum == newOddSum) {
res++;
}
}
if (sumOdd == sumEven - arr[0]) {
res++;
}
if (n % 2 == 1) {
if (sumOdd == sumEven - arr[n - 1]) {
res++;
}
}
else {
if (sumEven == sumOdd - arr[n - 1]) {
res++;
}
}
return res;
}
let arr = [ 1, 1, 1 ];
let n = arr.length;
document.write(cntIndexesToMakeBalance(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...