Minimum operations required to Sort the Array using following operations
Given an array arr[] of size N. Make the array arr[] sorted in non-decreasing order in the minimum number of operations where you can choose any integer x and replace elements arr[i] == x equal to 0 for,
0 ? i ? N – 1.
Examples:
Input: n = 3, arr[] = {3, 3, 2}
Output: 1
Explanation: If you choose x = 3 then after one operation array will become {0, 0, 2} which is sorted in non-decreasing order.
Input: n = 4, arr[] = {2, 4, 1, 2}
Output: 3
Explanation: First you can choose x = 2 then after one operation array will become {0, 4, 1, 0} and in the second operation you can choose x = 1 then after the operation array will become {0, 4, 0, 0} and at last you can choose x = 4 and the array will become {0, 0, 0, 0} which is sorted in non-decreasing order.
Approach: The problem can be solved based on the following idea:
This problem can be solved using a greedy approach. We know that an array is sorted in non-decreasing order if arr[i] <= arr[i+1] for all i. So as we get arr[i] > arr[i+1] then we have to set x = arr[i]. So, the optimal idea is to traverse the array from the end and mark the index for which arr[i] > arr[i+1] and then count the different elements till that index.
Follow the below steps to implement the idea:
- Find the last index from the end such that arr[i] > arr[i + 1].
- If the array is already sorted then return 0.
- Else make a visit[] of size n+1 which will be true only for those elements which should be zero to make the array sorted.
- To fill the visit[] we will run a loop from the end of the arr array and mark all the indexes till the last index as true and then from the last index+1 to n-index iterate the arr array and mark the vis index as true for which the element already exists.
- Now count the all true elements of the vis vector and return the answer.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minMoves( int n, int arr[])
{
int last = -1;
for ( int i = n - 2; i >= 0; i--) {
if (arr[i] > arr[i + 1]) {
last = i;
break ;
}
}
if (last == -1) {
return 0;
}
bool visit[n + 1];
for ( int i = 1; i < n + 1; i++) {
visit[i] = false ;
}
for ( int i = 0; i <= last; i++) {
visit[arr[i]] = true ;
}
for ( int i = last + 1; i < n; i++) {
if (visit[arr[i]] == true ) {
last = i;
}
}
for ( int i = last; i >= 0; i--) {
visit[arr[i]] = true ;
}
int ans = 0;
for ( int i = 1; i <= n; i++) {
if (visit[i] == true ) {
ans++;
}
}
return ans;
}
int main()
{
int n = 3;
int arr[] = { 3, 3, 2 };
cout << minMoves(n, arr) << endl;
n = 4;
int arr1[] = { 2, 4, 1, 2 };
cout << minMoves(n, arr1) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int minMoves( int n, int [] arr)
{
int last = - 1 ;
for ( int i = n - 2 ; i >= 0 ; i--) {
if (arr[i] > arr[i + 1 ]) {
last = i;
break ;
}
}
if (last == - 1 ) {
return 0 ;
}
boolean [] visit = new boolean [n + 1 ];
Arrays.fill(visit, false );
for ( int i = 0 ; i <= last; i++) {
visit[arr[i]] = true ;
}
for ( int i = last + 1 ; i < n; i++) {
if (visit[arr[i]] == true ) {
last = i;
}
}
for ( int i = last; i >= 0 ; i--) {
visit[arr[i]] = true ;
}
int ans = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (visit[i] == true ) {
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int n = 3 ;
int [] arr = { 3 , 3 , 2 };
System.out.println(minMoves(n, arr));
n = 4 ;
int [] arr1 = { 2 , 4 , 1 , 2 };
System.out.println(minMoves(n, arr1));
}
}
|
Python3
def minMoves(n,arr):
last = - 1
for i in range (n - 2 , - 1 , - 1 ):
if (arr[i]>arr[i + 1 ]):
last = i
break
if (last = = - 1 ):
return 0
visit = [ True ] * (n + 1 )
for i in range ( 1 ,n + 1 ):
visit[i] = False
for i in range ( 0 ,last + 1 ):
visit[arr[i]] = True
for i in range (last + 1 ,n):
if (visit[arr[i]] = = True ):
last = i
for i in range (last, - 1 , - 1 ):
visit[arr[i]] = True
ans = 0
for i in range ( 1 ,n + 1 ):
if (visit[i] = = True ):
ans + = 1
return ans
n = 3
arr = [ 3 , 3 , 2 ]
print (minMoves(n,arr))
n = 4
arr = [ 2 , 4 , 1 , 2 ]
print (minMoves(n,arr))
|
C#
using System;
using System.Linq;
public class GFG {
public static int minMoves( int n, int [] arr)
{
int last = -1;
for ( int i = n - 2; i >= 0; i--) {
if (arr[i] > arr[i + 1]) {
last = i;
break ;
}
}
if (last == -1) {
return 0;
}
bool [] visit = new bool [n + 1];
for ( int i = 0; i <= n; i++) {
visit[i] = false ;
}
for ( int i = 0; i <= last; i++) {
visit[arr[i]] = true ;
}
for ( int i = last + 1; i < n; i++) {
if (visit[arr[i]] == true ) {
last = i;
}
}
for ( int i = last; i >= 0; i--) {
visit[arr[i]] = true ;
}
int ans = 0;
for ( int i = 1; i <= n; i++) {
if (visit[i] == true ) {
ans++;
}
}
return ans;
}
static public void Main()
{
int n = 3;
int [] arr = { 3, 3, 2 };
Console.WriteLine(minMoves(n, arr));
n = 4;
int [] arr1 = { 2, 4, 1, 2 };
Console.WriteLine(minMoves(n, arr1));
}
}
|
Javascript
function minMoves(n, arr) {
let last = -1;
for (let i = n - 2; i >= 0; i--) {
if (arr[i] > arr[i + 1]) {
last = i;
break ;
}
}
if (last == -1) {
return 0;
}
let visit = new Array(n + 1).fill(0);
for (let i = 1; i < n + 1; i++) {
visit[i] = false ;
}
for (let i = 0; i <= last; i++) {
visit[arr[i]] = true ;
}
for (let i = last + 1; i < n; i++) {
if (visit[arr[i]] == true ) {
last = i;
}
}
for (let i = last; i >= 0; i--) {
visit[arr[i]] = true ;
}
let ans = 0;
for (let i = 1; i <= n; i++) {
if (visit[i] == true ) {
ans++;
}
}
return ans;
}
let n = 3;
let arr = [3, 3, 2];
console.log(minMoves(n, arr) + "<br>" );
n = 4;
let arr1 = [2, 4, 1, 2];
console.log(minMoves(n, arr1) + "<br>" );
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related articles:
Last Updated :
30 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...