Sorting array with conditional swapping | C # 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 ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code
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 ) ) ; } }