通过重复用第二小的元素替换最大的数组元素来使所有数组元素相等
给定一个大小为N的数组arr[] ,任务是计算使所有数组元素相等所需的操作数,方法是用第二大数组元素替换最大数组元素,第二大数组元素严格小于最大数组元素。
例子:
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
解决方法:按照以下步骤解决问题:
- 初始化一个变量,比如value_count = 0和operation_count = 0 。
- 按升序对数组arr[]进行排序。
- 遍历数组arr[]并检查当前元素是否大于前一个元素。如果发现为 true ,则将value_count增加1 。
- 对于每次迭代,将value_count添加到operation_count中。
- 最后,打印operation_count的值。
下面是上述方法的实现:
C++
// C++ program to Make all array elements
// equal by perform certain operation
#include
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.
4
时间复杂度: O(NLogN)
辅助空间: O(1)