Open In App

Balancing Odd-Even Index Sums with Subarray Negation

Last Updated : 09 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 yes, return true.
  • 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++




// C++ code for the above approach:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
 
// Function to check whether sum of elements
// is equal or not
bool solve(int N, vector<long long>& A)
{
 
    // Intialize a vector
    vector<ll> v(N);
 
    ll o = 0, e = 0;
    ll s = 0;
 
    // Iterate over the array
    for (int i = 0; i < N; i++) {
 
        // Find difference between sum of
        // elements of even and odd indexes
        if (i % 2)
            A[i] *= -1;
 
        s += A[i];
    }
 
    // If difference of elements is 0
    if (s == 0)
        return 1;
 
    // If difference is odd
    if (s % 2)
        return 0;
 
    s /= 2;
    map<int, int> mp;
    ll p = 0;
    mp[0] = 1;
 
    // Check for subarray having sum / 2
    for (int i = 0; i < N; i++) {
 
        p += A[i];
 
        // If difference is already present
        if (mp.find(p - s) != mp.end()) {
 
            return 1;
        }
        mp[p] = 1;
    }
    return 0;
}
 
// Driver code
int main()
{
 
    int N = 4;
    vector<long long> A = { 1, -1, 2, 2 };
 
    // Function call
    cout << solve(N, A);
 
    return 0;
}


Java




import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
 
public class Main {
 
    // Function to check whether sum of elements is equal or not
    static boolean solve(int N, Vector<Long> A) {
 
        // Initialize a vector
        Vector<Long> v = new Vector<Long>(N);
 
        long o = 0, e = 0;
        long s = 0;
 
        // Iterate over the array
        for (int i = 0; i < N; i++) {
 
            // Find the difference between the sum of
            // elements of even and odd indexes
            if (i % 2 == 1)
                A.set(i, A.get(i) * -1);
 
            s += A.get(i);
        }
 
        // If the difference of elements is 0
        if (s == 0)
            return true;
 
        // If the difference is odd
        if (s % 2 == 1)
            return false;
 
        s /= 2;
        Map<Long, Integer> mp = new HashMap<>();
        long p = 0;
        mp.put((long)0, 1);
 
        // Check for a subarray having sum / 2
        for (int i = 0; i < N; i++) {
 
            p += A.get(i);
 
            // If the difference is already present
            if (mp.containsKey(p - s)) {
 
                return true;
            }
            mp.put(p, 1);
        }
        return false;
    }
 
    // Driver code
    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);
 
        // Function call
        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
 
# Driver code
if __name__ == "__main__":
    N = 4
    A = [1, -1, 2, 2]
 
    # Function call
    print(solve(N, A))


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
    // Function to check whether sum of elements
    // is equal or not
    static bool Solve(int N, List<long> A)
    {
        long s = 0;
 
        // Iterate over the array
        for (int i = 0; i < N; i++) {
            // Find difference between sum of
            // elements of even and odd indexes
            if (i % 2 == 1)
                A[i] *= -1;
 
            s += A[i];
        }
 
        // If difference of elements is 0
        if (s == 0)
            return true;
 
        // If difference is odd
        if (s % 2 == 1)
            return false;
 
        s /= 2;
        Dictionary<long, int> mp
            = new Dictionary<long, int>();
        long p = 0;
        mp[0] = 1;
 
        // Check for subarray having sum / 2
        for (int i = 0; i < N; i++) {
            p += A[i];
 
            // If difference is already present
            if (mp.ContainsKey(p - s)) {
                return true;
            }
            mp[p] = 1;
        }
        return false;
    }
 
    // Driver code
    static void Main()
    {
        int N = 4;
        List<long> A = new List<long>{ 1, -1, 2, 2 };
 
        // Function call
        Console.WriteLine(Solve(N, A)? 1:0);
    }
}
 
// This code is contributed by Susobhan Akhuli


Javascript




// Javascript program for the above approach
 
// Function to check whether sum of elements
// is equal or not
function solve(N, A) {
    let o = 0, e = 0;
    let s = 0;
    let v = new Array(N);
 
    // Iterate over the array
    for (let i = 0; i < N; i++) {
 
        // Find difference between sum of
        // elements of even and odd indexes
        if (i % 2) {
            A[i] *= -1;
        }
        s += A[i];
    }
 
    // If difference of elements is 0
    if (s == 0) {
        return true;
    }
 
    // If difference is odd
    if (s % 2) {
        return false;
    }
 
    s /= 2;
    let mp = new Map();
    let p = 0;
    mp.set(0, 1);
 
    // Check for subarray having sum / 2
    for (let i = 0; i < N; i++) {
        p += A[i];
 
        // If difference is already present
        if (mp.has(p - s)) {
            return true;
        }
        mp.set(p, 1);
    }
    return false;
}
 
let N = 4;
let A = [1, -1, 2, 2];
 
// Function call
console.log(solve(N, A)? 1:0);
 
// This code is contributed by Susobhan Akhuli


Output

1







Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads