从给定数组中找到 N-1 对 (X, Y),使得 X 和 Y 不同,并且 X 模 Y 未在数组中预设
给定一个大小为N的数组Arr[] ,由N对不同的正整数组成。任务是找到满足以下条件的N – 1 个不同的正整数X、Y对:
- X≠Y
- X、Y 都属于数组。
- X mod Y 不属于数组。
Note: It is proved that N-1 such pairs always exists.
例子:
Input: N = 4 , Arr [ ] = { 2 , 3 ,4 ,5 }
Output: (5 , 2) , (4 , 2) , (3 , 2)
Explanation: 4 – 1 = 3, hence 3 such pairs printed.
In the first pair 5 and 2 both belongs to the array, 5 not equals to 2 , 5 mod 2 does not belongs to the array.
Same is applicable for all the printed pairs.
Input: N = 2, Arr [ ] = { 1 , 3 }
Output: 3 , 1
Explanation: 2 – 1 = 1. Hence only one such pair exists. That is 3 , 1.
方法:上述问题可以使用贪心法来解决。在这种方法中,永远不要寻找所有可能的情况。相反,寻找我们需要的部分或数量。请按照以下步骤解决此问题:
- 将变量min_element初始化为Arr[0]。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 计算min_element的值作为数组Arr[] 的最小元素。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 如果Arr[i]不等于min_element,则打印Arr[i], min_element作为对。
这种方法基于以下观察:
Say X is the minimum element in the given array.
The value Arr[i] % X will always be less than X and no element in array is less than X
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the possible pairs
void find(int N, int Arr[])
{
int min_element = Arr[0];
// Get the minimum element
for (int i = 0; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
// Pair rest N - 1 elements with
// the minimum element and print
for (int i = 0; i < N; i++) {
if (Arr[i] != min_element) {
cout << Arr[i] << " " <<
min_element << endl;
}
}
}
// Driver Code
int main()
{
// Initialize N and the Array
int N = 4;
int Arr[4] = { 2, 3, 4, 5 };
find(N, Arr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Function to find the possible pairs
static void find(int N, int Arr[])
{
int min_element = Arr[0];
// Get the minimum element
for (int i = 0; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
// Pair rest N - 1 elements with
// the minimum element and print
for (int i = 0; i < N; i++) {
if (Arr[i] != min_element) {
System.out.println(Arr[i] + " " +
min_element);
}
}
}
// Driver Code
public static void main(String args[])
{
int N = 4;
int Arr[] = { 2, 3, 4, 5 };
find(N, Arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# python3 program for the above approach
# Function to find the possible pairs
def find(N, Arr):
min_element = Arr[0]
# Get the minimum element
for i in range(0, N):
if (Arr[i] < min_element):
min_element = Arr[i]
# Pair rest N - 1 elements with
# the minimum element and print
for i in range(0, N):
if (Arr[i] != min_element):
print(f"{Arr[i]} {min_element}")
# Driver Code
if __name__ == "__main__":
# Initialize N and the Array
N = 4
Arr = [2, 3, 4, 5]
find(N, Arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the possible pairs
static void find(int N, int []Arr)
{
int min_element = Arr[0];
// Get the minimum element
for (int i = 0; i < N; i++) {
if (Arr[i] < min_element) {
min_element = Arr[i];
}
}
// Pair rest N - 1 elements with
// the minimum element and print
for (int i = 0; i < N; i++) {
if (Arr[i] != min_element) {
Console.WriteLine(Arr[i] + " " +
min_element);
}
}
}
// Driver Code
public static void Main()
{
int N = 4;
int []Arr = { 2, 3, 4, 5 };
find(N, Arr);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
3 2
4 2
5 2
时间复杂度: O(N)
辅助空间: O(1)