Divide array into two sub-arrays such that their averages are equal
Last Updated :
07 Jul, 2022
Given an integer array, the task is to divide an integer array into two sub-arrays to make their averages equal if possible.
Examples :
Input : arr[] = {1, 5, 7, 2, 0};
Output : (0 1) and (2 4)
Subarrays arr[0..1] and arr[2..4] have
same average.
Input : arr[] = {4, 3, 5, 9, 11};
Output : Not possible
Asked in Microsoft
A Naive Approach is to run two loops and find subarrays whose averages are equal.
Implementation:
C++
// Simple C++ program to find subarrays
// whose averages are equal
#include<bits/stdc++.h>
using namespace std;
// Finding two subarrays
// with equal average.
void findSubarrays(int arr[], int n)
{
bool found = false;
int lsum = 0;
for (int i = 0; i < n - 1; i++)
{
lsum += arr[i];
int rsum = 0;
for (int j = i + 1; j < n; j++)
rsum += arr[j];
// If averages of arr[0...i] and
// arr[i+1..n-1] are same. To avoid
// floating point problems we compare
// "lsum*(n-i+1)" and "rsum*(i+1)"
// instead of "lsum/(i+1)" and
// "rsum/(n-i+1)"
if (lsum * (n - i - 1) ==
rsum * (i + 1))
{
printf("From (%d %d) to (%d %d)\n",
0, i, i + 1, n - 1);
found = true;
}
}
// If no subarrays found
if (found == false)
cout << "Subarrays not found"
<< endl;
}
// Driver code
int main()
{
int arr[] = {1, 5, 7, 2, 0};
int n = sizeof(arr) / sizeof(arr[0]);
findSubarrays(arr, n);
return 0;
}
Java
// Simple Java program to find subarrays
// whose averages are equal
public class GFG {
// Finding two subarrays
// with equal average.
static void findSubarrays(int[] arr, int n)
{
boolean found = false;
int lsum = 0;
for (int i = 0; i < n - 1; i++)
{
lsum += arr[i];
int rsum = 0;
for (int j = i + 1; j < n; j++)
rsum += arr[j];
// If averages of arr[0...i] and
// arr[i+1..n-1] are same. To avoid
// floating point problems we compare
// "lsum*(n-i+1)" and "rsum*(i+1)"
// instead of "lsum/(i+1)" and
// "rsum/(n-i+1)"
if (lsum * (n - i - 1) ==
rsum * (i + 1))
{
System.out.println("From (0 " + i
+ ") to (" +(i + 1) + " "
+ (n - 1)+ ")");
found = true;
}
}
// If no subarrays found
if (found == false)
System.out.println( "Subarrays not "
+ "found");
}
// Driver code
static public void main (String[] args)
{
int[] arr = {1, 5, 7, 2, 0};
int n = arr.length;
findSubarrays(arr, n);
}
}
// This code is contributed by Mukul Singh.
Python 3
# Simple Python 3 program to find subarrays
# whose averages are equal
# Finding two subarrays with equal average.
def findSubarrays(arr, n):
found = False
lsum = 0
for i in range(n - 1):
lsum += arr[i]
rsum = 0
for j in range(i + 1, n):
rsum += arr[j]
# If averages of arr[0...i] and
# arr[i+1..n-1] are same. To avoid
# floating point problems we compare
# "lsum*(n-i+1)" and "rsum*(i+1)"
# instead of "lsum/(i+1)" and
# "rsum/(n-i+1)"
if (lsum * (n - i - 1) == rsum * (i + 1)):
print("From", "(", 0, i, ")",
"to", "(", i + 1, n - 1, ")")
found = True
# If no subarrays found
if (found == False):
print("Subarrays not found")
# Driver code
if __name__ == "__main__":
arr = [1, 5, 7, 2, 0]
n = len(arr)
findSubarrays(arr, n)
# This code is contributed by ita_c
C#
// Simple C# program to find subarrays
// whose averages are equal
using System;
public class GFG {
// Finding two subarrays
// with equal average.
static void findSubarrays(int []arr, int n)
{
bool found = false;
int lsum = 0;
for (int i = 0; i < n - 1; i++)
{
lsum += arr[i];
int rsum = 0;
for (int j = i + 1; j < n; j++)
rsum += arr[j];
// If averages of arr[0...i] and
// arr[i+1..n-1] are same. To avoid
// floating point problems we compare
// "lsum*(n-i+1)" and "rsum*(i+1)"
// instead of "lsum/(i+1)" and
// "rsum/(n-i+1)"
if (lsum * (n - i - 1) ==
rsum * (i + 1))
{
Console.WriteLine("From ( 0 " + i
+ ") to(" + (i + 1) + " "
+ (n - 1) + ")");
found = true;
}
}
// If no subarrays found
if (found == false)
Console.WriteLine( "Subarrays not "
+ "found");
}
// Driver code
static public void Main ()
{
int []arr = {1, 5, 7, 2, 0};
int n = arr.Length;
findSubarrays(arr, n);
}
}
// This code is contributed by anuj_67.
PHP
<?php
// Simple PHP program to find subarrays
// whose averages are equal
// Finding two subarrays
// with equal average.
function findSubarrays( $arr, $n)
{
$found = false;
$lsum = 0;
for ( $i = 0; $i < $n - 1; $i++)
{
$lsum += $arr[$i];
$rsum = 0;
for ( $j = $i + 1; $j < $n; $j++)
$rsum += $arr[$j];
// If averages of arr[0...i] and
// arr[i+1..n-1] are same. To avoid
// floating point problems we compare
// "lsum*(n-i+1)" and "rsum*(i+1)"
// instead of "lsum/(i+1)" and "rsum/(n-i+1)"
if ($lsum * ($n - $i - 1) ==
$rsum * ($i + 1))
{
echo "From ( 0 ", $i," )".
" to (", $i + 1," ", $n - 1,")\n";
$found = true;
}
}
// If no subarrays found
if ($found == false)
echo "Subarrays not found" ;
}
// Driver code
$arr = array(1, 5, 7, 2, 0);
$n = count($arr);
findSubarrays($arr, $n);
// This code is contributed by vt_m
?>
JavaScript
<script>
// Simple Javascript program to find subarrays
// whose averages are equal
// Finding two subarrays
// with equal average.
function findSubarrays(arr,n)
{
let found = false;
let lsum = 0;
for (let i = 0; i < n - 1; i++)
{
lsum += arr[i];
let rsum = 0;
for (let j = i + 1; j < n; j++)
rsum += arr[j];
// If averages of arr[0...i] and
// arr[i+1..n-1] are same. To avoid
// floating point problems we compare
// "lsum*(n-i+1)" and "rsum*(i+1)"
// instead of "lsum/(i+1)" and
// "rsum/(n-i+1)"
if (lsum * (n - i - 1) ==
rsum * (i + 1))
{
document.write("From (0 " + i
+ ") to (" +(i + 1) + " "
+ (n - 1)+ ")");
found = true;
}
}
// If no subarrays found
if (found == false)
document.write( "Subarrays not "
+ "found");
}
// Driver code
let arr=[1, 5, 7, 2, 0];
let n = arr.length;
findSubarrays(arr, n);
// This code is contributed by avanitrachhadiya2155
</script>
OutputFrom (0 1) to (2 4)
Time complexity : O(n2)
Auxiliary Space : O(1)
An Efficient solution is to find sum of array elements. Initialize leftsum as zero. Run a loop and find leftsum by adding elements of array. For rightsum, we subtract leftsum from total sum then we find rightsum and find average of leftsum and rightsum as according to their index.
1) Compute sum of all array elements. Let this
sum be "sum"
2) Initialize leftsum = 0.
3) Run a loop for i=0 to n-1.
a) leftsum = leftsum + arr[i]
b) rightsum = sum - leftsum
c) If average of left and right are same,
print current index as output.
Below is the implementation for above approach:
C++
// Efficient C++ program for
// dividing array to make
// average equal
#include<bits/stdc++.h>
using namespace std;
void findSubarrays(int arr[], int n)
{
// Find array sum
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
bool found = false;
int lsum = 0;
for (int i = 0; i < n - 1; i++)
{
lsum += arr[i];
int rsum = sum - lsum;
// If averages of arr[0...i]
// and arr[i+1..n-1] are same.
// To avoid floating point problems
// we compare "lsum*(n-i+1)"
// and "rsum*(i+1)" instead of
// "lsum/(i+1)" and "rsum/(n-i+1)"
if (lsum * (n - i - 1) == rsum * (i + 1))
{
printf("From (%d %d) to (%d %d)\n",
0, i, i+1, n-1);
found = true;
}
}
// If no subarrays found
if (found == false)
cout << "Subarrays not found"
<< endl;
}
// Driver code
int main()
{
int arr[] = {1, 5, 7, 2, 0};
int n = sizeof(arr) / sizeof(arr[0]);
findSubarrays(arr, n);
return 0;
}
Java
// Efficient Java program for
// dividing array to make
// average equal
import java.util.*;
class GFG
{
static void findSubarrays(int arr[], int n)
{
// Find array sum
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
boolean found = false;
int lsum = 0;
for (int i = 0; i < n - 1; i++)
{
lsum += arr[i];
int rsum = sum - lsum;
// If averages of arr[0...i]
// and arr[i+1..n-1] are same.
// To avoid floating point problems
// we compare "lsum*(n-i+1)"
// and "rsum*(i+1)" instead of
// "lsum/(i+1)" and "rsum/(n-i+1)"
if (lsum * (n - i - 1) == rsum * (i + 1))
{
System.out.printf("From (%d %d) to (%d %d)\n",
0, i, i + 1, n - 1);
found = true;
}
}
// If no subarrays found
if (found == false)
System.out.println("Subarrays not found");
}
// Driver code
static public void main ( String []arg)
{
int arr[] = {1, 5, 7, 2, 0};
int n = arr.length;
findSubarrays(arr, n);
}
}
// This code is contributed by Princi Singh
Python3
# Efficient Python program for
# dividing array to make
# average equal
def findSubarrays(arr, n):
# Find array sum
sum = 0;
for i in range(n):
sum += arr[i];
found = False;
lsum = 0;
for i in range(n - 1):
lsum += arr[i];
rsum = sum - lsum;
# If averages of arr[0...i]
# and arr[i + 1..n - 1] are same.
# To avoid floating point problems
# we compare "lsum*(n - i + 1)"
# and "rsum*(i + 1)" instead of
# "lsum / (i + 1)" and "rsum/(n - i + 1)"
if (lsum * (n - i - 1) == rsum * (i + 1)):
print("From (%d %d) to (%d %d)\n"%
(0, i, i + 1, n - 1));
found = True;
# If no subarrays found
if (found == False):
print("Subarrays not found");
# Driver code
if __name__ == '__main__':
arr = [ 1, 5, 7, 2, 0 ];
n = len(arr);
findSubarrays(arr, n);
# This code is contributed by Rajput-Ji
C#
// Efficient C# program for
// dividing array to make
// average equal
using System;
class GFG
{
static void findSubarrays(int []arr, int n)
{
// Find array sum
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
bool found = false;
int lsum = 0;
for (int i = 0; i < n - 1; i++)
{
lsum += arr[i];
int rsum = sum - lsum;
// If averages of arr[0...i]
// and arr[i+1..n-1] are same.
// To avoid floating point problems
// we compare "lsum*(n-i+1)"
// and "rsum*(i+1)" instead of
// "lsum/(i+1)" and "rsum/(n-i+1)"
if (lsum * (n - i - 1) == rsum * (i + 1))
{
Console.Write("From ({0} {1}) to ({2} {3})\n",
0, i, i + 1, n - 1);
found = true;
}
}
// If no subarrays found
if (found == false)
Console.WriteLine("Subarrays not found");
}
// Driver code
static public void Main ( String []arg)
{
int []arr = {1, 5, 7, 2, 0};
int n = arr.Length;
findSubarrays(arr, n);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Efficient Javascript program for
// dividing array to make
// average equal
function findSubarrays(arr,n)
{
// Find array sum
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
let found = false;
let lsum = 0;
for (let i = 0; i < n - 1; i++)
{
lsum += arr[i];
let rsum = sum - lsum;
// If averages of arr[0...i]
// and arr[i+1..n-1] are same.
// To avoid floating point problems
// we compare "lsum*(n-i+1)"
// and "rsum*(i+1)" instead of
// "lsum/(i+1)" and "rsum/(n-i+1)"
if (lsum * (n - i - 1) == rsum * (i + 1))
{
document.write(
"From (0 "+i+") to ("+(i+1)+" "+(n-1)+")\n"
);
found = true;
}
}
// If no subarrays found
if (found == false)
document.write("Subarrays not found");
}
// Driver code
let arr=[1, 5, 7, 2, 0];
let n = arr.length;
findSubarrays(arr, n);
// This code is contributed by rag2127
</script>
OutputFrom (0 1) to (2 4)
Time complexity : O(n)
Auxiliary Space : O(1)
Similar Reads
Check if array can be divided into two sub-arrays such that their absolute difference is K
Given an array arr[] and an integer K, the task is to find whether the array can be divided into two sub-arrays such that the absolute difference of the sum of the elements of both the sub-arrays is K. Examples: Input: arr[] = {2, 4, 5, 1}, K = 0 Output: Yes {2, 4} and {5, 1} are the two possible su
6 min read
Break an array into maximum number of sub-arrays such that their averages are same
Given an integer array, the task is to divide the array into the maximum number of sub-arrays such that the averages of all subarrays are the 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[
8 min read
Find the sums for which an array can be divided into sub-arrays of equal sum
Given an array of integers arr[], the task is to find all the values for sum such that for a value sum[i] the array can be divided into sub-arrays of sum equal to sum[i]. If array cannot be divided into sub-arrays of equal sum then print -1. Examples: Input: arr[] = {2, 2, 2, 1, 1, 2, 2} Output: 2 4
10 min read
Generate an Array such that Average of triplet exist in given Array
Given an array A[] of length N and your task is to find an array B[] of size N+2, such that Average of (B[i], B[i+1], B[i+2]) equals to A[i] for each i (1 <= i <= N) . Note: First two elements B[] must be zero. Examples:Input: N = 1, A[] = {3} Output: B[] = {0, 0, 9} Explanation: Let us unders
5 min read
Find sub-arrays from given two arrays such that they have equal sum
Given two arrays A[] and B[] of equal sizes i.e. N containing integers from 1 to N. The task is to find sub-arrays from the given arrays such that they have equal sum. Print the indices of such sub-arrays. If no such sub-arrays are possible then print -1.Examples: Input: A[] = {1, 2, 3, 4, 5}, B[] =
15+ min read
Find an Array such that mean of this and given Array together equals to K
Given two integers N, K, and an array arr[] consisting of positive integers. The task is to find any possible array of size N such that the mean of arr[] and the array to be found together is K. Examples: Input: arr[] = {1, 5, 6}, N = 4, K = 3Output: {1, 2, 3, 3}Explanation: Mean of {1, 5, 6} and {1
7 min read
Divide array into two parts with equal sum according to the given constraints
Given an array arr[] of N integers, the task is to select an integer x (which may or may not be present in the array) and remove all of its occurrences from the array and divide the remaining array into two non-empty sub-sets such that: The elements of the first set are strictly smaller than x.The e
11 min read
Divide binary array into three equal parts with same value
Given an array A of length n such that it contains only '0s' and '1s'. The task is to divide the array into THREE different non-empty parts such that all of these parts represent the same binary value(in decimals). If it is possible, return any [i, j] with i+1 < j, such that: A[0], A[1], ..., A[i
15+ min read
Number of ways to choose elements from the array such that their average is K
Given an array arr[] of N integers and an integer K. The task is to find the number of ways to select one or more elements from the array such that the average of the selected integers is equal to the given number K. Examples: Input: arr[] = {7, 9, 8, 9}, K = 8 Output: 5 {8}, {7, 9}, {7, 9}, {7, 8,
11 min read
Find an element in array such that sum of left array is equal to sum of right array
Given, an array of size n. Find an element that divides the array into two sub-arrays with equal sums. Examples: Input: 1 4 2 5 0Output: 2Explanation: If 2 is the partition, subarrays are : [1, 4] and [5] Input: 2 3 4 1 4 5Output: 1Explanation: If 1 is the partition, Subarrays are : [2, 3, 4] and [4
15+ min read