Given an array arr[] of size N, the task is to count the number of operations required to make all array elements equal by replacing the largest array element with the second-largest array element, which is strictly smaller than the largest array element.
Examples:
Input: arr[ ] = {1, 1, 2, 2, 3}
Output: 4
Explanation: A total of 4 operations are required to make all array elements equal.
Operation 1: Replace the largest element (= arr[4] = 3) with the next largest( = arr[2] = 2). The array arr[] modifies to {1, 1, 2, 2, 2}.
Operation 2: Replace the largest element (= arr[2] = 2) with the next largest( = arr[0] = 1). The array arr[] modifies to {1, 1, 1, 2, 2}
Operation 3: Replace the largest element (= arr[3] = 2) with the next largest( = arr[0] = 1). The array arr[] modifies to {1, 1, 1, 1, 2}
Operation 4: Replace the largest element (= arr[4] = 2) with the next largest( = arr[0] = 1). The array arr[] modifies to {1, 1, 1, 1, 1}
Input: arr[ ] = {1, 1, 1}
Output: 0
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say value_count = 0 and operation_count = 0.
- Sort the array arr[] in ascending order.
- Traverse the array arr[] and check if the current element is greater than the previous element. If found to be true, then increase value_count by 1.
- For each iteration, add value_count in operation_count.
- Finally, print the value of operation_count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int operation( int arr[], int n)
{
int val_count = 0, operation_count = 0;
sort(arr, arr + n);
for ( int i = 1; i < n; i++) {
if (arr[i - 1] < arr[i]) {
val_count++;
}
operation_count = operation_count + val_count;
}
return operation_count;
}
int main()
{
int arr[] = { 1, 1, 2, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << operation(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG
{
static int operation( int arr[], int n)
{
int val_count = 0 , operation_count = 0 ;
Arrays.sort(arr);
for ( int i = 1 ; i < n; i++) {
if (arr[i - 1 ] < arr[i]) {
val_count++;
}
operation_count = operation_count + val_count;
}
return operation_count;
}
public static void main (String[] args)
{
int arr[] = { 1 , 1 , 2 , 2 , 3 };
int n = arr.length;
System.out.println( operation(arr, n));
}
}
|
Python3
def operation(arr, n):
val_count = 0
operation_count = 0
arr.sort()
for i in range ( 1 , n):
if arr[i - 1 ] < arr[i]:
val_count + = 1
operation_count + = val_count
return operation_count
arr = [ 1 , 1 , 2 , 2 , 3 ]
n = len (arr)
print (operation(arr, n))
|
C#
using System;
public class GFG
{
static int operation( int []arr, int n)
{
int val_count = 0, operation_count = 0;
Array.Sort(arr);
for ( int i = 1; i < n; i++) {
if (arr[i - 1] < arr[i]) {
val_count++;
}
operation_count = operation_count + val_count;
}
return operation_count;
}
public static void Main(String[] args)
{
int []arr = { 1, 1, 2, 2, 3 };
int n = arr.Length;
Console.WriteLine( operation(arr, n));
}
}
|
Javascript
function operation(arr, n) {
let val_count = 0,
operation_count = 0;
arr.sort();
for (let i = 1; i < n; i++) {
if (arr[i - 1] < arr[i]) {
val_count++;
}
operation_count = operation_count + val_count;
}
return operation_count;
}
let arr = [1, 1, 2, 2, 3];
let n = arr.length;
document.write(operation(arr, n));
|
Time Complexity: O(NLogN)
Auxiliary Space: O(1)