Check if Array can be sorted in non-decreasing order using given operations
Last Updated :
06 Jun, 2022
Given an array arr[] of size N consisting of positive integers, the task is to check if the array can be sorted in non-decreasing order by performing the following operations:
- Select two adjacent elements.
- Swap the elements and invert their signs.
- All elements in the sorted array received at the end, must be positive.
Example:
Input: N = 4, arr[] = [4, 3, 2, 5]
Output: true
Explanation: In the given array, perform the following operations:
Swap arr[0] and arr[1] updated array: [-3, -4, 2, 5]
Swap arr[1] and arr[2] updated array: [-3, -2, 4, 5]
Swap arr[1] and arr[0] updated array: [2, 3, 4, 5]
The array is sorted in non-decreasing order and the elements are all positive.
Input: N = 4, arr[] = [3, 3, 2, 2]
Output: true
Explanation: In the given array, perform the following operations :
Swap arr[0] and arr[2] updated array: [-2, 3, -3, 2]
Swap arr[1] and arr[3] updated array: [-2, -2, -3, -3]
Swap arr[1] and arr[0] updated array: [2, 2, -3, -3]
Swap arr[2] and arr[3] updated array: [2, 2, 3, 3]
The array is sorted in non-decreasing order and the elements are all positive.
Input: N = 5, arr[] = [1, 2, 3, 5, 4]
Output: false
Explanation: There is no way to sort the array such that it follows all the mentioned conditions.
Approach: The problem can be solved using the Greedy approach based on the following observation:
Each number needs to move an even distance because they should all be positive at the end. And a positive number remains positive only after even number of inversion of signs.
So the difference of distance for all array elements between the given one and the sorted array must be even.
Follow the below steps to solve this problem:
- Initialize an empty map (say ctr[2][]) to keep the frequency of an array element in even position and odd position.
- Iterate over an array arr and increment the ctr[i % 1][arr[i]] by 1
- Sort the array arr[].
- Iterate over the array arr[] and decrement the ctr[i % 1][arr[i]] by 1.
- Iterate over the array arr[] and if ctr[1][a[i]] ≠0 or ctr[0][a[i]] ≠0 then return false.
- Otherwise, if the iteration is complete then return true.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isItPossible( int n, vector< int >& a)
{
map< int , int > ctr[2];
for ( int i = 0; i < n; i++) {
ctr[i % 2][a[i]]++;
}
sort(a.begin(), a.end());
for ( int i = 0; i < n; i++) {
ctr[i % 2][a[i]]--;
}
for ( int i = 0; i < n; i++) {
if (ctr[0][a[i]] != 0 || ctr[1][a[i]] != 0) {
return false ;
}
}
return true ;
}
int main()
{
int N = 4;
vector< int > arr = { 3, 3, 2, 2 };
cout << boolalpha
<< isItPossible(N, arr);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static boolean isItPossible( int n, int [] a)
{
Map<Integer, Integer> ctr1 = new HashMap<Integer, Integer>();
Map<Integer, Integer> ctr2 = new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 )
{
if (ctr1.containsKey(a[i]))
ctr1.put(a[i], ctr1.get(a[i]) + 1 );
else
ctr1.put(a[i], 1 );
}
else
{
if (ctr2.containsKey(a[i]))
ctr2.put(a[i], ctr2.get(a[i]) + 1 );
else
ctr2.put(a[i], 1 );
}
}
Arrays.sort(a);
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 )
{
if (ctr1.containsKey(a[i]))
ctr1.put(a[i], ctr1.get(a[i]) - 1 );
else
ctr1.put(a[i], - 1 );
}
else
{
if (ctr2.containsKey(a[i]))
ctr2.put(a[i], ctr2.get(a[i]) - 1 );
else
ctr2.put(a[i], - 1 );
}
}
for ( int i = 0 ; i < n; i++) {
if (ctr1.get(a[i]) != 0 || ctr2.get(a[i]) != 0 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int N = 4 ;
int [] arr = { 3 , 3 , 2 , 2 };
System.out.println(isItPossible(N, arr));
}
}
|
Python3
def isItPossible(n, a):
ctr = [ dict (), dict ()]
for i in range (n):
if a[i] in ctr[i % 2 ]:
ctr[i % 2 ][a[i]] + = 1
else :
ctr[i % 2 ][a[i]] = 1
a.sort()
for i in range (n):
if a[i] in ctr[i % 2 ]:
ctr[i % 2 ][a[i]] - = 1
for i in range (n):
if a[i] in ctr[ 0 ] and ctr[ 0 ][a[i]] ! = 0 or a[i] in ctr[ 1 ] and ctr[ 1 ][a[i]] ! = 0 :
return False
return True
N = 4
arr = [ 3 , 3 , 2 , 2 ]
print (isItPossible(N, arr))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
public class GFG
{
public static bool isItPossible( int n, int [] a)
{
var ctr1 = new Dictionary< int , int >();
var ctr2 = new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (ctr1.ContainsKey(a[i])) {
ctr1[a[i]] = ctr1[a[i]] + 1;
}
else {
ctr1[a[i]] = 1;
}
}
else {
if (ctr2.ContainsKey(a[i])) {
ctr2[a[i]] = ctr2[a[i]] + 1;
}
else {
ctr2[a[i]] = 1;
}
}
}
Array.Sort(a);
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (ctr1.ContainsKey(a[i])) {
ctr1[a[i]] = ctr1[a[i]] - 1;
}
else {
ctr1[a[i]] = -1;
}
}
else {
if (ctr2.ContainsKey(a[i])) {
ctr2[a[i]] = ctr2[a[i]] - 1;
}
else {
ctr2[a[i]] = -1;
}
}
}
for ( int i = 0; i < n; i++) {
if (ctr1[a[i]] != 0 || ctr2[a[i]] != 0) {
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
var N = 4;
int [] arr = { 3, 3, 2, 2 };
Console.WriteLine(GFG.isItPossible(N, arr));
}
}
|
Javascript
<script>
const isItPossible = (n, a) => {
let ctr = [{}, {}];
for (let i = 0; i < n; i++) {
ctr[i % 2][a[i]] = a[i] in ctr[i % 2] ? ctr[i % 2][a[i]] + 1 : 1;
}
a.sort();
for (let i = 0; i < n; i++) {
if (a[i] in ctr[i % 2])
ctr[i % 2][a[i]]--;
}
for (let i = 0; i < n; i++) {
if (a[i] in ctr[0] && ctr[0][a[i]] != 0 || a[i] in ctr[1] && ctr[1][a[i]] != 0) {
return false ;
}
}
return true ;
}
let N = 4;
let arr = [3, 3, 2, 2];
s
document.write(isItPossible(N, arr));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...