Minimum replacements with 0 to sort the array
Given an array A[] of N integers, the task is to find the minimum number of operations to sort the array in non-decreasing order, by choosing an integer X and replacing all the occurrences of X in the array with 0.
Examples:
Input: N = 5, A[] = {2, 2, 1, 1, 3}
Output: 1
Explanation: We choose X = 2 and replace all the occurrences of 2 with 0. Now the array becomes {2, 2, 1, 1, 3} -> {0, 0, 1, 1, 3} , which is sorted in increasing order.
Input: N = 4, A[] = {2, 4, 1, 2}
Output: 3
Approach: The problem can be solved easily with the help of a Map.
Observations:
There are 2 cases that need to be considered :
- Case 1: Same element occurs more than once non-contiguously
- Consider the array : {1,6,3,4,5,3,2}.
- Now, since 3 at index 5 is greater than its next element, so we will make that 0 (as well as 3 at index 2).
- The array becomes {1,6,0,4,5,0,2}.
- So, the only way to sort the array would be to make all the elements before the zeroes equal to 0. i.e. the array becomes {0,0,0,0,0,0,2}.
- Case 2: Element at ith index is greater than the element at (i+1)th index :
- Consider the array : {1,2,3,5,4}.
- Since the element at the 3rd index is greater than the element at 4th index, we have to make the element at 3rd index equal to zero.
- So , the array becomes {1,2,3,0,4}.
- Now, the only way to sort the array would be to make all the elements before the zero equal to 0. i.e. the array becomes {0,0,0,0,4}.
It can be observed that in the end Case 2 breaks down to Case 1.
Considering the above cases, the problem can be solved following the below steps :
- Declare a hash map and add the frequency of each element of the array into the map.
- Iterate through the array from the back, i.e. from i=N-1 to i=0.
- At each iteration, handle Cases 1 and 2 as explained above.
- If iteration completes, return 0.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumReplacements( int A[], int N)
{
map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[A[i]]++;
}
for ( int i = N - 1; i >= 0; i--) {
while (i > 0 && A[i] == A[i - 1]) {
mp[A[i]]--;
i--;
}
mp[A[i]]--;
if (mp[A[i]] == 0) {
mp.erase(A[i]);
}
if (mp.find(A[i]) != mp.end()) {
return mp.size();
}
if (i > 0 && A[i - 1] > A[i]) {
return mp.size();
}
}
return 0;
}
int main()
{
int N = 5;
int A[] = { 2, 2, 1, 1, 3 };
int answer = minimumReplacements(A, N);
cout << answer;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int minimumReplacements( int A[], int N)
{
TreeMap<Integer, Integer> mp =
new TreeMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++) {
if (mp.get(A[i])!= null )
mp.put(A[i],mp.get(A[i])+ 1 );
else
mp.put(A[i], 1 );
}
for ( int i = N - 1 ; i >= 0 ; i--) {
while (i > 0 && A[i] == A[i - 1 ]) {
mp.put(A[i],mp.get(A[i])- 1 );
i--;
}
mp.put(A[i],mp.get(A[i])- 1 );
if (mp.get(A[i]) == 0 ) {
mp.remove(A[i]);
}
if (mp.get(A[i]) != null ) {
return mp.size();
}
if (i > 0 && A[i - 1 ] > A[i]) {
return mp.size();
}
}
return 0 ;
}
public static void main (String[] args) {
int N = 5 ;
int A[] = { 2 , 2 , 1 , 1 , 3 };
int answer = minimumReplacements(A, N);
System.out.println(answer);
}
}
|
Python3
def minimumReplacements(A, N):
mp = dict .fromkeys(A, 0 );
for i in range (N) :
if A[i] in mp :
mp[A[i]] + = 1 ;
else :
mp[A[i]] = 1
for i in range (N - 1 , - 1 , - 1 ) :
while (i > 0 and A[i] = = A[i - 1 ]) :
mp[A[i]] - = 1 ;
i - = 1 ;
mp[A[i]] - = 1 ;
if (mp[A[i]] = = 0 ) :
mp.pop(A[i]);
if A[i] in mp :
return len (mp);
if (i > 0 and A[i - 1 ] > A[i]) :
return len (mp);
return 0 ;
if __name__ = = "__main__" :
N = 5 ;
A = [ 2 , 2 , 1 , 1 , 3 ];
answer = minimumReplacements(A, N);
print (answer);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main( string [] args)
{
int N = 5;
int [] A = { 2, 2, 1, 1, 3 };
int answer = minimumReplacements(A, N);
Console.WriteLine(answer);
}
static int minimumReplacements( int [] A, int N)
{
SortedDictionary< int , int > mp
= new SortedDictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(A[i]))
mp[A[i]] += 1;
else
mp[A[i]] = 1;
}
for ( int i = N - 1; i >= 0; i--) {
while (i > 0 && A[i] == A[i - 1]) {
mp[A[i]] -= 1;
i--;
}
mp[A[i]] -= 1;
if (mp[A[i]] == 0) {
mp.Remove(A[i]);
}
if (mp.ContainsKey(A[i])) {
return mp.Count();
}
if (i > 0 && A[i - 1] > A[i]) {
return mp.Count();
}
}
return 0;
}
}
|
Javascript
<script>
function minimumReplacements(A, N) {
let mp = new Map();
for (let i = 0; i < N; i++) {
if (mp.get(A[i]) != null )
mp.set(A[i], mp.get(A[i]) + 1);
else
mp.set(A[i], 1);
}
for (let i = N - 1; i >= 0; i--) {
while (i > 0 && A[i] == A[i - 1]) {
mp.set(A[i], mp.get(A[i]) - 1);
i--;
}
mp.set(A[i], mp.get(A[i]) - 1);
if (mp.get(A[i]) == 0) {
mp. delete (A[i]);
}
if (mp.get(A[i]) != null ) {
return mp.size;
}
if (i > 0 && A[i - 1] > A[i]) {
return mp.size;
}
}
return 0;
}
let N = 5;
let A = [2, 2, 1, 1, 3];
let answer = minimumReplacements(A, N);
document.write(answer);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...