Sorting array with conditional swapping
Last Updated :
17 Aug, 2022
Given an array arr containing elements from [1...to n]. Each element appears exactly once in the array arr. Given an string str of length n-1. Each character of the string is either 0 or 1. In the array, swapping of the i-th element with (i + 1)-th element can be done as many times as we want, if the i-th character of the string is 1. Our task is to find whether it is possible to sort the array or not in ascending order.
Examples:
Input : arr = {1, 2, 5, 3, 4, 6}
str = "01110"
Output : Yes
Explanation :
Here, we can swap arr[2] and arr[3], and then
swap arr[3] and arr[4].
Input : arr = {1, 2, 5, 3, 4, 6}
str = "01010"
Output : No
Explanation :
Here, the 3rd element of the array i.e. 5 can not
be swapped as the 3rd character of the string is 0.
Therefore it is impossible to sort the array.
Approach Run a loop to length of the string str and calculate the max_element of the array from 0 to i for each i. At each iteration, if the i-th character of the string is '0' and the max_element is greater than i + 1 then, it is impossible to sort the array, otherwise, possible.
Basic implementation of the above approach :
C++
// CPP program to Check if it
// is possible to sort the
// array in ascending order.
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible to
// sort the array
string possibleToSort(int* arr, int n, string str)
{
int max_element = -1;
for (long i = 0; i < str.size(); i++) {
// Calculating max_element
// at each iteration.
max_element = max(max_element, arr[i]);
// if we can not swap the i-th element.
if (str[i] == '0') {
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if (max_element > i + 1)
return "No";
}
}
// Otherwise, we can sort the array.
return "Yes";
}
// Driver function
int main()
{
int arr[] = { 1, 2, 5, 3, 4, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
string str = "01110";
cout << possibleToSort(arr, n, str);
return 0;
}
Java
// Java program to Check if it is possible to
// sort the array in ascending order.
import java.util.*;
import java.lang.*;
// Function to check if it is possible to
// sort the array
class GFG
{
public static String possibleToSort(int arr[],
int n, String str)
{
int max_element = -1;
for (int i = 0; i < str.length(); i++)
{
// Calculating max_element at each
// iteration.
max_element = Math.max(max_element,
arr[i]);
// if we can not swap the i-th
// element.
if (str.charAt(i) == '0') {
// if it is impossible to swap
// the max_element then we can
// not sort the array.
if (max_element > i + 1)
return "No";
}
}
// Otherwise, we can sort the array.
return "Yes";
}
// Driven Program
public static void main(String[] args)
{
int arr[] = { 1, 2, 5, 3, 4, 6 };
int n = arr.length;
String str = "01110";
System.out.println(
possibleToSort(arr, n, str));
}
}
// This code is contributed by Prasad Kshirsagar
Python 3
# Python 3 program to Check if it
# is possible to sort the
# array in ascending order.
# Function to check if it is
# possible to sort the array
def possibleToSort(arr, n, str):
max_element = -1
for i in range(len(str)) :
# Calculating max_element
# at each iteration.
max_element = max(max_element, arr[i])
# if we can not swap the i-th element.
if (str[i] == '0') :
# if it is impossible to swap the
# max_element then we can not
# sort the array.
if (max_element > i + 1):
return "No"
# Otherwise, we can sort the array.
return "Yes"
# Driver Code
if __name__ == "__main__":
arr = [ 1, 2, 5, 3, 4, 6 ]
n = len(arr)
str = "01110"
print(possibleToSort(arr, n, str))
# This code is contributed
# by ChitraNayal
C#
// C# program to Check if it
// is possible to sort the
// array in ascending order
using System;
class GFG {
// Function to check if it
// is possible to sort the array
static string possibleToSort(int []arr,
int n,
string str)
{
int max_element = -1;
for (int i = 0; i < str.Length; i++)
{
// Calculating max_element
// at each iteration.
max_element = Math.Max(max_element,
arr[i]);
// if we can not swap
// the i-th element.
if (str[i] == '0')
{
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if (max_element > i + 1)
return "No";
}
}
// Otherwise, we can
// sort the array.
return "Yes";
}
// Driver Code
static public void Main ()
{
int []arr = {1, 2, 5, 3, 4, 6};
int n = arr.Length;
string str = "01110";
Console.WriteLine(possibleToSort(arr, n, str));
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to Check if it is possible
// to sort the array in ascending order.
// Function to check if it is possible
// to sort the array
function possibleToSort($arr, $n, $str)
{
$max_element = -1;
for ($i = 0; $i < sizeof($str); $i++)
{
// Calculating max_element
// at each iteration.
$max_element = max($max_element,
$arr[$i]);
// if we can not swap the i-th element.
if ($str[$i] == '0')
{
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if ($max_element > $i + 1)
return "No";
}
}
// Otherwise, we can sort the array.
return "Yes";
}
// Driver Code
$arr = array(1, 2, 5, 3, 4, 6);
$n = sizeof($arr);
$str = "01110";
echo possibleToSort($arr, $n, $str);
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Javascript program to Check if it
// is possible to sort the
// array in ascending order
// Function to check if it
// is possible to sort the array
function possibleToSort(arr, n, str)
{
let max_element = -1;
for (let i = 0; i < str.length; i++)
{
// Calculating max_element
// at each iteration.
max_element = Math.max(max_element, arr[i]);
// if we can not swap
// the i-th element.
if (str[i] == '0')
{
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if (max_element > i + 1)
return "No";
}
}
// Otherwise, we can
// sort the array.
return "Yes";
}
let arr = [1, 2, 5, 3, 4, 6];
let n = arr.Length;
let str = "01110";
document.write(possibleToSort(arr, n, str));
// This code is contributed by divyesh072019.
</script>
Complexity Analysis:
- Time Complexity: O(n), where n is the size of the given string str
- Auxiliary Space: O(1), as no extra space is used
Similar Reads
Sorting Array with Two Swaps Given an array A[] that represents a permutation of the N numbers, the task is to determine if it's possible to sort the array using two swaps. If it is not possible return false otherwise return true. Examples: Input: N = 4, A[] = {4, 3, 2, 1}Output: TrueExplanation: Swap(A[1], A[4]), now A[] = {1,
7 min read
Sort array by performing swapping under certain condition Given an array of positive elements arr[] of size N. You can select any index i such that i + 1 < N and swap arr[i], arr[i + 1] if they have the same number of set bits in their binary representation. The task is to determine whether it is possible to sort the array in ascending order by swapping
7 min read
Check if array can be sorted with one swap Given an array containing N elements. Find if it is possible to sort it in non-decreasing order using atmost one swap. Examples: Input : arr[] = {1, 2, 3, 4} Output : YES The array is already sorted Input : arr[] = {3, 2, 1} Output : YES Swap 3 and 1 to get [1, 2, 3] Input : arr[] = {4, 1, 2, 3} Out
11 min read
Permutation Sorting with distance and swaps Given a permutation arr[] of size n and a positive integer x, the task is to sort the permutation in increasing order by performing the following operations: You can swap arr[i] with arr[j] if abs(i-j] = x, you can perform this operation any number of times.You can swap arr[i] with arr[j] if abs(i-j
9 min read
Sort an array with swapping only with a special element is allowed Given an array of length n + 1, containing elements 1 through n and a space, Requires the use of a given swap (index i, index j) function to sort the array, You can only swap the gap and a number, in the end, put the gap at the end. There will be a number 999 in the array as a gap or space. Examples
10 min read