Count subarrays having sum of elements at even and odd positions equal
Last Updated :
11 Jul, 2022
Given an array arr[] of integers, the task is to find the total count of subarrays such that the sum of elements at even position and sum of elements at the odd positions are equal.
Examples:
Input: arr[] = {1, 2, 3, 4, 1}
Output: 1
Explanation:
{3, 4, 1} is the only subarray in which sum of elements at even position {3, 1} = sum of element at odd position {4}
Input: arr[] = {2, 4, 6, 4, 2}
Output: 2
Explanation:
There are two subarrays {2, 4, 6, 4} and {4, 6, 4, 2}.
Approach: The idea is to generate all possible subarrays. For each subarray formed find the sum of the elements at even index and subtract the elements at odd index. If the sum is 0, count this subarray else check for the next subarray.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countSubarrays( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int sum = 0;
for ( int j = i; j < n; j++)
{
if ((j - i) % 2 == 0)
sum += arr[j];
else
sum -= arr[j];
if (sum == 0)
count++;
}
}
cout << " " << count ;
}
int main()
{
int arr[] = { 2, 4, 6, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
countSubarrays(arr, n);
return 0;
}
|
C
#include <stdio.h>
void countSubarrays( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int sum = 0;
for ( int j = i; j < n; j++)
{
if ((j - i) % 2 == 0)
sum += arr[j];
else
sum -= arr[j];
if (sum == 0)
count++;
}
}
printf ( "%d" , count);
}
int main()
{
int arr[] = { 2, 4, 6, 4, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
countSubarrays(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void countSubarrays( int arr[],
int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
int sum = 0 ;
for ( int j = i; j < n; j++) {
if ((j - i) % 2 == 0 )
sum += arr[j];
else
sum -= arr[j];
if (sum == 0 )
count++;
}
}
System.out.println(count);
}
public static void
main(String[] args)
{
int arr[] = { 2 , 4 , 6 , 4 , 2 };
int n = arr.length;
countSubarrays(arr, n);
}
}
|
Python3
def countSubarrays(arr, n):
count = 0
for i in range (n):
sum = 0
for j in range (i, n):
if ((j - i) % 2 = = 0 ):
sum + = arr[j]
else :
sum - = arr[j]
if ( sum = = 0 ):
count + = 1
print (count)
if __name__ = = '__main__' :
arr = [ 2 , 4 , 6 , 4 , 2 ]
n = len (arr)
countSubarrays(arr, n)
|
C#
using System;
class GFG{
static void countSubarrays( int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
int sum = 0;
for ( int j = i; j < n; j++)
{
if ((j - i) % 2 == 0)
sum += arr[j];
else
sum -= arr[j];
if (sum == 0)
count++;
}
}
Console.WriteLine(count);
}
public static void Main(String[] args)
{
int []arr = { 2, 4, 6, 4, 2 };
int n = arr.Length;
countSubarrays(arr, n);
}
}
|
Javascript
<script>
function countSubarrays(arr, n)
{
var count = 0;
var i,j;
for (i = 0; i < n; i++)
{
var sum = 0;
for (j = i; j < n; j++)
{
if ((j - i) % 2 == 0)
sum += arr[j];
else
sum -= arr[j];
if (sum == 0)
count++;
}
}
document.write(count);
}
var arr = [2, 4, 6, 4, 2];
var n = arr.length;
countSubarrays(arr, n);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Share your thoughts in the comments
Please Login to comment...