📌  相关文章
📜  通过重复用第二小的元素替换最大的数组元素来使所有数组元素相等

📅  最后修改于: 2022-05-13 01:56:08.933000             🧑  作者: Mango

通过重复用第二小的元素替换最大的数组元素来使所有数组元素相等

给定一个大小为N的数组arr[] ,任务是计算使所有数组元素相等所需的操作数,方法是用第二大数组元素替换最大数组元素,第二大数组元素严格小于最大数组元素。

例子:

解决方法:按照以下步骤解决问题:

  • 初始化一个变量,比如value_count = 0operation_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)