Make the array elements equal by performing given operations minimum number of times
Last Updated :
21 Apr, 2021
Given an array arr[] of size N, the task is to make all the array elements equal by performing following operations minimum number of times:
- Increase all array elements of any suffix array by 1.
- Decrease all the elements of any suffix array by 1.
- Replace any array element y another.
Examples:
Input: arr[] = {99, 96, 97, 95}
Output: 3
Explanation:
Operation 1: Replace a1 by a2, i.e. 99 ? 96. The array arr[] modifies to {96, 96, 97, 95}.
Operation 2: Increment the suffix { a4} by 2, i.e., 95 ? 97. The array arr[] modifies to {96, 96, 97, 97}.
Operation 3: Decrement the suffix { a3 } by 1, i.e., 97 ? 96. The array arr[] modifies to {96, 96, 96, 96}.
Hence, the total number of operations required is 3.
Input: arr[] = {1, -1, 0, 1, 1}
Output: 2
Approach: The idea is to find the difference between the actual sum and the sum of the array having all elements equal and then choose the operations to perform such that it leads to the minimum count of operations. Follow the steps below to solve the problem:
- Initialize a variable, say totOps, to store the actual operations needed to make all the array elements equal.
- Traverse the array and store the difference between all pair of consecutive elements and store their sum in totOps.
- Initialize a variable, say maxOps, to store the maximum count of operations required.
- Now, find the maximum change that occurs while changing an element and store it in the maxOps variable. There are three cases:
- For the 1st element i.e. arr[1], the optimal way to change arr[1] is to make it arr[2].
- For the last element i.e. arr[N], the optimal way to change arr[N] is to make it into arr[N-1].
- For the rest of the element, changing arr[i] affects both arr[i-1] and arr[i+1], therefore, the maximum change is abs(arr[i] – arr[i+1]) + abs(arr[i] – arr[i-1]) – abs(arr[i-1] – arr[i+1).
- Therefore, the minimum operations required is equal to the difference between totOps and maxOps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperation( int a[], int N)
{
int totOps = 0;
for ( int i = 0; i < N - 1; i++) {
totOps += abs (a[i] - a[i + 1]);
}
int maxOps
= max( abs (a[0] - a[1]),
abs (a[N - 1] - a[N - 2]));
for ( int i = 1; i < N - 1; i++) {
maxOps
= max(maxOps, abs (a[i] - a[i - 1])
+ abs (a[i] - a[i + 1])
- abs (a[i - 1] - a[i + 1]));
}
cout << totOps - maxOps << endl;
}
int main()
{
int arr[] = { 1, -1, 0, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
minOperation(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void minOperation( int a[], int N)
{
int totOps = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
totOps += Math.abs(a[i] - a[i + 1 ]);
}
int maxOps
= Math.max(Math.abs(a[ 0 ] - a[ 1 ]),
Math.abs(a[N - 1 ] - a[N - 2 ]));
for ( int i = 1 ; i < N - 1 ; i++)
{
maxOps = Math.max(
maxOps,
Math.abs(a[i] - a[i - 1 ])
+ Math.abs(a[i] - a[i + 1 ])
- Math.abs(a[i - 1 ] - a[i + 1 ]));
}
System.out.println(totOps - maxOps);
}
public static void main(String[] args)
{
int [] arr = { 1 , - 1 , 0 , 1 , 1 };
int N = arr.length;
minOperation(arr, N);
}
}
|
Python3
def minOperation(a, N):
totOps = 0
for i in range (N - 1 ):
totOps + = abs (a[i] - a[i + 1 ])
maxOps = max ( abs (a[ 0 ] - a[ 1 ]), abs (a[N - 1 ] - a[N - 2 ]))
for i in range ( 1 , N - 1 ):
maxOps = max (maxOps, abs (a[i] - a[i - 1 ]) +
abs (a[i] - a[i + 1 ]) - abs (a[i - 1 ] - a[i + 1 ]))
print (totOps - maxOps)
if __name__ = = '__main__' :
arr = [ 1 , - 1 , 0 , 1 , 1 ]
N = len (arr)
minOperation(arr, N)
|
C#
using System;
public class GFG {
static void minOperation( int [] a, int N)
{
int totOps = 0;
for ( int i = 0; i < N - 1; i++)
{
totOps += Math.Abs(a[i] - a[i + 1]);
}
int maxOps
= Math.Max(Math.Abs(a[0] - a[1]),
Math.Abs(a[N - 1] - a[N - 2]));
for ( int i = 1; i < N - 1; i++)
{
maxOps = Math.Max(
maxOps,
Math.Abs(a[i] - a[i - 1])
+ Math.Abs(a[i] - a[i + 1])
- Math.Abs(a[i - 1] - a[i + 1]));
}
Console.WriteLine(totOps - maxOps);
}
static public void Main()
{
int [] arr = { 1, -1, 0, 1, 1 };
int N = arr.Length;
minOperation(arr, N);
}
}
|
Javascript
<script>
function minOperation(a , N) {
var totOps = 0;
for (i = 0; i < N - 1; i++) {
totOps += Math.abs(a[i] - a[i + 1]);
}
var maxOps = Math.max(Math.abs(a[0] - a[1]), Math.abs(a[N - 1] - a[N - 2]));
for (i = 1; i < N - 1; i++) {
maxOps = Math.max(maxOps,
Math.abs(a[i] - a[i - 1]) + Math.abs(a[i] - a[i + 1]) - Math.abs(a[i - 1] - a[i + 1]));
}
document.write(totOps - maxOps);
}
var arr = [ 1, -1, 0, 1, 1 ];
var N = arr.length;
minOperation(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...