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”.
Examples:
Input: arr[] = {{340000, 2}, {45000, 1}, {30000, 2}, {50000, 4}}
Output: Yes
Explanation:
One of the possible way to sort the array is to swap the array elements in the following order:
- Swap, arr[0] and arr[3], which modifies the array to arr[] = {{50000, 4}, {45000, 1}, {30000, 2}, {340000, 2}}.
- Swap, arr[0] and arr[2], which modifies the array to arr[] = {{30000, 2}, {45000, 1}, {50000, 4}, {340000, 2}}.
Therefore, after the above steps the given array is sorted by the first element..
Input: arr[] = {{15000, 2}, {34000, 2}, {10000, 2}}
Output: No
Approach: The given problem can be solved based on the observation that the array can be sorted if there exist any two array elements with different IDs. Follow the steps below to solve the problem:
- Initialize a variable, say X that stores the ID of the pair at index 0.
- Traverse the array arr[] and if there exists any pair whose ID is different from X, then print “Yes” and break out of the loop.
- After completing the above steps, if all the elements have the same IDs and if the array is already sorted then print “Yes”. Otherwise, Print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isSorted(pair< int , int >* arr,
int N)
{
for ( int i = 1; i < N; i++) {
if (arr[i].first
> arr[i - 1].first) {
return false ;
}
}
return true ;
}
string isPossibleToSort(
pair< int , int >* arr, int N)
{
int group = arr[0].second;
for ( int i = 1; i < N; i++) {
if (arr[i].second != group) {
return "Yes" ;
}
}
if (isSorted(arr, N)) {
return "Yes" ;
}
else {
return "No" ;
}
}
int main()
{
pair< int , int > arr[]
= { { 340000, 2 }, { 45000, 1 },
{ 30000, 2 }, { 50000, 4 } };
int N = sizeof (arr) / sizeof (arr[0]);
cout << isPossibleToSort(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
static boolean isSorted( int [][] arr, int N)
{
for ( int i = 1 ; i < N; i++) {
if (arr[i][ 0 ] > arr[i - 1 ][ 0 ]) {
return false ;
}
}
return true ;
}
static String isPossibleToSort( int [][] arr, int N)
{
int group = arr[ 0 ][ 1 ];
for ( int i = 1 ; i < N; i++) {
if (arr[i][ 1 ] != group) {
return "Yes" ;
}
}
if (isSorted(arr, N)) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void main(String[] args)
{
int arr[][] = { { 340000 , 2 },
{ 45000 , 1 },
{ 30000 , 2 },
{ 50000 , 4 } };
int N = arr.length;
System.out.print(isPossibleToSort(arr, N));
}
}
|
Python3
def isSorted(arr, N):
for i in range ( 1 , N):
if (arr[i][ 0 ] > arr[i - 1 ][ 0 ]):
return False
return True
def isPossibleToSort(arr, N):
group = arr[ 0 ][ 1 ]
for i in range ( 1 , N):
if (arr[i][ 1 ] ! = group):
return "Yes"
if (isSorted(arr, N)):
return "Yes"
else :
return "No"
if __name__ = = '__main__' :
arr = [ [ 340000 , 2 ], [ 45000 , 1 ],[ 30000 , 2 ], [ 50000 , 4 ] ]
N = len (arr)
print (isPossibleToSort(arr, N))
|
C#
using System;
class GFG {
static bool isSorted( int [, ] arr, int N)
{
for ( int i = 1; i < N; i++) {
if (arr[i, 0] > arr[i - 1, 0]) {
return false ;
}
}
return true ;
}
static string isPossibleToSort( int [, ] arr, int N)
{
int group = arr[0, 1];
for ( int i = 1; i < N; i++) {
if (arr[i, 1] != group ) {
return "Yes" ;
}
}
if (isSorted(arr, N)) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void Main()
{
int [, ] arr = { { 340000, 2 },
{ 45000, 1 },
{ 30000, 2 },
{ 50000, 4 } };
int N = arr.GetLength(0);
Console.WriteLine(isPossibleToSort(arr, N));
}
}
|
Javascript
<script>
function isSorted(arr, N) {
for (let i = 1; i < N; i++) {
if (arr[i][0] > arr[i - 1][0]) {
return false ;
}
}
return true ;
}
function isPossibleToSort(arr, N) {
let group = arr[0][1];
for (let i = 1; i < N; i++) {
if (arr[i][1] != group) {
return "Yes" ;
}
}
if (isSorted(arr, N)) {
return "Yes" ;
}
else {
return "No" ;
}
}
let arr = [[340000, 2],
[15000, 2],
[34000, 2],
[10000, 2]];
let N = arr.length;
document.write(isPossibleToSort(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.
Approach 2: Sorting Technique:
Here’s another approach to solve the problem:
- Sort the given array of pairs in descending order of the first element.
- Traverse the sorted array of pairs and check if the i-th element and (i+1)-th element belong to the same group. If not, return “Yes” as they need to be swapped to make the array sorted.
- If all pairs belong to the same group and the array is still not sorted, return “No”.
- Time Complexity: O(n*log(n)) where n is the number of elements in the array.
- Space Complexity: O(1)
Here’s the implementation of this approach in C++:
C++
#include <bits/stdc++.h>
using namespace std;
bool comparePairs(pair< int , int > a, pair< int , int > b) {
return a.first > b.first;
}
string isPossibleToSort(pair< int , int >* arr, int N) {
sort(arr, arr + N, comparePairs);
bool isSorted = true ;
for ( int i = 1; i < N; i++) {
if (arr[i].first > arr[i-1].first) {
isSorted = false ;
break ;
}
}
if (isSorted) {
return "Yes" ;
}
int group = arr[0].second;
for ( int i = 1; i < N; i++) {
if (arr[i].second != group) {
return "Yes" ;
}
}
return "No" ;
}
int main() {
pair< int , int > arr[] = { { 340000, 2 }, { 45000, 1 }, { 30000, 2 }, { 50000, 4 } };
int N = sizeof (arr) / sizeof (arr[0]);
cout << isPossibleToSort(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Pair[] arr = { new Pair( 340000 , 2 ), new Pair( 45000 , 1 ), new Pair( 30000 , 2 ), new Pair( 50000 , 4 ) };
int N = arr.length;
System.out.println(isPossibleToSort(arr, N));
}
static class Pair {
int first, second;
public Pair( int first, int second) {
this .first = first;
this .second = second;
}
}
static boolean comparePairs(Pair a, Pair b) {
return a.first > b.first;
}
static String isPossibleToSort(Pair[] arr, int N) {
Arrays.sort(arr, new Comparator<Pair>() {
public int compare(Pair a, Pair b) {
return comparePairs(a, b) ? - 1 : 1 ;
}
});
boolean isSorted = true ;
for ( int i = 1 ; i < N; i++) {
if (arr[i].first > arr[i- 1 ].first) {
isSorted = false ;
break ;
}
}
if (isSorted) {
return "Yes" ;
}
int group = arr[ 0 ].second;
for ( int i = 1 ; i < N; i++) {
if (arr[i].second != group) {
return "Yes" ;
}
}
return "No" ;
}
}
|
Output:
Yes
Time Complexity: O(n*log(n)) where n is the number of elements in the array.
Auxiliary Space: O(1)