给定一个由N 个整数组成的数组arr[] ,任务是通过将数组最左边的最大元素替换为严格小于当前最大最小次数的最大元素来使所有数组元素相等。
例子:
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
方法:根据观察到的每一步,一个数组元素将替换所有大于当前数组元素的元素,通过对数组进行降序排序来解决该问题。请按照以下步骤解决问题:
- 按降序对数组进行排序。
- 初始化一个变量,比如count为0 ,以计算所需的移动总数。
- 使用变量i迭代范围[1, N – 1]并执行以下步骤:
- 如果arr[i] != arr[i – 1] ,则将count增加i 。
- 最后,在完成上述步骤后,在计数作为答案获得的打印值。
下面是上述方法的实现:
C++
// C++ program for above approach
#include
using namespace std;
// Function to count minimum number of
// times the leftmost larger element is
// required to be replaced to make
// all array elements equal
int reductionOperations(int arr[], int N)
{
// Sort the array in descending order
sort(arr, arr + N, greater());
// Stores minimum number of moves required
int count = 0;
// Traverse the array
for (int i = 1; i < N; i++) {
// If arr[i - 1] is not equal to arr[i]
if (arr[i - 1] != arr[i]) {
// Update count
count += i;
}
}
// Return count
return count;
}
// Driver code
int main()
{
// Given input
int arr[] = { 1, 1, 2, 2, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << reductionOperations(arr, N);
}
Java
// Java Program for the above approach
import java.io.*;
import java.util.Arrays;
class GFG {
public static int reductionOperations(int arr[], int N)
{
// Sort the array in descending order
Arrays.sort(arr);
reverse(arr);
// Stores minimum number of moves required
int count = 0;
// Traverse the array
for (int i = 1; i < N; i++) {
// If arr[i - 1] is not equal to arr[i]
if (arr[i - 1] != arr[i]) {
// Update count
count += i;
}
}
// Return count
return count;
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void main(String[] args)
{
// Given input
int arr[] = { 1, 1, 2, 2, 3 };
int N = arr.length;
// Function Call
System.out.println(reductionOperations(arr, N));
}
}
// This code is contributed by Potta Lokesh
Python3
# python 3 program for above approach
# Function to count minimum number of
# times the leftmost larger element is
# required to be replaced to make
# all array elements equal
def reductionOperations(arr, N):
# Sort the array in descending order
arr.sort(reverse=True)
# Stores minimum number of moves required
count = 0
# Traverse the array
for i in range(1, N, 1):
# If arr[i - 1] is not equal to arr[i]
if (arr[i - 1] != arr[i]):
# Update count
count += i
# Return count
return count
# Driver code
if __name__ == '__main__':
# Given input
arr = [1, 1, 2, 2, 3]
N = len(arr)
# Function Call
print(reductionOperations(arr, N))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
static int reductionOperations(int[] arr, int N)
{
// Sort the array in descending order
Array.Sort(arr);
reverse(arr);
// Stores minimum number of moves required
int count = 0;
// Traverse the array
for(int i = 1; i < N; i++)
{
// If arr[i - 1] is not equal to arr[i]
if (arr[i - 1] != arr[i])
{
// Update count
count += i;
}
}
// Return count
return count;
}
static void reverse(int[] array)
{
// Length of the array
int n = array.Length;
// Swaping the first half elements
// with last half elements
for(int i = 0; i < n / 2; i++)
{
// Storing the first half
// elements temporarily
int temp = array[i];
// Assigning the first half
// to the last half
array[i] = array[n - i - 1];
// Assigning the last half
// to the first half
array[n - i - 1] = temp;
}
}
// Driver code
public static void Main()
{
// Given input
int[] arr = { 1, 1, 2, 2, 3 };
int N = arr.Length;
// Function Call
Console.Write(reductionOperations(arr, N));
}
}
// This code is contributed by subham348
Javascript
输出
4
时间复杂度: O(N * log(N))
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。