📌  相关文章
📜  从给定数组中找到 N-1 对 (X, Y),使得 X 和 Y 不同,并且 X 模 Y 未在数组中预设

📅  最后修改于: 2022-05-13 01:56:08.652000             🧑  作者: Mango

从给定数组中找到 N-1 对 (X, Y),使得 X 和 Y 不同,并且 X 模 Y 未在数组中预设

给定一个大小为N的数组Arr[] ,由N对不同的正整数组成。任务是找到满足以下条件的N – 1 个不同的正整数X、Y对:

  • X≠Y
  • X、Y 都属于数组。
  • X mod Y 不属于数组。

例子:

方法:上述问题可以使用贪心法来解决。在这种方法中,永远不要寻找所有可能的情况。相反,寻找我们需要的部分或数量。请按照以下步骤解决此问题:

  • 将变量min_element初始化为Arr[0]。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • 计算min_element的值作为数组Arr[] 的最小元素。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • 如果Arr[i]不等于min_element,则打印Arr[i], min_element作为对。

这种方法基于以下观察:

下面是上述方法的实现:

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)