Minimum count of array elements that must be changed such that difference between maximum and minimum array element is N - 1
Last Updated :
18 Apr, 2023
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++
// 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
// 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.
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)
# 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#
// 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.
JavaScript
<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>
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)
Similar Reads
Minimize difference between maximum and minimum element by decreasing and increasing Array elements by 1 Given an array arr[], consisting of N positive integers. The task is to minimize the difference between the maximum and the minimum element of the array by performing the below operation any number of times (possibly zero). In one operation choose 2 different index, i and j and decrement arr[i] and
5 min read
Smallest number that can replace all -1s in an array such that maximum absolute difference between any pair of adjacent elements is minimum Given an array arr[] consisting of N positive integers and some elements as -1, the task is to find the smallest number, say K, such that replacing all -1s in the array by K minimizes the maximum absolute difference between any pair of adjacent elements. Examples: Input: arr[] = {-1, 10, -1, 12, -1}
8 min read
Minimize difference between maximum and minimum array elements by removing a K-length subarray Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum difference between the maximum and minimum element present in the array after removing any subarray of size K. Examples: Input: arr[] = {4, 5, 8, 9, 1, 2}, K = 2Output: 4Explanation: Remove the subarray {
10 min read
Minimum distance between the maximum and minimum element of a given Array Given an array A[] consisting of N elements, the task is to find the minimum distance between the minimum and the maximum element of the array.Examples: Input: arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 8, 2} Output: 3 Explanation: The minimum element(= 1) is present at indices {2, 4} The maximum elemen
8 min read
Split array into subarrays such that sum of difference between their maximums and minimums is maximum Given an array arr[] consisting of N integers, the task is to split the array into subarrays such that the sum of the difference between the maximum and minimum elements for all the subarrays is maximum. Examples : Input: arr[] = {8, 1, 7, 9, 2}Output: 14Explanation:Consider splitting the given arra
6 min read
Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1 Given an array arr[] consisting of N positive integers, the task is to maximize the sum of the array element such that the first element of the array is 1 and the difference between the adjacent elements of the array is at most 1 after performing the following operations: Rearrange the array element
7 min read
Minimize absolute difference between the smallest and largest array elements by minimum increment decrement operations Given an array arr[] consisting of N positive integers, the task is to minimize the number of operations required to minimize the absolute difference between the smallest and largest elements present in the array. In each operation, subtract 1 from an array element and increment 1 to another array e
8 min read
Minimize the difference between the maximum and minimum values of the modified array Given an array A of n integers and integer X. You may choose any integer between -X\leq k\leq X , and add k to A[i] for each 0\leq i \leq n-1 . The task is to find the smallest possible difference between the maximum value of A and the minimum value of A after updating array A. Examples: Input: arr[
5 min read
Minimize maximum difference between adjacent elements possible by removing a single array element Given an sorted array arr[] consisting of N elements, the task is to find the minimum of all maximum differences between adjacent elements of all arrays obtained by removal of any single array element. Examples: Input: arr[ ] = { 1, 3, 7, 8}Output: 5Explanation:All possible arrays after removing a s
6 min read
Generate an N-length array having length of non-decreasing subarrays maximized and minimum difference between first and last array elements Given an array arr[ ] of size N, the task is to print an N-length array whose sum of lengths of all non-decreasing subarrays is maximum and the difference between the first and last elements is minimum. Examples: Input: N = 5, arr = {4, 3, 5, 3, 2}Output: {3, 4, 5, 2, 3}Explanation: Difference betwe
6 min read