We are given an unsorted array of integers in the range from 0 to n-1. We are allowed to swap adjacent elements in array many number of times but only if the absolute difference between these element is 1. Check if it is possible to sort the array.If yes then print “yes” else “no”.
Examples:
Input : arr[] = {1, 0, 3, 2}
Output : yes
Explanation:- We can swap arr[0] and arr[1].
Again we swap arr[2] and arr[3].
Final arr[] = {0, 1, 2, 3}.
Input : arr[] = {2, 1, 0}
Output : no
Although the problems looks complex at first look, there is a simple solution to it. If we traverse array from left to right and we make sure elements before an index i are sorted before we reach i, we must have maximum of arr[0..i-1] just before i. And this maximum must be either smaller than arr[i] or just one greater than arr[i]. In first case, we simply move ahead. In second case, we swap and move ahead.
Compare the current element with the next element in array.If current element is greater than next element then do following:-
- Check if difference between two numbers is 1 then swap it.
- else Return false.
If we reach end of array, we return true.
C++
#include<bits/stdc++.h>
using namespace std;
bool checkForSorting( int arr[], int n)
{
for ( int i=0; i<n-1; i++)
{
if (arr[i] > arr[i+1])
{
if (arr[i] - arr[i+1] == 1)
swap(arr[i], arr[i+1]);
else
return false ;
}
}
return true ;
}
int main()
{
int arr[] = {1,0,3,2};
int n = sizeof (arr)/ sizeof (arr[0]);
if (checkForSorting(arr, n))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
class Main
{
static boolean checkForSorting( int arr[], int n)
{
for ( int i= 0 ; i<n- 1 ; i++)
{
if (arr[i] > arr[i+ 1 ])
{
if (arr[i] - arr[i+ 1 ] == 1 )
{
int temp = arr[i];
arr[i] = arr[i+ 1 ];
arr[i+ 1 ] = temp;
}
else
return false ;
}
}
return true ;
}
public static void main(String args[])
{
int arr[] = { 1 , 0 , 3 , 2 };
int n = arr.length;
if (checkForSorting(arr, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkForSorting(arr, n):
for i in range ( 0 ,n - 1 ):
if (arr[i] > arr[i + 1 ]):
if (arr[i] - arr[i + 1 ] = = 1 ):
arr[i], arr[i + 1 ] = arr[i + 1 ], arr[i]
else :
return False
return True
arr = [ 1 , 0 , 3 , 2 ]
n = len (arr)
if (checkForSorting(arr, n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool checkForSorting( int []arr, int n)
{
for ( int i=0; i<n-1; i++)
{
if (arr[i] > arr[i+1])
{
if (arr[i] - arr[i+1] == 1)
{
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
else
return false ;
}
}
return true ;
}
public static void Main()
{
int []arr = {1, 0, 3, 2};
int n = arr.Length;
if (checkForSorting(arr, n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function checkForSorting( $arr , $n )
{
$temp = 0;
for ( $i = 0; $i < $n - 1; $i ++)
{
if ( $arr [ $i ] > $arr [ $i + 1])
{
if ( $arr [ $i ] - $arr [ $i + 1] == 1)
{
$temp = $arr [ $i ];
$arr [ $i ] = $arr [ $i + 1];
$arr [ $i + 1] = $temp ;
}
else
return false;
}
}
return true;
}
$arr = array (1,0,3,2);
$n = sizeof( $arr );
if (checkForSorting( $arr , $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function checkForSorting(arr, n)
{
let temp = 0;
for (let i = 0; i < n - 1; i++)
{
if (arr[i] > arr[i + 1])
{
if (arr[i] - arr[i + 1] == 1)
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
else
return false ;
}
}
return true ;
}
let arr = new Array(1,0,3,2);
let n = arr.length;
if (checkForSorting(arr, n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity=O(n)
Auxiliary Space=O(1)
This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.