Minimize operations to sort given array by swapping K and arr[i] if K is greater
Last Updated :
16 Feb, 2022
Given an array arr[] of N integers and an integer K, the task is to find the minimum number of operations required to sort the array in non-decreasing order such that in each operation any array element arr[i] can be swapped with K if the value of (arr[i] > K).
Examples:
Input: arr[] = {0, 2, 3, 5, 4}, K = 1
Output: 3
Explanation:
The given array can be sorted using the following steps:
- For i = 1, since arr[1] > K, swapping the values of arr[1] and K. Hence, the array becomes {0, 1, 3, 5, 4} and value of K = 2.
- For i = 2, since arr[2] > K, swapping the values of arr[2] and K. Hence, the array becomes {0, 1, 2, 5, 4} and value of K = 3.
- For i = 3, since arr[3] > K, swapping the values of arr[3] and K. Hence, the array becomes {0, 1, 2, 3, 4} and value of K = 5.
After the above operations, the given array has been sorted.
Input: arr[] = {1, 3, 5, 9, 7}, K = 10
Output: -1
Approach: The given problem can be solved using a Greedy Approach, the idea is to minimize the value of arr[i] at each step for all i in the range [0, N – 1] which is the most optimal choice for the further array to be sorted. Therefore, if the value of arr[i] > K, swapping the values of arr[i] and K is the most optimal choice. Follow the steps below to solve the given problem:
- Create a variable cnt, which stores the count of the operations performed. Initially cnt = 0.
- Traverse the array arr[] using a variable i in the range [0, N-1] in increasing order of i.
- For each index, if arr[i] > K, swap the value of K and arr[i] and increment the value of cnt by 1.
- After every operation, check whether the array arr[] is sorted or not using the approach discussed in this article. If the array arr[] is sorted, return the value of cnt as the required answer.
- If the array is not sorted after performing the above steps, the print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumswaps( int arr[], int N, int K)
{
if (is_sorted(arr, arr + N)) {
return 0;
}
int cnt = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > K) {
swap(arr[i], K);
cnt++;
if (is_sorted(arr, arr + N)) {
return cnt;
}
}
}
return -1;
}
int main()
{
int arr[] = { 0, 2, 3, 5, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 1;
cout << minimumswaps(arr, N, K);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean is_sorted( int arr[], int N)
{
for ( int i = 0 ; i < N - 1 ; i++)
{
if (arr[i] > arr[i + 1 ])
return false ;
}
return true ;
}
static int minimumswaps( int arr[], int N, int K)
{
if (is_sorted(arr, N)) {
return 0 ;
}
int cnt = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] > K) {
int temp = arr[i];
arr[i] = K;
K = temp;
cnt++;
if (is_sorted(arr, N)) {
return cnt;
}
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 0 , 2 , 3 , 5 , 4 };
int N = arr.length;
int K = 1 ;
System.out.println(minimumswaps(arr, N, K));
}
}
|
Python3
def is_sort(arr):
for i in range ( len (arr) - 1 ):
if arr[i]>arr[i + 1 ]:
return False
return True
def minimumswaps(arr, N, K):
if is_sort(arr):
return 0
cnt = 0
for i in range (N):
if (arr[i] > K):
temp = arr[i]
arr[i] = K
K = temp
cnt + = 1
if is_sort(arr):
return cnt
return - 1
if __name__ = = '__main__' :
arr = [ 0 , 2 , 3 , 5 , 4 ]
N = len (arr)
K = 1
print (minimumswaps(arr, N, K))
|
C#
using System;
class GFG {
static bool is_sorted( int [] arr, int N)
{
for ( int i = 0; i < N - 1; i++) {
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
static int minimumswaps( int [] arr, int N, int K)
{
if (is_sorted(arr, N)) {
return 0;
}
int cnt = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] > K) {
int temp = arr[i];
arr[i] = K;
K = temp;
cnt++;
if (is_sorted(arr, N)) {
return cnt;
}
}
}
return -1;
}
public static void Main( string [] args)
{
int [] arr = { 0, 2, 3, 5, 4 };
int N = arr.Length;
int K = 1;
Console.WriteLine(minimumswaps(arr, N, K));
}
}
|
Javascript
<script>
function is_sorted(arr, N) {
for (let i = 0; i < N - 1; i++) {
if (arr[i] > arr[i + 1])
return false ;
}
return true ;
}
function minimumswaps(arr, N, K)
{
if (is_sorted(arr, N)) {
return 0;
}
let cnt = 0;
for (let i = 0; i < N; i++) {
if (arr[i] > K) {
let temp = arr[i];
arr[i] = K;
K = temp;
cnt++;
if (is_sorted(arr, N)) {
return cnt;
}
}
}
return -1;
}
let arr = [0, 2, 3, 5, 4];
let N = arr.length;
let K = 1;
document.write(minimumswaps(arr, N, K));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...