Make all array elements equal by repeatedly replacing largest array element with the second smallest element
Last Updated :
09 Aug, 2021
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++
// C++ program to Make all array elements
// equal by perform certain operation
#include <bits/stdc++.h>
using namespace std;
// Function to count number of operations
// required to make all array elements equal
int operation(int arr[], int n)
{
// Initialize the val_count
// and operation_count by 0.
int val_count = 0, operation_count = 0;
// Sort the array in ascending order.
sort(arr, arr + n);
for (int i = 1; i < n; i++) {
// Current element greater
// than the previous element
if (arr[i - 1] < arr[i]) {
// If yes then update the
// val_count by 1.
val_count++;
}
// Add the value_count in operation_count.
operation_count = operation_count + val_count;
}
// Return the operation_count
return operation_count;
}
// Driver Code
int main()
{
// Given Input
int arr[] = { 1, 1, 2, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << operation(arr, n);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
import java.io.*;
class GFG
{
// Function to count number of operations
// required to make all array elements equal
static int operation(int arr[], int n)
{
// Initialize the val_count
// and operation_count by 0.
int val_count = 0, operation_count = 0;
// Sort the array in ascending order.
Arrays.sort(arr);
for (int i = 1; i < n; i++) {
// Current element greater
// than the previous element
if (arr[i - 1] < arr[i]) {
// If yes then update the
// val_count by 1.
val_count++;
}
// Add the value_count in operation_count.
operation_count = operation_count + val_count;
}
// Return the operation_count
return operation_count;
}
// Driver Code
public static void main (String[] args)
{
// Given Input
int arr[] = { 1, 1, 2, 2, 3 };
int n = arr.length;
// Function Call
System.out.println( operation(arr, n));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program to Make all array elements
# equal by perform certain operation
# Function to count number of operations
# required to make all array elements equal
def operation(arr, n):
# Initialize the val_count
# and operation_count by 0.
val_count = 0
operation_count = 0
# Sort the array in ascending order.
arr.sort()
for i in range(1, n):
# Current element greater
# than the previous element
if arr[i-1] < arr[i]:
# If yes then update the
# val_count by 1.
val_count += 1
# Add the value_count in operation_count.
operation_count += val_count
# Return the operation_count
return operation_count
# Driver code
arr = [1, 1, 2, 2, 3]
n = len(arr)
print(operation(arr, n))
# This code is contributed by Parth Manchanda
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count number of operations
// required to make all array elements equal
static int operation(int []arr, int n)
{
// Initialize the val_count
// and operation_count by 0.
int val_count = 0, operation_count = 0;
// Sort the array in ascending order.
Array.Sort(arr);
for (int i = 1; i < n; i++) {
// Current element greater
// than the previous element
if (arr[i - 1] < arr[i]) {
// If yes then update the
// val_count by 1.
val_count++;
}
// Add the value_count in operation_count.
operation_count = operation_count + val_count;
}
// Return the operation_count
return operation_count;
}
// Driver Code
public static void Main(String[] args)
{
// Given Input
int []arr = { 1, 1, 2, 2, 3 };
int n = arr.Length;
// Function Call
Console.WriteLine( operation(arr, n));
}
}
// This code is contributed by Amit Katiyar
JavaScript
// Javascript program to Make all array elements
// equal by perform certain operation
// Function to count number of operations
// required to make all array elements equal
function operation(arr, n) {
// Initialize the val_count
// and operation_count by 0.
let val_count = 0,
operation_count = 0;
// Sort the array in ascending order.
arr.sort();
for (let i = 1; i < n; i++) {
// Current element greater
// than the previous element
if (arr[i - 1] < arr[i]) {
// If yes then update the
// val_count by 1.
val_count++;
}
// Add the value_count in operation_count.
operation_count = operation_count + val_count;
}
// Return the operation_count
return operation_count;
}
// Driver Code
// Given Input
let arr = [1, 1, 2, 2, 3];
let n = arr.length;
// Function Call
document.write(operation(arr, n));
// This code is contributed by gfgking.
Time Complexity: O(NLogN)
Auxiliary Space: O(1)
Similar Reads
Count decrements to nearest smaller element required to make all array elements equal Given an array arr[] consisting of N non-negative integers, the task is to find the number of operations required to make all array elements equal. In each operation, any array element can be changed to its nearest smaller array element. Examples: Input: arr[] = {2, 5, 4, 3, 5, 4}Output: 11Explanati
7 min read
Replace every element with the smallest of all other array elements Given an array arr[] which consist of N integers, the task is to replace every element by the smallest of all other elements present in the array. Examples: Input: arr[] = {1, 1, 1, 2} Output: 1 1 1 1 Input: arr[] = {4, 2, 1, 3} Output: 1 1 2 1 Naive Approach: The simplest approach is to find the sm
12 min read
Rearrange given Array by replacing every element with the element located at mean of adjacent elements Given an array arr[]. The array contains numbers from 0 to N-1 only, where N is the size of arr[]. The task is to modify the array in such that for each i from 0â¤i<N, arr[i] = arr[ (arr[i-1] + arr[i+1]) / 2 ] There are a few exceptions: For first element of the array, arr[i-1] = 0; because previo
9 min read
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: 4Explana
6 min read
Make all Array elements equal by replacing it with adjacent elements Given an array A[] of size N, the task is to find the minimum number of moves required to make array elements equal where you can make adjacent elements equal in one move. Examples: Input: A = {1, 6, 5, 1, 7, 1}, N = 6Output: 3Explanation: Replace 6 with 1, 5 with 1, and then at last replace 7 with
5 min read
Make all array elements equal by reducing array elements to half minimum number of times Given an array arr[] consisting of N integers, the task is to minimize the number of operations required to make all array elements equal by converting Ai to Ai / 2. in each operation Examples: Input: arr[] = {3, 1, 1, 3}Output: 2Explanation: Reducing A0 to A0 / 2 modifies arr[] to {1, 1, 1, 3}. Red
6 min read