Minimum count of array elements that must be changed such that difference between maximum and minimum array element is N – 1
Given an array arr[] consisting of N integers, the task is to find the minimum number of array elements that must be changed to any arbitrary integers such that the difference between maximum and minimum array element is (N – 1) and all array elements must be distinct.
Examples:
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
Approach: The given problem can be solved by using the Sorting and Sliding Window Technique. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations(vector< int >& A)
{
int N = A.size();
int ans = N;
int j = 0;
sort(begin(A), end(A));
A.erase(unique(begin(A), end(A)),
end(A));
int M = A.size();
for ( int i = 0; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1) {
++j;
}
ans = min(ans, N - j + i);
}
return ans;
}
int main()
{
vector< int > arr = { 1, 10, 100, 1000 };
cout << minOperations(arr);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.*;
class GFG {
public static int minOperations( int [] A) {
int N = A.length;
int ans = N;
int j = 0 ;
Arrays.sort(A);
removeDups(A);
int M = A.length;
for ( int i = 0 ; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1 ) {
++j;
}
ans = Math.min(ans, N - j + i);
}
return ans;
}
public static void removeDups( int [] a) {
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
for ( int i = 0 ; i < a.length; i++)
set.add(a[i]);
}
public static void main(String args[]) {
int [] arr = { 1 , 10 , 100 , 1000 };
System.out.println(minOperations(arr));
}
}
|
Python3
def minOperations(A):
N = len (A)
ans = N
j = 0
A.sort()
A = list ( set (A))
A.sort()
M = len (A)
for i in range (M):
while (j < M and A[j] < = A[i] + N - 1 ):
j + = 1
ans = min (ans, N - j + i)
return ans
if __name__ = = '__main__' :
arr = [ 1 , 10 , 100 , 1000 ]
print (minOperations(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG {
public static int minOperations( int [] A) {
int N = A.Length;
int ans = N;
int j = 0;
Array.Sort(A);
removeDups(A);
int M = A.Length;
for ( int i = 0; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1) {
++j;
}
ans = Math.Min(ans, N - j + i);
}
return ans;
}
public static void removeDups( int [] a) {
HashSet< int > set = new HashSet< int >();
for ( int i = 0; i < a.Length; i++)
set .Add(a[i]);
}
public static void Main() {
int [] arr = { 1, 10, 100, 1000 };
Console.Write(minOperations(arr));
}
}
|
Javascript
<script>
function minOperations(A)
{
let N = A.length;
let ans = N;
let j = 0;
A.sort( function (a, b) { return a - b });
let unique_A = [];
for (let i = 0; i < A.length - 1; i++) {
if (A[i] != A[i + 1]) {
unique_A.push(A[i])
}
}
A = unique_A;
let M = A.length;
for (let i = 0; i < M; ++i) {
while (j < M && A[j] <= A[i] + N - 1) {
++j;
}
ans = Math.min(ans, N - j + i);
}
return ans;
}
let arr = [1, 10, 100, 1000];
document.write(minOperations(arr));
</script>
|
Time Complexity: O(N*log N), The time complexity of the given program is O(NlogN), where N is the size of the input vector A. This is because the program sorts the input vector which takes O(NlogN) time, and then iterates over it once, performing constant time operations at each iteration.
Auxiliary Space: O(1)
Last Updated :
18 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...