必须更改的数组元素的最小计数,使得最大和最小数组元素之间的差为 N – 1
给定一个由N个整数组成的数组arr[] ,任务是找到必须更改为任意整数的最小数组元素数,使得最大和最小数组元素之间的差为(N – 1)并且所有数组元素必须是不同的。
例子:
Input: arr[] = {1, 2, 3, 5, 6}
Output: 1
Explanation:
Change 6->4, the final array will be {1, 2, 3, 5, 4} and the difference is equal to 5 – 1 = 4.
Input: arr[] = {1, 10, 100, 1000}
Output: 3
方法:给定的问题可以通过使用排序和滑动窗口技术来解决。请按照以下步骤解决问题:
- 以非降序对数组进行排序。
- 维护一个变量ans并用值N对其进行初始化,该值将存储最小可能的答案。
- 从给定数组arr[]中删除所有重复元素。
- 迭代范围[0, M) ,其中M是使用变量i删除重复项后数组的新大小,并执行以下任务:
- 在 while 循环中遍历直到j小于M并且A[j]小于等于A[i] + N – 1并将j的值增加1 。
- 将ans 的值更新为 ans和(N – j + i)的最小值。
- 执行上述步骤后,打印ans的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum changes
// needed to make difference of maximum
// and minimum element as (N - 1)
int minOperations(vector& A)
{
int N = A.size();
// Stores the resultant count
int ans = N;
// Maintain a pointer j that will
// denote the rightmost position of
// the valid array
int j = 0;
// Sort the array
sort(begin(A), end(A));
// Only keep unique elements
A.erase(unique(begin(A), end(A)),
end(A));
// Store the new size of the array
// after removing duplicates
int M = A.size();
// IterM;ate over the range
for (int i = 0; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1) {
++j;
}
// Check minimum over this
// starting point
ans = min(ans, N - j + i);
// The length of this subarray
// is `j - i`. Replace `N - j + i`
// elements to make it good
}
return ans;
}
// Driver Code
int main()
{
vector arr = { 1, 10, 100, 1000 };
cout << minOperations(arr);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
import java.util.*;
class GFG {
// Function to find the minimum changes
// needed to make difference of maximum
// and minimum element as (N - 1)
public static int minOperations(int[] A) {
int N = A.length;
// Stores the resultant count
int ans = N;
// Maintain a pointer j that will
// denote the rightmost position of
// the valid array
int j = 0;
// Sort the array
Arrays.sort(A);
// Only keep unique elements
removeDups(A);
// Store the new size of the array
// after removing duplicates
int M = A.length;
// IterM;ate over the range
for (int i = 0; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1) {
++j;
}
// Check minimum over this
// starting point
ans = Math.min(ans, N - j + i);
// The length of this subarray
// is `j - i`. Replace `N - j + i`
// elements to make it good
}
return ans;
}
public static void removeDups(int[] a) {
LinkedHashSet set = new LinkedHashSet();
// adding elements to LinkedHashSet
for (int i = 0; i < a.length; i++)
set.add(a[i]);
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 1, 10, 100, 1000 };
System.out.println(minOperations(arr));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python 3 program for the above approach
# Function to find the minimum changes
# needed to make difference of maximum
# and minimum element as (N - 1)
def minOperations(A):
N = len(A)
# Stores the resultant count
ans = N
# Maintain a pointer j that will
# denote the rightmost position of
# the valid array
j = 0
# Sort the array
A.sort()
# Only keep unique elements
A = list(set(A))
# Store the new size of the array
# after removing duplicates
A.sort()
M = len(A)
# IterM;ate over the range
for i in range(M):
while (j < M and A[j] <= A[i] + N - 1):
j += 1
# Check minimum over this
# starting point
ans = min(ans, N - j + i)
# The length of this subarray
# is `j - i`. Replace `N - j + i`
# elements to make it good
return ans
# Driver Code
if __name__ == '__main__':
arr = [1, 10, 100, 1000]
print(minOperations(arr))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the minimum changes
// needed to make difference of maximum
// and minimum element as (N - 1)
public static int minOperations(int[] A) {
int N = A.Length;
// Stores the resultant count
int ans = N;
// Maintain a pointer j that will
// denote the rightmost position of
// the valid array
int j = 0;
// Sort the array
Array.Sort(A);
// Only keep unique elements
removeDups(A);
// Store the new size of the array
// after removing duplicates
int M = A.Length;
// IterM;ate over the range
for (int i = 0; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1) {
++j;
}
// Check minimum over this
// starting point
ans = Math.Min(ans, N - j + i);
// The length of this subarray
// is `j - i`. Replace `N - j + i`
// elements to make it good
}
return ans;
}
public static void removeDups(int[] a) {
HashSet set = new HashSet();
// adding elements to LinkedHashSet
for (int i = 0; i < a.Length; i++)
set.Add(a[i]);
}
// Driver Code
public static void Main() {
int[] arr = { 1, 10, 100, 1000 };
Console.Write(minOperations(arr));
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
输出:
3
时间复杂度: O(N*log N)
辅助空间: O(1)