给定一个由N对组成的数组arr[] ,其中每对分别代表值和ID ,任务是检查是否可以通过仅交换具有不同ID 的对来按第一个元素对数组进行排序。如果可以排序,则打印“是” 。否则,打印“否” 。
例子:
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
方法:如果存在任意两个具有不同ID 的数组元素,则可以基于对数组进行排序的观察来解决给定的问题。请按照以下步骤解决问题:
- 初始化一个变量,比如X ,它将对的ID存储在索引0 处。
- 遍历数组arr[] ,如果存在任何ID与X不同的对,则打印“Yes”并跳出循环。
- 完成上述步骤后,如果所有元素都具有相同的ID,并且数组已经排序,则打印“Yes” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if an
// array is sorted or not
bool isSorted(pair* arr,
int N)
{
// Traverse the array arr[]
for (int i = 1; i < N; i++) {
if (arr[i].first
> arr[i - 1].first) {
return false;
}
}
// Return true
return true;
}
// Function to check if it is possible
// to sort the array w.r.t. first element
string isPossibleToSort(
pair* arr, int N)
{
// Stores the ID of the first element
int group = arr[0].second;
// Traverse the array arr[]
for (int i = 1; i < N; i++) {
// If arr[i].second is not
// equal to that of the group
if (arr[i].second != group) {
return "Yes";
}
}
// If array is sorted
if (isSorted(arr, N)) {
return "Yes";
}
else {
return "No";
}
}
// Driver Code
int main()
{
pair arr[]
= { { 340000, 2 }, { 45000, 1 },
{ 30000, 2 }, { 50000, 4 } };
int N = sizeof(arr) / sizeof(arr[0]);
cout << isPossibleToSort(arr, N);
return 0;
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to check if an
// array is sorted or not
static boolean isSorted(int[][] arr, int N)
{
// Traverse the array arr[]
for (int i = 1; i < N; i++) {
if (arr[i][0] > arr[i - 1][0]) {
return false;
}
}
// Return true
return true;
}
// Function to check if it is possible
// to sort the array w.r.t. first element
static String isPossibleToSort(int[][] arr, int N)
{
// Stores the ID of the first element
int group = arr[0][1];
// Traverse the array arr[]
for (int i = 1; i < N; i++) {
// If arr[i].second is not
// equal to that of the group
if (arr[i][1] != group) {
return "Yes";
}
}
// If array is sorted
if (isSorted(arr, N)) {
return "Yes";
}
else {
return "No";
}
}
// Driver Code
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));
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to check if an
# array is sorted or not
def isSorted(arr, N):
# Traverse the array arr[]
for i in range(1, N):
if (arr[i][0] > arr[i - 1][0]):
return False
# Return true
return True
# Function to check if it is possible
# to sort the array w.r.t. first element
def isPossibleToSort(arr, N):
# Stores the ID of the first element
group = arr[0][1]
# Traverse the array arr[]
for i in range(1, N):
# If arr[i][1] is not
# equal to that of the group
if (arr[i][1] != group):
return "Yes"
# If array is sorted
if (isSorted(arr, N)):
return "Yes"
else:
return "No"
# Driver Code
if __name__ == '__main__':
arr = [ [ 340000, 2 ], [ 45000, 1 ],[ 30000, 2 ], [ 50000, 4 ] ]
N = len(arr)
print (isPossibleToSort(arr, N))
# This code is contributted by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG {
// Function to check if an
// array is sorted or not
static bool isSorted(int[, ] arr, int N)
{
// Traverse the array arr[]
for (int i = 1; i < N; i++) {
if (arr[i, 0] > arr[i - 1, 0]) {
return false;
}
}
// Return true
return true;
}
// Function to check if it is possible
// to sort the array w.r.t. first element
static string isPossibleToSort(int[, ] arr, int N)
{
// Stores the ID of the first element
int group = arr[0, 1];
// Traverse the array arr[]
for (int i = 1; i < N; i++) {
// If arr[i].second is not
// equal to that of the group
if (arr[i, 1] != group) {
return "Yes";
}
}
// If array is sorted
if (isSorted(arr, N)) {
return "Yes";
}
else {
return "No";
}
}
// Driver Code
public static void Main()
{
int[, ] arr = { { 340000, 2 },
{ 45000, 1 },
{ 30000, 2 },
{ 50000, 4 } };
int N = arr.GetLength(0);
Console.WriteLine(isPossibleToSort(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live