Ways to make sum of odd and even indexed elements equal by removing an array element
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[1] = arr[2]
Removing arr[1] from the given array modifies arr[] to [ 1, 1 ] such that arr[0] = arr[2]
Removing arr[2] from the given array modifies arr[] to [ 1, 1 ] such that arr[0] = arr[1]
Therefore, the required output is 3.
Table of Content
[Naive Approach] Using Nested Loop – O(n^2) Time and O(1) Space
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.
#include <bits/stdc++.h>
using namespace std;
int countIndices(vector<int>& arr) {
int n = arr.size();
int count = 0;
// Traverse the array and check if
// removing each element makes the
// sum of even and odd indexed elements equal
for (int i = 0; i < n; ++i) {
int evenSum = 0, oddSum = 0;
// Calculate the sum of even and
// odd indexed elements excluding arr[i]
int k = 0;
for (int j = 0; j < n; ++j) {
// Skip the current element
if (j == i) continue;
if (k % 2 == 0) {
evenSum += arr[j];
} else {
oddSum += arr[j];
}
k++;
}
// If the sums of even and odd indexed elements are equal, increment the count
if (evenSum == oddSum) {
count++;
}
}
return count;
}
int main() {
vector<int> arr = {2,1,6,4};
cout << countIndices(arr) << endl;
return 0;
}
public class Main {
public static int countIndices(int[] arr) {
int n = arr.length;
int count = 0;
// Traverse the array and check if removing
// each element makes the sum of even and
// odd indexed elements equal
for (int i = 0; i < n; i++) {
int evenSum = 0, oddSum = 0;
// Calculate the sum of even and
// odd indexed elements excluding arr[i]
int k = 0;
for (int j = 0; j < n; j++) {
// Skip the current element
if (j == i) continue;
if (k % 2 == 0) {
evenSum += arr[j];
} else {
oddSum += arr[j];
}
k++;
}
// If the sums of even and odd indexed elements are equal, increment the count
if (evenSum == oddSum) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] arr = {2,1,6,4};
System.out.println(countIndices(arr));
}
}
def countIndices(arr):
n = len(arr)
count = 0
# Traverse the array and check if removing
# each element makes the sum of even and
# odd indexed elements equal
for i in range(n):
even_sum = 0
odd_sum = 0
# Calculate the sum of even and odd
# indexed elements excluding arr[i]
k = 0
for j in range(n):
# Skip the current element
if j == i:
continue
if k % 2 == 0:
even_sum += arr[j]
else:
odd_sum += arr[j]
k += 1
# If the sums of even and odd indexed
# elements are equal, increment the count
if even_sum == odd_sum:
count += 1
return count
# Driver code
arr = [2,1,6,4]
print(countIndices(arr))
using System;
class Program
{
public static int countIndices(int[] arr)
{
int n = arr.Length;
int count = 0;
// Traverse the array and check if removing each element makes the sum of even and odd indexed elements equal
for (int i = 0; i < n; i++)
{
int evenSum = 0, oddSum = 0;
// Calculate the sum of even and
// odd indexed elements excluding arr[i]
int k = 0;
for (int j = 0; j < n; j++)
{
// Skip the current element
if (j == i) continue;
if (k % 2 == 0)
{
evenSum += arr[j];
}
else
{
oddSum += arr[j];
}
k++;
}
// If the sums of even and odd indexed
// elements are equal, increment the count
if (evenSum == oddSum)
{
count++;
}
}
return count;
}
static void Main()
{
int[] arr = {2,1,6,4};
Console.WriteLine(countIndices(arr));
}
}
function countIndices(arr) {
let n = arr.length;
let count = 0;
// Traverse the array and check if removing each element makes the sum of even and odd indexed elements equal
for (let i = 0; i < n; i++) {
let evenSum = 0, oddSum = 0;
// Calculate the sum of even and odd
// indexed elements excluding arr[i]
let k = 0;
for (let j = 0; j < n; j++) {
// Skip the current element
if (j === i) continue;
if (k % 2 === 0) {
evenSum += arr[j];
} else {
oddSum += arr[j];
}
k++;
}
// If the sums of even and odd indexed
// elements are equal, increment the count
if (evenSum === oddSum) {
count++;
}
}
return count;
}
// Driver code
const arr = [2,1,6,4];
console.log(countIndices(arr));
Output
1
[Expected Approach] Using One variable – O(n) Time and O(1) Space
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:
- Initial Calculation: Compute the total sum of elements at even indices and the total sum of elements at odd indices by iterating over the entire array.
- Track Running Sums: As you iterate through the array, maintain running sums for even and odd indexed elements up to the current index.
- Balance Check: After processing each element, check if removing it results in equal sums for the remaining even and odd indexed elements. If true, count it as a valid index.
#include <bits/stdc++.h>
using namespace std;
int cntIdx(vector<int>& a) {
int n = a.size();
if (n == 1) return 1;
if (n == 2) return 0;
// Compute overall even and odd sums
int eSum = 0, oSum = 0, res = 0;
for (int i = 0; i < n; i++)
(i % 2 ? oSum : eSum) += a[i];
// Try removing every element, keep
// track of current even and odd sums
// and check if updated new even and odd
// sums become same
int cE = 0, cO = 0;
for (int i = 0; i < n; i++) {
(i % 2 ? cO : cE) += a[i];
int nESum = cE + oSum - cO;
int nOSum = cO + eSum - cE - a[i];
if (nESum == nOSum) res++;
}
return res;
}
int main() {
vector<int> a = {1, 1, 1};
cout << cntIdx(a);
return 0;
}
public class Main {
public static int cntIdx(int[] a) {
int n = a.length;
if (n == 1)
return 1;
if (n == 2)
return 0;
// Compute overall even and odd sums
int eSum = 0;
int oSum = 0;
int res = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1)
oSum += a[i];
else
eSum += a[i];
}
// Try removing every element, keep
// track of current even and odd sums
// and check if updated new even and odd
// sums become same
int cE = 0;
int cO = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 1)
cO += a[i];
else
cE += a[i];
int nESum = cE + oSum - cO;
int nOSum = cO + eSum - cE - a[i];
if (nESum == nOSum)
res++;
}
return res;
}
public static void main(String[] args) {
int[] a = { 1, 1, 1 };
System.out.println(cntIdx(a));
}
}
def cntIdx(a):
n = len(a)
if n == 1:
return 1
if n == 2:
return 0
# Compute overall even and odd sums
eSum = 0
oSum = 0
res = 0
for i in range(n):
if i % 2:
oSum += a[i]
else:
eSum += a[i]
# Try removing every element, keep
# track of current even and odd sums
# and check if updated new even and odd
# sums become same
cE = 0
cO = 0
for i in range(n):
if i % 2:
cO += a[i]
else:
cE += a[i]
nESum = cE + oSum - cO
nOSum = cO + eSum - cE - a[i]
if nESum == nOSum:
res += 1
return res
if __name__ == '__main__':
a = [1, 1, 1]
print(cntIdx(a))
using System;
using System.Linq;
class Program
{
static int CntIdx(int[] a)
{
int n = a.Length;
if (n == 1)
return 1;
if (n == 2)
return 0;
// Compute overall even and odd sums
int eSum = 0;
int oSum = 0;
int res = 0;
for (int i = 0; i < n; i++)
{
if (i % 2 == 1)
oSum += a[i];
else
eSum += a[i];
}
// Try removing every element, keep
// track of current even and odd sums
// and check if updated new even and odd
// sums become same
int cE = 0;
int cO = 0;
for (int i = 0; i < n; i++)
{
if (i % 2 == 1)
cO += a[i];
else
cE += a[i];
int nESum = cE + oSum - cO;
int nOSum = cO + eSum - cE - a[i];
if (nESum == nOSum)
res++;
}
return res;
}
static void Main(string[] args)
{
int[] a = { 1, 1, 1 };
Console.WriteLine(CntIdx(a));
}
}
function cntIdx(a) {
let n = a.length;
if (n === 1) return 1;
if (n === 2) return 0;
// Compute overall even and odd sums
let eSum = 0, oSum = 0, res = 0;
for (let i = 0; i < n; i++) {
if (i % 2) {
oSum += a[i];
} else {
eSum += a[i];
}
}
// Try removing every element, keep
// track of current even and odd sums
// and check if updated new even and odd
// sums become same
let cE = 0, cO = 0;
for (let i = 0; i < n; i++) {
if (i % 2) {
cO += a[i];
} else {
cE += a[i];
}
let nESum = cE + oSum - cO;
let nOSum = cO + eSum - cE - a[i];
if (nESum === nOSum) res++;
}
return res;
}
const a = [1, 1, 1];
console.log(cntIdx(a));
Output
3