Check if every Subarray of even length has sum 0
Last Updated :
21 Sep, 2023
Given an array A[ ] of size N, the task is to check if the sum of every even-sized subarray is 0 or not.
Examples:
Input: N = 4, A[] = {8, -8, 7, 9}
Output: NO
Explanation: Sum of subarray {7, 9} is not 0.
Input: N = 2, A[] = {0, 0}
Output: YES
Explanation: The only possible even length subarray is {0, 0} and its sum is 0.
Naive Approach: The basic way to solve the problem is as follows:
Generate all possible subarrays,then choose all even length subarray from that.Then, check if everytime its sum is 0 or not. If anytime it’s sum is not zero then return “NO” else return “YES”.
Steps to implement-
- Run two loops to find all subarrays
- Simultaneously calculate the length of all subarray
- If any subarray’s length is even then find its sum
- If anyone subarray of even length has non-zero sum then print “NO” else print “YES”
Code-
C++
#include <bits/stdc++.h>
using namespace std;
string solve( int N, int A[])
{
for ( int i=0;i<N;i++){
int length=0;
for ( int j=i;j<N;j++){
length++;
if (length%2==0){
int sum=0;
for ( int k=i;k<=j;k++){
sum+=A[k];
}
if (sum!=0){
return "NO" ;
}
}
}
}
return "YES" ;
}
int main()
{
int A[] = { 8, -8, 7, 9 };
int N = sizeof (A) / sizeof (A[0]);
cout << solve(N, A);
return 0;
}
|
Java
public class GFG {
static String solve( int N, int [] A) {
for ( int i = 0 ; i < N; i++) {
int length = 0 ;
for ( int j = i; j < N; j++) {
length++;
if (length % 2 == 0 ) {
int sum = 0 ;
for ( int k = i; k <= j; k++) {
sum += A[k];
}
if (sum != 0 ) {
return "NO" ;
}
}
}
}
return "YES" ;
}
public static void main(String[] args) {
int [] A = { 8 , - 8 , 7 , 9 };
int N = A.length;
System.out.println(solve(N, A));
}
}
|
Python3
def solve(N, A):
for i in range (N):
length = 0
for j in range (i, N):
length + = 1
if length % 2 = = 0 :
_sum = 0
for k in range (i, j + 1 ):
_sum + = A[k]
if _sum ! = 0 :
return "NO"
return "YES"
A = [ 8 , - 8 , 7 , 9 ]
N = len (A)
print (solve(N, A))
|
C#
using System;
using System.Collections.Generic;
class Gfg{
static string solve( int N, int [] A)
{
for ( int i=0;i<N;i++){
int length=0;
for ( int j=i;j<N;j++){
length++;
if (length%2==0){
int sum=0;
for ( int k=i;k<=j;k++){
sum+=A[k];
}
if (sum!=0){
return "NO" ;
}
}
}
}
return "YES" ;
}
static void Main( string [] args)
{
int [] A = { 8, -8, 7, 9 };
int N = A.Length;
Console.WriteLine(solve(N, A));
}
}
|
Javascript
function solve(N, A) {
for (let i = 0; i < N; i++) {
let length = 0;
for (let j = i; j < N; j++) {
length++;
if (length % 2 === 0) {
let sum = 0;
for (let k = i; k <= j; k++) {
sum += A[k];
}
if (sum !== 0) {
return "NO" ;
}
}
}
}
return "YES" ;
}
const A = [8, -8, 7, 9];
const N = A.length;
console.log(solve(N, A));
|
Time Complexity: O(N3), because of two nested loops to find all subarray and a third loop to find the sum of all elements of the subarray
Auxiliary Space: O(1), because no extra space has been used
Efficient Approach: To solve the problem follow the below idea:
The idea is to check the complete array once for all possible subarrays of length 2 because all other even sized subarrays of length greater than 2 can be made by combining subarrays of length 2. So if all subarrays of length 2 have sum 0, all other even sized subarrays will also have sum 0.
Follow the steps mentioned below to implement the idea:
- Start iterating from i = 1 to N-1:
- Check if the sum of A[i] and A[i-1] is 0 or not.
- If it is not 0, return the answer as “NO” and no need to calculate further.
- If the iteration is over and the condition is satisfied for all the subarrays, return “YES” as the required answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string solve( int N, int A[])
{
int ans = 1;
for ( int i = 1; i < N; i++) {
if (A[i] + A[i - 1] != 0) {
ans = 0;
break ;
}
}
if (ans)
return "YES" ;
return "NO" ;
}
int main()
{
int A[] = { 8, -8, 7, 9 };
int N = sizeof (A) / sizeof (A[0]);
cout << solve(N, A);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static String solve( int N, int A[])
{
int ans = 1 ;
for ( int i = 1 ; i < N; i++) {
if (A[i] + A[i - 1 ] != 0 ) {
ans = 0 ;
break ;
}
}
if (ans != 0 )
return "YES" ;
return "NO" ;
}
public static void main(String[] args)
{
int A[] = { 8 , - 8 , 7 , 9 };
int N = A.length;
System.out.print(solve(N, A));
}
}
|
Python3
def solve(N, A):
ans = 1
for i in range ( 1 , N):
if A[i] + A[i - 1 ] ! = 0 :
ans = 0
break
if ans:
return "YES"
return "NO"
A = [ 8 , - 8 , 7 , 9 ]
N = len (A)
print (solve(N, A))
|
C#
using System;
public class GFG {
static string solve( int N, int [] A)
{
int ans = 1;
for ( int i = 1; i < N; i++) {
if (A[i] + A[i - 1] != 0) {
ans = 0;
break ;
}
}
if (ans == 1)
return "YES" ;
return "NO" ;
}
static public void Main()
{
int [] A = { 8, -8, 7, 9 };
int N = A.Length;
Console.WriteLine(solve(N, A));
}
}
|
Javascript
const solve = (N, A) => {
let ans = 1;
for (let i = 1; i < N; i++) {
if (A[i] + A[i - 1] != 0) {
ans = 0;
break ;
}
}
if (ans)
return "YES" ;
return "NO" ;
}
let A = [8, -8, 7, 9];
let N = A.length;
console.log(solve(N, A));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...