Sort an array by swapping elements of different type specified by another array
Last Updated :
15 Jun, 2022
Given two arrays a[] and b[] which contain the integer elements and their respective types (either type 0 or type 1) respectively, the task is to check if it is possible to sort the array in non-decreasing order by swapping elements of different types only.
Examples:
Input: a[] = {30, 20, 20, 10}, b[] = {1, 1, 1, 1}
Output: No
Explanation:
Since all elements are of same type, no swaps are allowed and the given array is not sorted in non-decreasing order.
Input: a[] = {6, 5, 4}, b[] = {1, 1, 0}
Output: Yes
Explanation:
Swap 4 and 6 to convert the array into non-decreasing order.
Approach:
To solve the problem mentioned above, the following observations need to be made:
- If the array a[] is already sorted in non-decreasing order, then the answer is Yes.
- If the array a[] is not sorted and all the elements are of the same type, then the answer is No as no swaps are possible.
- In any other case, the answer is Yes as we can always sort the array. This is because we will have at least one element whose type is different from the other elements, so we can swap it with all the other elements any number of times till all the elements are in their sorted position.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool sorting_possible( int a[],
int b[], int n)
{
bool sorted = true ;
int type1 = 0, type0 = 0, i;
for (i = 1; i < n; i++) {
if (a[i] < a[i - 1]) {
sorted = false ;
break ;
}
}
for (i = 0; i < n; i++) {
if (b[i] == 0)
type0++;
else
type1++;
}
if (sorted)
return true ;
else if (type1 == n
|| type0 == n)
return false ;
else
return true ;
}
int main()
{
int a[] = { 15, 1, 2, 17, 6 };
int b[] = { 1, 1, 0, 1, 1 };
int n = sizeof (a) / sizeof (a[0]);
bool res = sorting_possible(a, b, n);
if (res)
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.util.*;
class GFG{
static boolean sorting_possible( int a[],
int b[],
int n)
{
boolean sorted = true ;
int type1 = 0 , type0 = 0 , i;
for (i = 1 ; i < n; i++)
{
if (a[i] < a[i - 1 ])
{
sorted = false ;
break ;
}
}
for (i = 0 ; i < n; i++)
{
if (b[i] == 0 )
type0++;
else
type1++;
}
if (sorted)
return true ;
else if (type1 == n || type0 == n)
return false ;
else
return true ;
}
public static void main(String[] args)
{
int a[] = { 15 , 1 , 2 , 17 , 6 };
int b[] = { 1 , 1 , 0 , 1 , 1 };
int n = a.length;
boolean res = sorting_possible(a, b, n);
if (res)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def sorting_possible(a, b, n):
sorted = True
type1 = 0
type0 = 0
for i in range ( 1 , n):
if (a[i] < a[i - 1 ]):
sorted = False
break
for i in range (n):
if (b[i] = = 0 ):
type0 + = 1
else :
type1 + = 1
if ( sorted ! = False ):
return True
elif (type1 = = n or type0 = = n):
return False
else :
return True
a = [ 15 , 1 , 2 , 17 , 6 ]
b = [ 1 , 1 , 0 , 1 , 1 ]
n = len (a)
res = sorting_possible(a, b, n)
if (res ! = False ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool sorting_possible( int []a,
int []b,
int n)
{
bool sorted = true ;
int type1 = 0, type0 = 0, i;
for (i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
sorted = false ;
break ;
}
}
for (i = 0; i < n; i++)
{
if (b[i] == 0)
type0++;
else
type1++;
}
if (sorted)
return true ;
else if (type1 == n || type0 == n)
return false ;
else
return true ;
}
public static void Main(String[] args)
{
int []a = { 15, 1, 2, 17, 6 };
int []b = { 1, 1, 0, 1, 1 };
int n = a.Length;
bool res = sorting_possible(a, b, n);
if (res)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function sorting_possible(a,b, n)
{
let sorted = true ;
let type1 = 0, type0 = 0, i;
for (i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
sorted = false ;
break ;
}
}
for (i = 0; i < n; i++)
{
if (b[i] == 0)
type0++;
else
type1++;
}
if (sorted)
return true ;
else if (type1 == n || type0 == n)
return false ;
else
return true ;
}
let a = [ 15, 1, 2, 17, 6 ];
let b = [ 1, 1, 0, 1, 1 ];
let n = a.length;
let res = sorting_possible(a, b, n);
if (res)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Illustration:
a[] = {15, 1, 2, 17, 6}
Only 2 is of type 0 and rest are of type 1.
Hence the following steps leads to a sorted array:
15, 1, 2, 17, 6 – > 15, 1, 6, 17, 2
15, 1, 6, 17, 2 -> 15, 1, 6, 2, 17
15, 1, 6, 2, 17 -> 2, 1, 6, 15, 17
2, 1, 6, 15, 17 -> 1, 2, 6, 15, 17
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...