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:1Explanation:

{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:2Explanation:

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++

`// C program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count subarrays in` `// which sum of elements at even` `// and odd positions are equal` `void` `countSubarrays(` `int` `arr[], ` `int` `n)` `{` ` ` ` ` `// Initialize variables` ` ` `int` `count = 0;` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `j = i; j < n; j++)` ` ` `{` ` ` ` ` `// Check if position is` ` ` `// even then add to sum` ` ` `// then add it to sum` ` ` `if` `((j - i) % 2 == 0)` ` ` `sum += arr[j];` ` ` `// Else subtract it to sum` ` ` `else` ` ` `sum -= arr[j];` ` ` `// Increment the count` ` ` `// if the sum equals 0` ` ` `if` `(sum == 0)` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the count of subarrays` ` ` `cout << ` `" "` `<< count ;` `}` `// Driver Code` `int` `main()` `{` ` ` ` ` `// Given array arr[]` ` ` `int` `arr[] = { 2, 4, 6, 4, 2 };` ` ` `// Size of the array` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function call` ` ` `countSubarrays(arr, n);` ` ` `return` `0;` `}` `// This code is contributed by shivanisinghss2110` |

*chevron_right*

*filter_none*

## C

`// C program for the above approach` `#include <stdio.h>` `// Function to count subarrays in` `// which sum of elements at even` `// and odd positions are equal` `void` `countSubarrays(` `int` `arr[], ` `int` `n)` `{` ` ` ` ` `// Initialize variables` ` ` `int` `count = 0;` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `j = i; j < n; j++)` ` ` `{` ` ` ` ` `// Check if position is` ` ` `// even then add to sum` ` ` `// then add it to sum` ` ` `if` `((j - i) % 2 == 0)` ` ` `sum += arr[j];` ` ` `// Else subtract it to sum` ` ` `else` ` ` `sum -= arr[j];` ` ` `// Increment the count` ` ` `// if the sum equals 0` ` ` `if` `(sum == 0)` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the count of subarrays` ` ` `printf` `(` `"%d"` `, count);` `}` `// Driver Code` `int` `main()` `{` ` ` ` ` `// Given array arr[]` ` ` `int` `arr[] = { 2, 4, 6, 4, 2 };` ` ` `// Size of the array` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function call` ` ` `countSubarrays(arr, n);` ` ` `return` `0;` `}` `// This code is contributed by piyush3010` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` ` ` `// Function to count subarrays in` ` ` `// which sum of elements at even` ` ` `// and odd positions are equal` ` ` `static` `void` `countSubarrays(` `int` `arr[],` ` ` `int` `n)` ` ` `{` ` ` `// Initialize variables` ` ` `int` `count = ` `0` `;` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) {` ` ` `int` `sum = ` `0` `;` ` ` `for` `(` `int` `j = i; j < n; j++) {` ` ` `// Check if position is` ` ` `// even then add to sum` ` ` `// then add it to sum` ` ` `if` `((j - i) % ` `2` `== ` `0` `)` ` ` `sum += arr[j];` ` ` `// else subtract it to sum` ` ` `else` ` ` `sum -= arr[j];` ` ` `// Increment the count` ` ` `// if the sum equals 0` ` ` `if` `(sum == ` `0` `)` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the count of subarrays` ` ` `System.out.println(count);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` ` ` `main(String[] args)` ` ` `{` ` ` `// Given array arr[]` ` ` `int` `arr[] = { ` `2` `, ` `4` `, ` `6` `, ` `4` `, ` `2` `};` ` ` `// Size of the array` ` ` `int` `n = arr.length;` ` ` `// Function call` ` ` `countSubarrays(arr, n);` ` ` `}` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to count subarrays in` `# which sum of elements at even` `# and odd positions are equal` `def` `countSubarrays(arr, n):` ` ` `# Initialize variables` ` ` `count ` `=` `0` ` ` `# Iterate over the array` ` ` `for` `i ` `in` `range` `(n):` ` ` `sum` `=` `0` ` ` ` ` `for` `j ` `in` `range` `(i, n):` ` ` `# Check if position is` ` ` `# even then add to sum` ` ` `# hen add it to sum` ` ` `if` `((j ` `-` `i) ` `%` `2` `=` `=` `0` `):` ` ` `sum` `+` `=` `arr[j]` ` ` `# else subtract it to sum` ` ` `else` `:` ` ` `sum` `-` `=` `arr[j]` ` ` `# Increment the count` ` ` `# if the sum equals 0` ` ` `if` `(` `sum` `=` `=` `0` `):` ` ` `count ` `+` `=` `1` ` ` ` ` `# Print the count of subarrays` ` ` `print` `(count)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given array arr[]` ` ` `arr ` `=` `[ ` `2` `, ` `4` `, ` `6` `, ` `4` `, ` `2` `]` ` ` `# Size of the array` ` ` `n ` `=` `len` `(arr)` ` ` `# Function call` ` ` `countSubarrays(arr, n)` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to count subarrays in` `// which sum of elements at even` `// and odd positions are equal` `static` `void` `countSubarrays(` `int` `[]arr, ` `int` `n)` `{` ` ` ` ` `// Initialize variables` ` ` `int` `count = 0;` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `int` `sum = 0;` ` ` `for` `(` `int` `j = i; j < n; j++) ` ` ` `{` ` ` ` ` `// Check if position is` ` ` `// even then add to sum` ` ` `// then add it to sum` ` ` `if` `((j - i) % 2 == 0)` ` ` `sum += arr[j];` ` ` `// else subtract it to sum` ` ` `else` ` ` `sum -= arr[j];` ` ` `// Increment the count` ` ` `// if the sum equals 0` ` ` `if` `(sum == 0)` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the count of subarrays` ` ` `Console.WriteLine(count);` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` ` ` `// Given array []arr` ` ` `int` `[]arr = { 2, 4, 6, 4, 2 };` ` ` `// Size of the array` ` ` `int` `n = arr.Length;` ` ` `// Function call` ` ` `countSubarrays(arr, n);` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:*** O(N ^{2})*

**Auxiliary Space:**

*O(1)*

## Recommended Posts:

- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count subarrays with same even and odd elements
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Count of subarrays of size K with elements having even frequencies
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Even numbers at even index and odd numbers at odd index
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Generate an array of given size with equal count and sum of odd and even numbers
- Count of subarrays having sum equal to its length
- Number of permutations such that sum of elements at odd index and even index are equal
- Split an Array A[] into Subsets having equal Sum and sizes equal to elements of Array B[]
- Check if count of even divisors of N is equal to count of odd divisors
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.