Balancing Odd-Even Index Sums with Subarray Negation
Last Updated :
09 Jan, 2024
Given an array A[] of size N. The task is to check whether the sum of elements of A on the odd and even indexes is equal or not, where you are allowed to choose a subarray A[i, j] with 1 ≤ i ≤ j ≤ N and multiply −1 by all elements of the subarray.
Examples:
Input: N = 5, A[] = [1, 5, -2, 3, -1]
Output: True
Explanation: apply the operation on the subarray A[3,4] making A=[1,5,2,−3,−1], Now the sum of elements on the odd indices is 1+2−1=2, and the sum of elements on the even indices is 5−3=2.
Input: N = 4, A[] = [-10, 7, 9, -1]
Output: False
Explanation: it is impossible to achieve the goal.
Approach: This can be solved with the following idea:
Find the difference between sum of elements present at even and odd indexes. Then find the if any subarray exists with sum / 2, as the both sum should be equal.
Below are the steps involved:
- Find the difference of sum between even and odd index.
- Check if the difference is 0:
- If the sum is odd, return false.
- Divide the sum by 2.
- Initialize a map, and find a subarray having a sum equal to sum / 2.
- Return true. if found
- Otherwise, Return false.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
bool solve( int N, vector< long long >& A)
{
vector<ll> v(N);
ll o = 0, e = 0;
ll s = 0;
for ( int i = 0; i < N; i++) {
if (i % 2)
A[i] *= -1;
s += A[i];
}
if (s == 0)
return 1;
if (s % 2)
return 0;
s /= 2;
map< int , int > mp;
ll p = 0;
mp[0] = 1;
for ( int i = 0; i < N; i++) {
p += A[i];
if (mp.find(p - s) != mp.end()) {
return 1;
}
mp[p] = 1;
}
return 0;
}
int main()
{
int N = 4;
vector< long long > A = { 1, -1, 2, 2 };
cout << solve(N, A);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class Main {
static boolean solve( int N, Vector<Long> A) {
Vector<Long> v = new Vector<Long>(N);
long o = 0 , e = 0 ;
long s = 0 ;
for ( int i = 0 ; i < N; i++) {
if (i % 2 == 1 )
A.set(i, A.get(i) * - 1 );
s += A.get(i);
}
if (s == 0 )
return true ;
if (s % 2 == 1 )
return false ;
s /= 2 ;
Map<Long, Integer> mp = new HashMap<>();
long p = 0 ;
mp.put(( long ) 0 , 1 );
for ( int i = 0 ; i < N; i++) {
p += A.get(i);
if (mp.containsKey(p - s)) {
return true ;
}
mp.put(p, 1 );
}
return false ;
}
public static void main(String[] args) {
int N = 4 ;
Vector<Long> A = new Vector<Long>();
A.add(( long ) 1 );
A.add(( long )- 1 );
A.add(( long ) 2 );
A.add(( long ) 2 );
System.out.println(solve(N, A));
}
}
|
Python3
def solve(N, A):
v = [ 0 ] * N
o, e, s = 0 , 0 , 0
for i in range (N):
if i % 2 :
A[i] * = - 1
s + = A[i]
if s = = 0 :
return True
if s % 2 :
return False
s / / = 2
mp = { 0 : 1 }
p = 0
for i in range (N):
p + = A[i]
if p - s in mp:
return True
mp[p] = 1
return False
if __name__ = = "__main__" :
N = 4
A = [ 1 , - 1 , 2 , 2 ]
print (solve(N, A))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static bool Solve( int N, List< long > A)
{
long s = 0;
for ( int i = 0; i < N; i++) {
if (i % 2 == 1)
A[i] *= -1;
s += A[i];
}
if (s == 0)
return true ;
if (s % 2 == 1)
return false ;
s /= 2;
Dictionary< long , int > mp
= new Dictionary< long , int >();
long p = 0;
mp[0] = 1;
for ( int i = 0; i < N; i++) {
p += A[i];
if (mp.ContainsKey(p - s)) {
return true ;
}
mp[p] = 1;
}
return false ;
}
static void Main()
{
int N = 4;
List< long > A = new List< long >{ 1, -1, 2, 2 };
Console.WriteLine(Solve(N, A)? 1:0);
}
}
|
Javascript
function solve(N, A) {
let o = 0, e = 0;
let s = 0;
let v = new Array(N);
for (let i = 0; i < N; i++) {
if (i % 2) {
A[i] *= -1;
}
s += A[i];
}
if (s == 0) {
return true ;
}
if (s % 2) {
return false ;
}
s /= 2;
let mp = new Map();
let p = 0;
mp.set(0, 1);
for (let i = 0; i < N; i++) {
p += A[i];
if (mp.has(p - s)) {
return true ;
}
mp.set(p, 1);
}
return false ;
}
let N = 4;
let A = [1, -1, 2, 2];
console.log(solve(N, A)? 1:0);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...