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++
// C++ program to check if it
// is possible to sort the
// array in non-decreasing
// order by swapping
// elements of different types
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
bool sorting_possible(int a[],
int b[], int n)
{
// Consider the array
// to be already sorted
bool sorted = true;
int type1 = 0, type0 = 0, i;
// checking if array is
// already sorted
for (i = 1; i < n; i++) {
// Check for a pair which
// is in decreasing order
if (a[i] < a[i - 1]) {
sorted = false;
break;
}
}
// Count the frequency of
// each type of element
for (i = 0; i < n; i++) {
// type0 stores count
// of elements of type 0
if (b[i] == 0)
type0++;
// type1 stores count
// of elements of type 1
else
type1++;
}
// Return true if array
// is already sorted
if (sorted)
return true;
// Return false if all
// elements are of same
// type and array
// is unsorted
else if (type1 == n
|| type0 == n)
return false;
// Possible for all
// other cases
else
return true;
}
// Driver Program
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
// Java program to check if it is
// possible to sort the array in
// non-decreasing order by swapping
// elements of different types
import java.util.*;
class GFG{
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
static boolean sorting_possible(int a[],
int b[],
int n)
{
// Consider the array
// to be already sorted
boolean sorted = true;
int type1 = 0, type0 = 0, i;
// Checking if array is
// already sorted
for(i = 1; i < n; i++)
{
// Check for a pair which
// is in decreasing order
if (a[i] < a[i - 1])
{
sorted = false;
break;
}
}
// Count the frequency of
// each type of element
for(i = 0; i < n; i++)
{
// type0 stores count
// of elements of type 0
if (b[i] == 0)
type0++;
// type1 stores count
// of elements of type 1
else
type1++;
}
// Return true if array
// is already sorted
if (sorted)
return true;
// Return false if all elements
// are of same type and array
// is unsorted
else if (type1 == n || type0 == n)
return false;
// Possible for all
// other cases
else
return true;
}
// Driver code
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");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to check if it
# is possible to sort the
# array in non-decreasing
# order by swapping
# elements of different types
# Function to check if it is
# possible to sort the array
# in non-decreasing order by
# swapping elements of different types
def sorting_possible(a, b, n):
# Consider the array
# to be already sorted
sorted = True
type1 = 0
type0 = 0
# Checking if array is
# already sorted
for i in range(1, n):
# Check for a pair which
# is in decreasing order
if (a[i] < a[i - 1]):
sorted = False
break
# Count the frequency of
# each type of element
for i in range(n):
# type0 stores count
# of elements of type 0
if (b[i] == 0):
type0 += 1
# type1 stores count
# of elements of type 1
else:
type1 += 1
# Return true if array
# is already sorted
if (sorted != False):
return True
# Return false if all elements
# are of same type and array
# is unsorted
elif (type1 == n or type0 == n):
return False
# Possible for all
# other cases
else:
return True
# Driver code
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")
# This code is contributed by sanjoy_62
C#
// C# program to check if it is
// possible to sort the array in
// non-decreasing order by swapping
// elements of different types
using System;
class GFG{
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
static bool sorting_possible(int []a,
int []b,
int n)
{
// Consider the array
// to be already sorted
bool sorted = true;
int type1 = 0, type0 = 0, i;
// Checking if array is
// already sorted
for(i = 1; i < n; i++)
{
// Check for a pair which
// is in decreasing order
if (a[i] < a[i - 1])
{
sorted = false;
break;
}
}
// Count the frequency of
// each type of element
for(i = 0; i < n; i++)
{
// type0 stores count
// of elements of type 0
if (b[i] == 0)
type0++;
// type1 stores count
// of elements of type 1
else
type1++;
}
// Return true if array
// is already sorted
if (sorted)
return true;
// Return false if all elements
// are of same type and array
// is unsorted
else if (type1 == n || type0 == n)
return false;
// Possible for all
// other cases
else
return true;
}
// Driver code
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");
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to check if it is
// possible to sort the array in
// non-decreasing order by swapping
// elements of different types
// Function to check if it is
// possible to sort the array
// in non-decreasing order by
// swapping elements of
// different types
function sorting_possible(a,b, n)
{
// Consider the array
// to be already sorted
let sorted = true;
let type1 = 0, type0 = 0, i;
// Checking if array is
// already sorted
for(i = 1; i < n; i++)
{
// Check for a pair which
// is in decreasing order
if (a[i] < a[i - 1])
{
sorted = false;
break;
}
}
// Count the frequency of
// each type of element
for(i = 0; i < n; i++)
{
// type0 stores count
// of elements of type 0
if (b[i] == 0)
type0++;
// type1 stores count
// of elements of type 1
else
type1++;
}
// Return true if array
// is already sorted
if (sorted)
return true;
// Return false if all elements
// are of same type and array
// is unsorted
else if (type1 == n || type0 == n)
return false;
// Possible for all
// other cases
else
return true;
}
// Driver code
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");
// This code is contributed by souravghosh0416.
</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)
Similar Reads
Check if an array can be converted to another given array by swapping pairs of unequal elements
Given two arrays arr1[] and arr2[] of size N, consisting of binary integers, the task is to check if arr1[] can be converted to arr2[] by swapping any pair of array elements (arr1[i], arr1[j]) such that i < j and arr1[i] is 1 and arr1[j] is 0 (any number of times). If it is possible to do so, the
11 min read
Maximize Array sum by swapping at most K elements with another array
Given two arrays A and B of size N and an integer K, the task is to find the maximum possible sum of array A by swapping at most K elements with array B. Examples: Input: A[] = {2, 3, 4}, B[] = {6, 8, 5}, K = 1 Output: 15 Explanation: Swap A[0] and B[1]. Hence sum = 8 + 3 + 4 = 15. Input: A[] = {9,
6 min read
Check if an array can be sorted by swapping pairs from indices consisting of unequal elements in another array
Given an array A[] of size N and a binary array B[] of size N, the task is to check if the array A[] can be converted into a sorted array by swapping pairs (A[i], A[j]) if B[i] is not equal to B[j]. If the array A[] can be sorted, then print "Yes". Otherwise, print "No". Examples: Input: A[] = {3, 1
15 min read
Shuffle the position of each Array element by swapping adjacent elements
Given an array arr[], the task is to rearrange the array elements by swapping adjacent elements such that no element remains at the same position after swapping. Examples: Input: arr[] = { 1, 2, 3, 4, 5 } Output: 2 1 5 3 4 Explanation: Adjacent elements are swapped as follows: (1, 2 -> 2, 1) (3,
10 min read
Rearrange an array to make similar indexed elements different from that of another array
Given two sorted arrays A[] and B[] consisting of N distinct integers, the task is to rearrange the elements of array B[] such that, for every ith index, A[i] is not equal to B[i]. If multiple such arrangements exist, print any one of them. If no such arrangement exists, print -1. Examples: Input: A
7 min read
Print elements of an array according to the order defined by another array | set 2
Given two arrays a1[] and a2[], print elements of a1 in such a way that the relative order among the elements will be the same as those are in a2. That is, elements which come before in the array a2[], print those elements first from the array a1[]. For the elements not present in a2, print them at
7 min read
Check if an array of pairs can be sorted by swapping pairs with different first elements
Given an array arr[] consisting of N pairs, where each pair represents the value and ID respectively, the task is to check if it is possible to sort the array by the first element by swapping only pairs having different IDs. If it is possible to sort, then print "Yes". Otherwise, print "No". Example
11 min read
Sort an array with swapping only with a special element is allowed
Given an array of length n + 1, containing elements 1 through n and a space, Requires the use of a given swap (index i, index j) function to sort the array, You can only swap the gap and a number, in the end, put the gap at the end. There will be a number 999 in the array as a gap or space. Examples
10 min read
Convert one array to another using adjacent swaps of elements
Given two arrays arr1[] and arr2[] of N integers. We can choose any two adjacent elements from array arr1[] and swap them if they are of opposite parity, the task is to check if it is possible to convert array arr1[] to array arr2[] by performing the given operation on arr1[]. Print "Yes" if it is p
9 min read
Merging elements of two different arrays alternatively in third array
Given two arrays arr1[] and arr2[], we need to combine two arrays in such a way that the combined array has alternate elements of both. If one array has extra element, then these elements are appended at the end of the combined array. Input : arr1[] = {1, 2, 3, 4, 5, 6} arr2[] = {11, 22, 33, 44}Outp
7 min read