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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Jul, 2022
Like Article
Save Article