Make all array elements equal by replacing adjacent pairs by their sum
Last Updated :
31 May, 2022
Given an array arr[] consisting of N integers, the task is to replace a minimum number of pairs of adjacent elements by their sum to make all array elements equal. Print the minimum number of such operations required.
Examples:
Input: arr[] = {1, 2, 3}
Output: 1
Explanation: Replace arr[0] and arr[1] by their sum. Therefore, the array modifies to {3, 3}.
After completing the above operations, all the array elements become equal.
Therefore, the number of operations required is 1.
Input: arr[] = {4, 4, 4}
Output: 0
Approach: The given problem can be solved using Prefix Sum Array technique. Follow the steps below to solve the given problem:
- Initialize a variable, say count, to store the maximum count of subarrays that can be obtained for any given sum value.
- Initialize an auxiliary array prefix[], of size N, and store the prefix sum of the given array arr[] in it.
- Traverse the array prefix[] and for each element prefix[i], perform the following steps:
- Check whether the given array arr[] can be divided into subarrays with sum prefix[i] or not. If found to be true, then store the count of such subarrays in a variable, say ans.
- Update the value of count as the maximum of count and ans.
- After completing the above steps, print the value of (N – count) as the minimum number of operations required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minSteps(vector< int > a, int n)
{
vector< int > prefix_sum(n);
prefix_sum[0] = a[0];
for ( int i = 1; i < n; i++)
prefix_sum[i] += prefix_sum[i - 1] + a[i];
int mx = -1;
for ( int subgroupsum :prefix_sum)
{
int sum = 0;
int i = 0;
int grp_count = 0;
while (i < n)
{
sum += a[i];
if (sum == subgroupsum)
{
grp_count += 1;
sum = 0;
}
else if (sum > subgroupsum)
{
grp_count = -1;
break ;
}
i += 1;
}
if (grp_count > mx)
mx = grp_count;
}
return n - mx;
}
int main()
{
vector< int > A = {1, 2, 3, 2, 1, 3};
int N = A.size();
cout << minSteps(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minSteps(ArrayList<Integer> a, int n)
{
int []prefix_sum = new int [n];
prefix_sum[ 0 ] = a.get( 0 );
for ( int i = 1 ; i < n; i++)
prefix_sum[i] += prefix_sum[i - 1 ] + a.get(i);
int mx = - 1 ;
for ( int subgroupsum : prefix_sum)
{
int sum = 0 ;
int i = 0 ;
int grp_count = 0 ;
while (i < n)
{
sum += a.get(i);
if (sum == subgroupsum)
{
grp_count += 1 ;
sum = 0 ;
}
else if (sum > subgroupsum)
{
grp_count = - 1 ;
break ;
}
i += 1 ;
}
if (grp_count > mx)
mx = grp_count;
}
return n - mx;
}
public static void main(String[] args)
{
ArrayList<Integer>A = new ArrayList<Integer>();
A.add( 1 );
A.add( 2 );
A.add( 3 );
A.add( 2 );
A.add( 1 );
A.add( 3 );
int N = A.size();
System.out.print(minSteps(A, N));
}
}
|
Python3
def minSteps(a, n):
prefix_sum = a[:]
for i in range ( 1 , n):
prefix_sum[i] + = prefix_sum[i - 1 ]
mx = - 1
for subgroupsum in prefix_sum:
sum = 0
i = 0
grp_count = 0
while i < n:
sum + = a[i]
if sum = = subgroupsum:
grp_count + = 1
sum = 0
elif sum > subgroupsum:
grp_count = - 1
break
i + = 1
if grp_count > mx:
mx = grp_count
return n - mx
if __name__ = = '__main__' :
A = [ 1 , 2 , 3 , 2 , 1 , 3 ]
N = len (A)
print (minSteps(A, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minSteps(List< int > a, int n)
{
int []prefix_sum = new int [n];
prefix_sum[0] = a[0];
for ( int i = 1; i < n; i++)
prefix_sum[i] += prefix_sum[i - 1] + a[i];
int mx = -1;
foreach ( int subgroupsum in prefix_sum)
{
int sum = 0;
int i = 0;
int grp_count = 0;
while (i < n)
{
sum += a[i];
if (sum == subgroupsum)
{
grp_count += 1;
sum = 0;
}
else if (sum > subgroupsum)
{
grp_count = -1;
break ;
}
i += 1;
}
if (grp_count > mx)
mx = grp_count;
}
return n - mx;
}
public static void Main()
{
List< int > A = new List< int >(){1, 2, 3, 2, 1, 3};
int N = A.Count;
Console.Write(minSteps(A, N));
}
}
|
Javascript
<script>
function minSteps(a, n)
{
var prefix_sum = Array(n).fill(0);
prefix_sum[0] = a[0];
for ( var i = 1; i < n; i++)
prefix_sum[i] += prefix_sum[i - 1] + a[i];
var mx = -1;
for ( var subgroupsum =0;
subgroupsum<prefix_sum.length; subgroupsum++)
{
var sum = 0;
var i = 0;
var grp_count = 0;
while (i < n)
{
sum += a[i];
if (sum == prefix_sum[subgroupsum])
{
grp_count += 1;
sum = 0;
}
else if (sum > prefix_sum[subgroupsum])
{
grp_count = -1;
break ;
}
i += 1;
}
if (grp_count > mx)
mx = grp_count;
}
return n - mx;
}
var A = [1, 2, 3, 2, 1, 3];
var N = A.length;
document.write( minSteps(A, N));
</script>
|
Time Complexity: O(N2), as we are using nested loops to traverse N2 times.
Auxiliary Space: O(N), as we are using extra space for prefix_sum.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...