Find the sums for which an array can be divided into sub-arrays of equal sum
Last Updated :
13 Sep, 2022
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 6
The array can be divided into sub-arrays of sum 2, 4 and 6.
{2}, {2}, {2}, {1, 1}, {2} and {2}
{2, 2}, {2, 1, 1} and {2, 2}
{2, 2, 2} and {1, 1, 2, 2}
Input: arr[] = {1, 1, 2}
Output: 2
The array can be divided into sub-arrays of sum 2.
{1, 1} and {2}
Approach: Make a Prefix Sum Array P[] where P[i] stores the sum of elements from index 0 to i. The divisors of the total sum S can only be the possible sub-array sum. So for every divisor, if all the multiples of the divisor upto total sum S are present in the array P[], then that would be a possible sub-array sum. Mark all the elements of P[] into a Map as 1 so that look-up would be easy. All the divisors can be checked in sqrt(S) time.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getSum( int a[], int n)
{
int P[n];
P[0] = a[0];
for ( int i = 1; i < n; i++)
P[i] = a[i] + P[i - 1];
int S = P[n - 1];
map< int , int > hash;
for ( int i = 0; i < n; i++)
hash[P[i]] = 1;
set< int > res;
for ( int i = 1; i * i <= S; i++) {
if (S % i == 0) {
bool pres = true ;
int div1 = i, div2 = S / i;
for ( int j = div1; j <= S; j += div1) {
if (hash[j] != 1) {
pres = false ;
break ;
}
}
if (pres and div1 != S)
res.insert(div1);
pres = true ;
for ( int j = S / i; j <= S; j += S / i) {
if (hash[j] != 1) {
pres = false ;
break ;
}
}
if (pres and div2 != S)
res.insert(div2);
}
}
if (res.size() == 0) {
cout << "-1" ;
return ;
}
for ( auto i : res)
cout << i << " " ;
}
int main()
{
int a[] = { 1, 2, 1, 1, 1, 2, 1, 3 };
int n = sizeof (a) / sizeof (a[0]);
getSum(a, n);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.HashSet;
class GFG {
public static void getSum( int [] a, int n)
{
int [] P = new int [n];
P[ 0 ] = a[ 0 ];
for ( int i = 1 ; i < n; i++)
P[i] = a[i] + P[i - 1 ];
int S = P[n - 1 ];
HashMap<Integer, Integer> hash = new HashMap<>();
for ( int i = 0 ; i < n; i++)
hash.put(P[i], 1 );
HashSet<Integer> res = new HashSet<>();
for ( int i = 1 ; i * i <= S; i++)
{
if (S % i == 0 )
{
boolean pres = true ;
int div1 = i, div2 = S / i;
for ( int j = div1; j <= S; j += div1)
{
if (hash.get(j) == null || hash.get(j) != 1 )
{
pres = false ;
break ;
}
}
if (pres && div1 != S)
res.add(div1);
pres = true ;
for ( int j = S / i; j <= S; j += S / i)
{
if (hash.get(j) == null || hash.get(j) != 1 )
{
pres = false ;
break ;
}
}
if (pres && div2 != S)
res.add(div2);
}
}
if (res.size() == 0 )
{
System.out.println( "-1" );
return ;
}
for ( int i : res)
System.out.print(i + " " );
}
public static void main(String[] args)
{
int [] a = { 1 , 2 , 1 , 1 , 1 , 2 , 1 , 3 };
int n = a.length;
getSum(a, n);
}
}
|
Python3
from math import sqrt
def getSum(a, n) :
P = [ 0 ] * n
P[ 0 ] = a[ 0 ]
for i in range ( 1 , n) :
P[i] = a[i] + P[i - 1 ]
S = P[n - 1 ]
hash = {}
for i in range (n) :
hash [P[i]] = 1
res = set ()
for i in range ( 1 , int (sqrt(S)) + 1 ) :
if (S % i = = 0 ) :
pres = True ;
div1 = i
div2 = S / / i
for j in range (div1 , S + 1 , div1) :
if j not in hash .keys() :
pres = False
break
if (pres and div1 ! = S) :
res.add(div1)
pres = True
for j in range (S / / i , S + 1 , S / / i) :
if j not in hash .keys():
pres = False ;
break
if (pres and div2 ! = S) :
res.add(div2)
if ( len (res) = = 0 ) :
print ( "-1" )
return
for i in res :
print (i, end = " " )
if __name__ = = "__main__" :
a = [ 1 , 2 , 1 , 1 , 1 , 2 , 1 , 3 ]
n = len (a)
getSum(a, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static void getSum( int [] a, int n)
{
int [] P = new int [n];
P[0] = a[0];
for ( int i = 1; i < n; i++)
P[i] = a[i] + P[i - 1];
int S = P[n - 1];
Dictionary< int ,
int > hash = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
if (!hash.ContainsKey(P[i]))
hash.Add(P[i], 1);
HashSet< int > res = new HashSet< int >();
for ( int i = 1; i * i <= S; i++)
{
if (S % i == 0)
{
Boolean pres = true ;
int div1 = i, div2 = S / i;
for ( int j = div1; j <= S; j += div1)
{
if (!hash.ContainsKey(j) ||
hash[j] != 1)
{
pres = false ;
break ;
}
}
if (pres && div1 != S)
res.Add(div1);
pres = true ;
for ( int j = S / i;
j <= S; j += S / i)
{
if (hash[j] == 0 ||
hash[j] != 1)
{
pres = false ;
break ;
}
}
if (pres && div2 != S)
res.Add(div2);
}
}
if (res.Count == 0)
{
Console.WriteLine( "-1" );
return ;
}
foreach ( int i in res)
Console.Write(i + " " );
}
public static void Main(String[] args)
{
int [] a = { 1, 2, 1, 1, 1, 2, 1, 3 };
int n = a.Length;
getSum(a, n);
}
}
|
Javascript
<script>
function getSum(a, n) {
let P = new Array(n);
P[0] = a[0];
for (let i = 1; i < n; i++)
P[i] = a[i] + P[i - 1];
let S = P[n - 1];
let hash = new Map();
for (let i = 0; i < n; i++)
hash.set(P[i], 1);
let res = new Set();
for (let i = 1; i * i <= S; i++) {
if (S % i == 0) {
let pres = true ;
let div1 = i, div2 = Math.floor(S / i);
for (let j = div1; j <= S; j += div1) {
if (hash.get(j) != 1) {
pres = false ;
break ;
}
}
if (pres && div1 != S)
res.add(div1);
pres = true ;
for (let j = Math.floor(S / i); j <= S; j += Math.floor(S / i)) {
if (hash.get(j) != 1) {
pres = false ;
break ;
}
}
if (pres && div2 != S)
res.add(div2);
}
}
if (res.size == 0) {
document.write( "-1" );
return ;
}
res = [...res].sort((a, b) => a - b)
for (let i of res)
document.write(i + " " );
}
let a = [1, 2, 1, 1, 1, 2, 1, 3];
let n = a.length;
getSum(a, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(nlogn), to check for divisors and multiples
- Auxiliary Space: O(n), as extra space of size n is used
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...