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++
#include <bits/stdc++.h>
using namespace std;
string possibleToSort( int * arr, int n, string str)
{
int max_element = -1;
for ( long i = 0; i < str.size(); i++) {
max_element = max(max_element, arr[i]);
if (str[i] == '0' ) {
if (max_element > i + 1)
return "No" ;
}
}
return "Yes" ;
}
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
import java.util.*;
import java.lang.*;
class GFG
{
public static String possibleToSort( int arr[],
int n, String str)
{
int max_element = - 1 ;
for ( int i = 0 ; i < str.length(); i++)
{
max_element = Math.max(max_element,
arr[i]);
if (str.charAt(i) == '0' ) {
if (max_element > i + 1 )
return "No" ;
}
}
return "Yes" ;
}
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));
}
}
|
Python 3
def possibleToSort(arr, n, str ):
max_element = - 1
for i in range ( len ( str )) :
max_element = max (max_element, arr[i])
if ( str [i] = = '0' ) :
if (max_element > i + 1 ):
return "No"
return "Yes"
if __name__ = = "__main__" :
arr = [ 1 , 2 , 5 , 3 , 4 , 6 ]
n = len (arr)
str = "01110"
print (possibleToSort(arr, n, str ))
|
C#
using System;
class GFG {
static string possibleToSort( int []arr,
int n,
string str)
{
int max_element = -1;
for ( int i = 0; i < str.Length; i++)
{
max_element = Math.Max(max_element,
arr[i]);
if (str[i] == '0' )
{
if (max_element > i + 1)
return "No" ;
}
}
return "Yes" ;
}
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));
}
}
|
PHP
<?php
function possibleToSort( $arr , $n , $str )
{
$max_element = -1;
for ( $i = 0; $i < sizeof( $str ); $i ++)
{
$max_element = max( $max_element ,
$arr [ $i ]);
if ( $str [ $i ] == '0' )
{
if ( $max_element > $i + 1)
return "No" ;
}
}
return "Yes" ;
}
$arr = array (1, 2, 5, 3, 4, 6);
$n = sizeof( $arr );
$str = "01110" ;
echo possibleToSort( $arr , $n , $str );
?>
|
Javascript
<script>
function possibleToSort(arr, n, str)
{
let max_element = -1;
for (let i = 0; i < str.length; i++)
{
max_element = Math.max(max_element, arr[i]);
if (str[i] == '0' )
{
if (max_element > i + 1)
return "No" ;
}
}
return "Yes" ;
}
let arr = [1, 2, 5, 3, 4, 6];
let n = arr.Length;
let str = "01110" ;
document.write(possibleToSort(arr, n, str));
</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
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2022
Like Article
Save Article