Check if Array can be sorted by swapping adjacent elements having odd sum
Last Updated :
10 Apr, 2023
Given an array arr[], the task is to check if the array can be sorted using the given operation any number of times. In one operation, you can swap any two adjacent elements if their sum is odd.
Examples:
Input: arr[] = [1, 6, 31, 14]
Output: Yes
Explanation: Swap 31 and 14 (31 + 14 = 45 which is odd). The array will be [1, 6, 14, 31] which is sorted.
Input: arr[] = [4, 2]
Output: No
Explanation: Here no swap is possible. Hence the array can not be made sorted using the given operation.
Approach: To solve the problem follow the below idea:
Adjacent elements can only be swapped if they are of different parity. The given array can not be sorted if any element that is greater and of the same parity comes earlier in the array. So, If either the order of even elements or the order of odd elements is not non-decreasing, then it is impossible to sort the given array. We can made two separate arrays for storing even and odd elements and then can check either both of the arrays are in non-decreasing order or not separately.
Below are the steps for the above approach:
- Initialize two arrays/vectors say, even[] and odd[] that store even and odd elements respectively.
- Iterate the given array and check if arr[i] % 2 == 0, push the current element in the array even[], else push the current element in the array odd[].
- Check if both the arrays are separately in non-decreasing order, return true, else return false.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check(vector< int >& arr, int n)
{
vector< int > even;
vector< int > odd;
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 == 0) {
even.push_back(arr[i]);
}
else {
odd.push_back(arr[i]);
}
}
for ( int i = 1; i < even.size(); i++) {
if (even[i - 1] > even[i]) {
return false ;
}
}
for ( int i = 1; i < odd.size(); i++) {
if (odd[i - 1] > odd[i]) {
return false ;
}
}
return true ;
}
int main()
{
vector< int > arr = { 1, 6, 31, 14 };
int n = arr.size();
bool flag = check(arr, n);
if (flag == true )
cout << "Yes" << endl;
else
cout << "No" << endl;
}
|
Java
import java.util.*;
public class Main {
public static boolean check(ArrayList<Integer> arr, int n) {
ArrayList<Integer> even = new ArrayList<Integer>();
ArrayList<Integer> odd = new ArrayList<Integer>();
for ( int i = 0 ; i < arr.size(); i++) {
if (arr.get(i) % 2 == 0 ) {
even.add(arr.get(i));
}
else {
odd.add(arr.get(i));
}
}
for ( int i = 1 ; i < even.size(); i++) {
if (even.get(i - 1 ) > even.get(i)) {
return false ;
}
}
for ( int i = 1 ; i < odd.size(); i++) {
if (odd.get(i - 1 ) > odd.get(i)) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer>(Arrays.asList( 1 , 6 , 31 , 14 ));
int n = arr.size();
boolean flag = check(arr, n);
if (flag == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static bool Check(List< int > arr, int n)
{
List< int > even = new List< int >();
List< int > odd = new List< int >();
for ( int i = 0; i < arr.Count; i++) {
if (arr[i] % 2 == 0) {
even.Add(arr[i]);
}
else {
odd.Add(arr[i]);
}
}
for ( int i = 1; i < even.Count; i++) {
if (even[i - 1] > even[i]) {
return false ;
}
}
for ( int i = 1; i < odd.Count; i++) {
if (odd[i - 1] > odd[i]) {
return false ;
}
}
return true ;
}
static public void Main()
{
List< int > arr = new List< int >{ 1, 6, 31, 14 };
int n = arr.Count;
bool flag = Check(arr, n);
if (flag == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Python3
def check(arr):
even = []
odd = []
for i in range ( len (arr)):
if arr[i] % 2 = = 0 :
even.append(arr[i])
else :
odd.append(arr[i])
if sorted (even) = = even and sorted (odd) = = odd:
return True
else :
return False
arr = [ 1 , 6 , 31 , 14 ]
flag = check(arr)
if flag:
print ( "Yes" )
else :
print ( "No" )
|
Javascript
function check(arr, n) {
let even = [];
let odd = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
even.push(arr[i]);
}
else {
odd.push(arr[i]);
}
}
for (let i = 1; i < even.length; i++) {
if (even[i - 1] > even[i]) {
return false ;
}
}
for (let i = 1; i < odd.length; i++) {
if (odd[i - 1] > odd[i]) {
return false ;
}
}
return true ;
}
let arr = [1, 6, 31, 14];
let n = arr.length;
let flag = check(arr, n);
if (flag === true )
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...