Sort an Array by decreasing and increasing elements with adjacent swaps
Last Updated :
04 Sep, 2023
Given an array arr[] of size N. The task is to make an array in increasing order where you are allowed (any number of times) to swap adjacent elements and decrease the value with a smaller index by 1 and increase the value of a greater index by 1. Output “Yes” if it is possible to arrange the array in increasing order otherwise, “No”.
Input: N = 4, arr[] = 6 2 8 9
Output: Yes
Explanation: Swap 1st and Second element, New array is [3, 5, 8, 9] now it is sorted.
Input: N = 3, arr[] = {20, 1, 18}
Output: No
Explanation: We cannot obtain a sorted array by swapping adjacent elements by increasing and decreasing their values.
Approach: This can be solved with the following idea:
Checking adjacent elements, if adjacent elements have a difference of 1, then it is not possible to sort them. If after swapping again the difference between 1 return “No“.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
void SortingArrayswithAdjacentSwaps( int n, vector< int >& a) {
bool possible = true ;
for ( int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
int diff = a[i] - a[i + 1];
if (diff == 1) {
possible = false ;
break ;
}
int temp1 = a[i] - 1;
int temp2 = a[i + 1] + 1;
a[i] = temp2;
a[i + 1] = temp1;
if (i >= 1 && a[i - 1] > a[i]) {
possible = false ;
break ;
}
}
}
if (possible)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main() {
int n = 4;
vector< int > a = {6, 2, 8, 9};
SortingArrayswithAdjacentSwaps(n, a);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class SortingArraysWithAdjacentSwaps {
public static void sortArraysWithAdjacentSwaps( int n, List<Integer> a) {
boolean possible = true ;
for ( int i = 0 ; i < n - 1 ; i++) {
if (a.get(i) > a.get(i + 1 )) {
int diff = a.get(i) - a.get(i + 1 );
if (diff == 1 ) {
possible = false ;
break ;
}
int temp1 = a.get(i) - 1 ;
int temp2 = a.get(i + 1 ) + 1 ;
a.set(i, temp2);
a.set(i + 1 , temp1);
if (i >= 1 && a.get(i - 1 ) > a.get(i)) {
possible = false ;
break ;
}
}
}
if (possible)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
public static void main(String[] args) {
int n = 4 ;
List<Integer> a = new ArrayList<>();
a.add( 6 );
a.add( 2 );
a.add( 8 );
a.add( 9 );
sortArraysWithAdjacentSwaps(n, a);
}
}
|
Python3
def SortingArrayswithAdjacentSwaps(n, a):
possible = True
for i in range (n - 1 ):
if a[i] > a[i + 1 ]:
diff = a[i] - a[i + 1 ]
if diff = = 1 :
possible = False
break
temp1 = a[i] - 1
temp2 = a[i + 1 ] + 1
a[i] = temp2
a[i + 1 ] = temp1
if i > = 1 and a[i - 1 ] > a[i]:
possible = False
break
if possible:
print ( "YES" )
else :
print ( "NO" )
n = 4
a = [ 6 , 2 , 8 , 9 ]
SortingArrayswithAdjacentSwaps(n, a)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void SortingArrayswithAdjacentSwaps( int n,
List< int > a)
{
bool possible = true ;
for ( int i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
int diff = a[i] - a[i + 1];
if (diff == 1) {
possible = false ;
break ;
}
int temp1 = a[i] - 1;
int temp2 = a[i + 1] + 1;
a[i] = temp2;
a[i + 1] = temp1;
if (i >= 1 && a[i - 1] > a[i]) {
possible = false ;
break ;
}
}
}
if (possible)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
static void Main( string [] args)
{
int n = 4;
List< int > a = new List< int >{ 6, 2, 8, 9 };
SortingArrayswithAdjacentSwaps(n, a);
}
}
|
Javascript
function sortArraysWithAdjacentSwaps(n, a) {
let possible = true ;
for (let i = 0; i < n - 1; i++) {
if (a[i] > a[i + 1]) {
let diff = a[i] - a[i + 1];
if (diff === 1) {
possible = false ;
break ;
}
let temp1 = a[i] - 1;
let temp2 = a[i + 1] + 1;
a[i] = temp2;
a[i + 1] = temp1;
if (i >= 1 && a[i - 1] > a[i]) {
possible = false ;
break ;
}
}
}
if (possible)
console.log( "YES" );
else
console.log( "NO" );
}
const n = 4;
const a = [6, 2, 8, 9];
sortArraysWithAdjacentSwaps(n, a);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...