Minimize replacements to sort an array with elements 1, 2, and 3
Last Updated :
14 Feb, 2024
Given an array arr[] of length N containing elements 1, 2 and 3. The task is to find the minimum number of operations required to make array sorted by replacing any elements of given array with either 1, 2 or 3.
Examples:
Input: arr[] = {2, 1, 3, 2, 1}
Output: 3
Explanation:
- 1st Operation: Choose index i = 0, Update arr[0] = 2 into 1. Then updated arr[] = {1, 1, 3, 2, 1}
- 2nd Operation: Choose index i = 2, Update arr[2] = 3 into 2. Then updated arr[] = {1, 1, 2, 2, 1}
- 3rd Operation: Choose index i = 4, Update arr[4] = 1 into 2. Then updated arr[] = {1, 1, 2, 2, 2}
Now, it is clearly visible that arr[] is sorted and required operations were 3. Which is minimum possible.
Input: arr[] = {1, 3, 2, 1, 3, 3}
Output: 2
Explanation: It can be verified that arr[] can be sorted under 2 operations.
Approach: Implement the idea below to solve the problem
As we have choice to update any arr[i] into either 1, 2 or 3. Then, Dynamic Programming can be used to solve this problem. The main concept of DP in the problem will be:
DP[i][j] will store the minimum number of operations to make first i elements of array sorted by making current element j(1, 2 or 3)
Transition:
- DP[i][1] = DP[i – 1][1] + (arr[i – 1] != 1) (If ith element is made 1, then (i – 1)th element should be 1 as well).
- DP[i][2] = min(DP[i – 1][1], [i – 1][2]) + (arr[i – 1] != 2) (If the ith element is made 2 then (i – 1)th element should be either 1 or 2).
- DP[i][3] = min({DP[i – 1][1], DP[i – 1][2], DP[i – 1][3]}) + (arr[i – 1] != 3) (If the ith element is made 3 then (i – 1)th element should be either 1, 2 or 3).
Step-by-step approach:
- Declare a 2D array let say DP of size [N + 1][4] with all initialized to zero.
- Calculate answer for ith state by iterating from i = 1 to N and follow below-mentioned steps:
- In each iteration update DP table as
- DP[i][1] = DP[i – 1][1] + (arr[i – 1] != 1)
- DP[i][2] = min(DP[i – 1][1], DP[i – 1][2]) + (arr[i – 1] != 2)
- DP[i][3] = min({DP[i – 1][1], DP[i – 1][2], DP[i – 1][3]}) + (arr[i – 1] != 3)
- Return min(DP[N][1], DP[N][2], DP[N][3])
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int arr[], int N)
{
vector<vector< int > > dp(N + 1, vector< int >(4, 0));
for ( int i = 1; i <= N; i++) {
dp[i][1] = dp[i - 1][1] + (arr[i - 1] != 1);
dp[i][2] = min(dp[i - 1][1], dp[i - 1][2])
+ (arr[i - 1] != 2);
dp[i][3] = min({ dp[i - 1][1], dp[i - 1][2],
dp[i - 1][3] })
+ (arr[i - 1] != 3);
}
return min({ dp[N][1], dp[N][2], dp[N][3] });
}
int main()
{
int N = 5;
int arr[] = { 2, 1, 3, 2, 1 };
cout << minOperations(arr, N) << endl;
return 0;
}
|
Java
public class MinOperations {
public static int minOperations( int [] arr, int N) {
int [][] dp = new int [N + 1 ][ 4 ];
for ( int i = 1 ; i <= N; i++) {
dp[i][ 1 ] = dp[i - 1 ][ 1 ] + (arr[i - 1 ] != 1 ? 1 : 0 );
dp[i][ 2 ] = Math.min(dp[i - 1 ][ 1 ], dp[i - 1 ][ 2 ]) + (arr[i - 1 ] != 2 ? 1 : 0 );
dp[i][ 3 ] = Math.min(Math.min(dp[i - 1 ][ 1 ], dp[i - 1 ][ 2 ]), dp[i - 1 ][ 3 ]) + (arr[i - 1 ] != 3 ? 1 : 0 );
}
return Math.min(Math.min(dp[N][ 1 ], dp[N][ 2 ]), dp[N][ 3 ]);
}
public static void main(String[] args) {
int N = 5 ;
int [] arr = { 2 , 1 , 3 , 2 , 1 };
System.out.println(minOperations(arr, N));
}
}
|
Python3
def min_operations(arr, N):
dp = [[ 0 ] * 4 for _ in range (N + 1 )]
for i in range ( 1 , N + 1 ):
dp[i][ 1 ] = dp[i - 1 ][ 1 ] + (arr[i - 1 ] ! = 1 )
dp[i][ 2 ] = min (dp[i - 1 ][ 1 ], dp[i - 1 ][ 2 ]) + (arr[i - 1 ] ! = 2 )
dp[i][ 3 ] = min (dp[i - 1 ][ 1 ], dp[i - 1 ][ 2 ],
dp[i - 1 ][ 3 ]) + (arr[i - 1 ] ! = 3 )
return min (dp[N][ 1 ], dp[N][ 2 ], dp[N][ 3 ])
if __name__ = = "__main__" :
N = 5
arr = [ 2 , 1 , 3 , 2 , 1 ]
print (min_operations(arr, N))
|
C#
using System;
class GFG
{
static int MinOperations( int [] arr, int N)
{
int [][] dp = new int [N + 1][];
for ( int i = 0; i <= N; i++)
{
dp[i] = new int [4];
}
for ( int i = 1; i <= N; i++)
{
dp[i][1] = dp[i - 1][1] + (arr[i - 1] != 1 ? 1 : 0);
dp[i][2] = Math.Min(dp[i - 1][1], dp[i - 1][2]) + (arr[i - 1] != 2 ? 1 : 0);
dp[i][3] = Math.Min(Math.Min(dp[i - 1][1], dp[i - 1][2]), dp[i - 1][3]) + (arr[i - 1] != 3 ? 1 : 0);
}
return Math.Min(Math.Min(dp[N][1], dp[N][2]), dp[N][3]);
}
static void Main()
{
int N = 5;
int [] arr = { 2, 1, 3, 2, 1 };
Console.WriteLine(MinOperations(arr, N));
}
}
|
Javascript
function minOperations(arr) {
const N = arr.length;
const dp = new Array(N + 1).fill(0).map(() => new Array(4).fill(0));
for (let i = 1; i <= N; i++) {
dp[i][1] = dp[i - 1][1] + (arr[i - 1] !== 1 ? 1 : 0);
dp[i][2] = Math.min(dp[i - 1][1], dp[i - 1][2]) + (arr[i - 1] !== 2 ? 1 : 0);
dp[i][3] = Math.min(dp[i - 1][1], dp[i - 1][2], dp[i - 1][3]) + (arr[i - 1] !== 3 ? 1 : 0);
}
return Math.min(dp[N][1], dp[N][2], dp[N][3]);
}
const arr = [2, 1, 3, 2, 1];
console.log(minOperations(arr));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...