Minimize replacements of leftmost largest array element required to make all array elements equal
Given an array arr[] consisting of N integers, the task is to make all array elements equal by replacing the leftmost largest element of the array with the largest element strictly smaller than the current maximum minimum number of times.
Examples:
Input: arr[] = { 1, 1, 2, 2, 3<}
Output: 4
Explanation: Following operations reduces the required number of operations to minimum:
- Leftmost largest array element is arr[4]( = 3). Replacing it with the largest element smaller than the current largest, arr[3] (= 2), the array modifies to {1, 1, 2, 2, 2}.
- Leftmost largest element of the array is arr[2]( = 2). Replacing it with the largest element smaller than the current largest, arr[1] (= 1), the array modifies to {1, 1, 1, 2, 2}.
- Leftmost largest element of the array is arr[3]( = 2), replacing it with the largest element smaller than the current largest, arr[1] (= 1), the array modifies to {1, 1, 1, 1, 2}.
- Leftmost largest element of the array is arr[4]( = 2). Replacing it with the largest element smaller than the current largest, arr[1] (=1), the array modifies to {1, 1, 1, 1, 1}
Therefore, a minimum of 4 moves are needed.
Input: arr[] = {5, 1, 3}
Output: 3
Approach: The problem can be solved by sort the array in descending order based on the observations that at each step, an array element will replace all the elements that are larger than the current array element. Follow the steps to solve the problem:
- Sort the array in descending order.
- Initialize a variable, say count as 0, to count the total number of moves required.
- Iterate over the range [1, N – 1] using a variable i and perform the following steps:
- If arr[i] != arr[i – 1], then increment count by i.
- Finally, after completing the above steps, the print value obtained in count as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reductionOperations( int arr[], int N)
{
sort(arr, arr + N, greater< int >());
int count = 0;
for ( int i = 1; i < N; i++) {
if (arr[i - 1] != arr[i]) {
count += i;
}
}
return count;
}
int main()
{
int arr[] = { 1, 1, 2, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << reductionOperations(arr, N);
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static int reductionOperations( int arr[], int N)
{
Arrays.sort(arr);
reverse(arr);
int count = 0 ;
for ( int i = 1 ; i < N; i++) {
if (arr[i - 1 ] != arr[i]) {
count += i;
}
}
return count;
}
public static void reverse( int [] array)
{
int n = array.length;
for ( int i = 0 ; i < n / 2 ; i++) {
int temp = array[i];
array[i] = array[n - i - 1 ];
array[n - i - 1 ] = temp;
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 2 , 2 , 3 };
int N = arr.length;
System.out.println(reductionOperations(arr, N));
}
}
|
Python3
def reductionOperations(arr, N):
arr.sort(reverse = True )
count = 0
for i in range ( 1 , N, 1 ):
if (arr[i - 1 ] ! = arr[i]):
count + = i
return count
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 2 , 3 ]
N = len (arr)
print (reductionOperations(arr, N))
|
C#
using System;
class GFG{
static int reductionOperations( int [] arr, int N)
{
Array.Sort(arr);
reverse(arr);
int count = 0;
for ( int i = 1; i < N; i++)
{
if (arr[i - 1] != arr[i])
{
count += i;
}
}
return count;
}
static void reverse( int [] array)
{
int n = array.Length;
for ( int i = 0; i < n / 2; i++)
{
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
public static void Main()
{
int [] arr = { 1, 1, 2, 2, 3 };
int N = arr.Length;
Console.Write(reductionOperations(arr, N));
}
}
|
Javascript
<script>
function reductionOperations(arr, N)
{
arr.sort( function (a, b) { return b - a });
let count = 0;
for (let i = 1; i < N; i++) {
if (arr[i - 1] != arr[i]) {
count += i;
}
}
return count;
}
let arr = [1, 1, 2, 2, 3];
let N = arr.length;
document.write(reductionOperations(arr, N));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Last Updated :
27 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...