Given an integer array, the task is to divide the array into maximum number of sub-arrays such that averages of all subarrays is same. If it is not possible to divide, then print “Not possible”.

**Examples:**

Input : arr[] = {1, 5, 7, 2, 0}; Output : (0 1) (2 4) Subarrays arr[0..1] and arr[2..4] have same average. Input : arr[] = {4, 6, 2, 4, 8, 0, 6, 2}; Output : (0, 0) (1, 2) (3, 3) (4, 5) (6, 7) Input : arr[] = {3, 3, 3}; Output : (0, 0) (1, 1) (2, 2) Input : arr[] = {4, 3, 5, 9, 11}; Output : Not possible

The idea is based on the fact that if an array can be divided in subarrays of same average, then anerage of all these subarrays must be same as overall average.

1) Find average of whole array.

2) Traverse array again and keep track of average of current subarray. As soon as the average becomes same as overall overall average, print current subarray and begin new subarray.

This solution divides to maximum number of subarrays because we begin a new subarray as soon as we find average same as overall average.

## C++

`// C++ prpgram to break given array into maximum ` `// number of subarrays with equal average. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `findSubarrays(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store all points where we can break ` ` ` `// given array into equal average subarrays. ` ` ` `vector<` `int` `> result; ` ` ` ` ` `// Compute total array sum ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `int` `curr_sum = 0; ` `// Current Sum ` ` ` `int` `prev_index = -1; ` `// Index of previous subarray ` ` ` ` ` `for` `(` `int` `i = 0; i < n ; i++) ` ` ` `{ ` ` ` `curr_sum += arr[i]; ` ` ` ` ` `// If current point is a break point. Note that ` ` ` `// we don't compare actual averages to avoid ` ` ` `// floating point errors. ` ` ` `if` `(sum * (i - prev_index) == curr_sum * n) ` ` ` `{ ` ` ` `// Update current sum and previous index ` ` ` `curr_sum = 0; ` ` ` `prev_index = i; ` ` ` ` ` `// Add current break point ` ` ` `result.push_back(i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If last break point was not end of array, we ` ` ` `// cannot break the whole array. ` ` ` `if` `(prev_index != n-1) ` ` ` `{ ` ` ` `cout << ` `"Not Possible"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Printing the result in required format ` ` ` `cout << ` `"(0, "` `<< result[0] << ` `")\n"` `; ` ` ` `for` `(` `int` `i=1; i<result.size(); i++) ` ` ` `cout << ` `"("` `<< result[i-1] + 1 << ` `", "` ` ` `<< result[i] << ` `")\n"` `; ` `} ` ` ` `// Main Entry function code ` `int` `main() ` `{ ` ` ` `int` `arr[] = {4, 6, 2, 4, 8, 0, 6, 2}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `findSubarrays(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to break given array into maximum ` `// number of subarrays with equal average. ` `import` `java.util.Vector; ` `class` `GFG { ` ` ` `static` `void` `findSubarrays(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store all points where we can break ` ` ` `// given array into equal average subarrays. ` ` ` `Vector<Integer> result = ` `new` `Vector<>(); ` ` ` ` ` `// Compute total array sum ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `int` `curr_sum = ` `0` `; ` `// Current Sum ` ` ` `int` `prev_index = -` `1` `; ` `// Index of previous subarray ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n ; i++) ` ` ` `{ ` ` ` `curr_sum += arr[i]; ` ` ` ` ` `// If current point is a break point. Note that ` ` ` `// we don't compare actual averages to avoid ` ` ` `// floating point errors. ` ` ` `if` `(sum *(i - prev_index) == curr_sum*n) ` ` ` `{ ` ` ` `// Update current sum and previous index ` ` ` `curr_sum = ` `0` `; ` ` ` `prev_index = i; ` ` ` ` ` `// Add current break point ` ` ` `result.add(i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If last break point was not end of array, we ` ` ` `// cannot break the whole array. ` ` ` `if` `(prev_index != n-` `1` `) ` ` ` `{ ` ` ` `System.out.println(` `"Not Possible"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Printing the result in required format ` ` ` `System.out.print(` `"(0, "` `+ result.get(` `0` `) + ` `")\n"` `); ` ` ` `for` `(` `int` `i=` `1` `; i<result.size(); i++) ` ` ` `System.out.print(` `"("` `+ (result.get(i-` `1` `) + ` `1` `) + ` `", "` ` ` `+ result.get(i) + ` `")\n"` `); ` `} ` ` ` `// Main Entry function code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `arr[] = {` `4` `, ` `6` `, ` `2` `, ` `4` `, ` `8` `, ` `0` `, ` `6` `, ` `2` `}; ` ` ` `int` `n = arr.length; ` ` ` `findSubarrays(arr, n); ` ` ` `} ` `} ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 prpgram to break given array ` `# into maximum number of subarrays with ` `# equal average. ` ` ` `def` `findSubarrays(arr, n): ` ` ` ` ` `# To store all points where we can break ` ` ` `# given array into equal average subarrays. ` ` ` `result ` `=` `[] ` ` ` ` ` `# Compute total array sum ` ` ` `sum` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `1` `): ` ` ` `sum` `+` `=` `arr[i] ` ` ` ` ` `curr_sum ` `=` `0` `# Current Sum ` ` ` `prev_index ` `=` `-` `1` `# Index of previous subarray ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, n, ` `1` `): ` ` ` `curr_sum ` `+` `=` `arr[i] ` ` ` ` ` `# If current point is a break point. ` ` ` `# Note that we don't compare actual ` ` ` `# averages to avoid floating point errors. ` ` ` `if` `(` `sum` `*` `(i ` `-` `prev_index) ` `=` `=` `curr_sum ` `*` `n): ` ` ` ` ` `# Update current sum and ` ` ` `# previous index ` ` ` `curr_sum ` `=` `0` ` ` `prev_index ` `=` `i ` ` ` ` ` `# Add current break point ` ` ` `result.append(i) ` ` ` ` ` `# If last break point was not end of ` ` ` `# array, we cannot break the whole array. ` ` ` `if` `(prev_index !` `=` `n ` `-` `1` `): ` ` ` `print` `(` `"Not Possible"` `, end ` `=` `" "` `) ` ` ` ` ` `# Printing the result in required format ` ` ` `print` `(` `"( 0 ,"` `, result[` `0` `], ` `")"` `) ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(result), ` `1` `): ` ` ` `print` `(` `"("` `, result[i ` `-` `1` `] ` `+` `1` `, ` `","` `, ` ` ` `result[i],` `")"` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `4` `, ` `6` `, ` `2` `, ` `4` `, ` `8` `, ` `0` `, ` `6` `, ` `2` `] ` ` ` `n ` `=` `len` `(arr) ` ` ` `findSubarrays(arr, n) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to break given array into maximum ` `// number of subarrays with equal average. ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `findSubarrays(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `// To store all points where we can break ` ` ` `// given array into equal average subarrays. ` ` ` `List<` `int` `> result = ` `new` `List<` `int` `>(); ` ` ` ` ` `// Compute total array sum ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `sum += arr[i]; ` ` ` ` ` `int` `curr_sum = 0; ` `// Current Sum ` ` ` `int` `prev_index = -1; ` `// Index of previous subarray ` ` ` ` ` `for` `(` `int` `i = 0; i < n ; i++) ` ` ` `{ ` ` ` `curr_sum += arr[i]; ` ` ` ` ` `// If current point is a break point. Note that ` ` ` `// we don't compare actual averages to avoid ` ` ` `// floating point errors. ` ` ` `if` `(sum *(i - prev_index) == curr_sum*n) ` ` ` `{ ` ` ` `// Update current sum and previous index ` ` ` `curr_sum = 0; ` ` ` `prev_index = i; ` ` ` ` ` `// Add current break point ` ` ` `result.Add(i); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If last break point was not end of array, we ` ` ` `// cannot break the whole array. ` ` ` `if` `(prev_index != n-1) ` ` ` `{ ` ` ` `Console.Write(` `"Not Possible"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Printing the result in required format ` ` ` `Console.Write(` `"(0, "` `+ result[0] + ` `")\n"` `); ` ` ` `for` `(` `int` `i = 1; i < result.Count; i++) ` ` ` `Console.Write(` `"("` `+ (result[i-1] + 1) + ` `", "` ` ` `+ result[i] + ` `")\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = {4, 6, 2, 4, 8, 0, 6, 2}; ` ` ` `int` `n = arr.Length; ` ` ` `findSubarrays(arr, n); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

(0, 0) (1, 2) (3, 3) (4, 5) (6, 7)

**Time complexity: **O(n)

**Auxiliary Space: **O(1)

This article is contributed by **Krishna Kumar**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Divide array into two sub-arrays such that their averages are equal
- Split array into two subarrays such that difference of their maximum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Number of subarrays whose minimum and maximum are same
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Find number of pairs in an array such that their XOR is 0