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:
- Sort the array in non-decreasing order.
- Maintain a variable ans and initialized it with value N that will store the minimum possible answer.
- Remove all the duplicate elements from the given array arr[].
-
Iterate over the range [0, M) where M is the new size of the array after removing duplicates using the variable i and perform the following tasks:
- Traverse in a while loop till j is less than M and A[j] is less than equal to A[i] + N – 1 and increase the value of j by 1.
- Update the value of ans to the minimum of ans and (N – j + i).
- After performing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the minimum changes // needed to make difference of maximum // and minimum element as (N - 1) int minOperations(vector< int >& 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();
// Iterate 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< int > arr = { 1, 10, 100, 1000 };
cout << minOperations(arr);
return 0;
} |
// 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;
// Iterate 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<Integer> set = new LinkedHashSet<Integer>();
// 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. |
# 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)
# Iterate 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# 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;
// Iterate 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< int > set = new HashSet< int >();
// 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. |
<script> // JavaScript Program to implement
// the above approach
// Function to find the minimum changes
// needed to make difference of maximum
// and minimum element as (N - 1)
function minOperations(A)
{
let N = A.length;
// Stores the resultant count
let ans = N;
// Maintain a pointer j that will
// denote the rightmost position of
// the valid array
let j = 0;
// Sort the array
A.sort( function (a, b) { return a - b });
// Only keep unique elements
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;
// Store the new size of the array
// after removing duplicates
let M = A.length;
// Iterate over the range
for (let 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;
}
// Driver Code
let arr = [1, 10, 100, 1000];
document.write(minOperations(arr));
// This code is contributed by Potta Lokesh
</script>
|
3
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)