Make the array elements equal by performing given operations minimum number of times
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++
// C++ Program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate the minimum // operations of given type required // to make the array elements equal void minOperation( int a[], int N) { // Stores the total count of operations int totOps = 0; // Traverse the array for ( int i = 0; i < N - 1; i++) { // Update difference between // pairs of adjacent elements totOps += abs (a[i] - a[i + 1]); } // Store the maximum count of operations int maxOps = max( abs (a[0] - a[1]), abs (a[N - 1] - a[N - 2])); for ( int i = 1; i < N - 1; i++) { // Rest of the elements maxOps = max(maxOps, abs (a[i] - a[i - 1]) + abs (a[i] - a[i + 1]) - abs (a[i - 1] - a[i + 1])); } // Total Operation - Maximum Operation cout << totOps - maxOps << endl; } // Driver Code int main() { // Given array int arr[] = { 1, -1, 0, 1, 1 }; // Size of the array int N = sizeof (arr) / sizeof (arr[0]); minOperation(arr, N); return 0; } |
Java
// Java Program for the above approach import java.io.*; class GFG { // Function to calculate the minimum // operations of given type required // to make the array elements equal static void minOperation( int a[], int N) { // Stores the total count of operations int totOps = 0 ; // Traverse the array for ( int i = 0 ; i < N - 1 ; i++) { // Update difference between // pairs of adjacent elements totOps += Math.abs(a[i] - a[i + 1 ]); } // Store the maximum count of operations 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++) { // Rest of the elements 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 ])); } // Total Operation - Maximum Operation System.out.println(totOps - maxOps); } // Driver Code public static void main(String[] args) { // Given array int [] arr = { 1 , - 1 , 0 , 1 , 1 }; // Size of the array int N = arr.length; minOperation(arr, N); } } // This code is contributed by Dharanendra L V |
Python3
# Python3 Program for the above approach # Function to calculate the minimum # operations of given type required # to make the array elements equal def minOperation(a, N): # Stores the total count of operations totOps = 0 # Traverse the array for i in range (N - 1 ): # Update difference between # pairs of adjacent elements totOps + = abs (a[i] - a[i + 1 ]) # Store the maximum count of operations maxOps = max ( abs (a[ 0 ] - a[ 1 ]), abs (a[N - 1 ] - a[N - 2 ])) for i in range ( 1 , N - 1 ): # Rest of the elements maxOps = max (maxOps, abs (a[i] - a[i - 1 ]) + abs (a[i] - a[i + 1 ]) - abs (a[i - 1 ] - a[i + 1 ])) # Total Operation - Maximum Operation print (totOps - maxOps) # Driver Code if __name__ = = '__main__' : # Given array arr = [ 1 , - 1 , 0 , 1 , 1 ] # Size of the array N = len (arr) minOperation(arr, N) # This code is contributed by mohit kumar 29. |
C#
// C# Program for the above approach using System; public class GFG { // Function to calculate the minimum // operations of given type required // to make the array elements equal static void minOperation( int [] a, int N) { // Stores the total count of operations int totOps = 0; // Traverse the array for ( int i = 0; i < N - 1; i++) { // Update difference between // pairs of adjacent elements totOps += Math.Abs(a[i] - a[i + 1]); } // Store the maximum count of operations 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++) { // Rest of the elements 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])); } // Total Operation - Maximum Operation Console.WriteLine(totOps - maxOps); } // Driver Code static public void Main() { // Given array int [] arr = { 1, -1, 0, 1, 1 }; // Size of the array int N = arr.Length; minOperation(arr, N); } } // This code is contributed by Dharanendra L V |
Javascript
<script> // javascript Program for the above approach // Function to calculate the minimum // operations of given type required // to make the array elements equal function minOperation(a , N) { // Stores the total count of operations var totOps = 0; // Traverse the array for (i = 0; i < N - 1; i++) { // Update difference between // pairs of adjacent elements totOps += Math.abs(a[i] - a[i + 1]); } // Store the maximum count of operations 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++) { // Rest of the elements 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])); } // Total Operation - Maximum Operation document.write(totOps - maxOps); } // Driver Code // Given array var arr = [ 1, -1, 0, 1, 1 ]; // Size of the array var N = arr.length; minOperation(arr, N); // This code contributed by aashish1995 </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...