📌  相关文章
📜  从两个范围中选择点,使得两个范围内都没有点

📅  最后修改于: 2021-10-26 05:39:05             🧑  作者: Mango

给定两个片段[L1, R1][L2, R2] ,任务是从两个范围(一个来自范围一,另一个来自范围二)中选择两个元素xy ,这样没有元素属于这两个范围,即x属于第一个范围, y属于第二个范围。如果不存在这样的元素,则改为打印-1

例子:

方法:

  • 如果L1 != L2R1 != R2那么点将是min(L1, L2)max(R1, R2)
  • 否则只能从一个范围中选择一个点,因为其中一个范围完全在另一个范围内,因此我们为该点打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the required points
void findPoints(int l1, int r1, int l2, int r2)
{
 
    int x = (l1 != l2) ? min(l1, l2) : -1;
    int y = (r1 != r2) ? max(r1, r2) : -1;
    cout << x << " " << y;
}
 
// Driver code
int main()
{
    int l1 = 5, r1 = 10, l2 = 1, r2 = 7;
    findPoints(l1, r1, l2, r2);
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to find the required points
static void findPoints(int l1, int r1,
                       int l2, int r2)
{
 
    int x = (l1 != l2) ? Math.min(l1, l2) : -1;
    int y = (r1 != r2) ? Math.max(r1, r2) : -1;
    System.out.println(x + " " + y);
}
 
// Driver code
public static void main(String[] args)
{
    int l1 = 5, r1 = 10, l2 = 1, r2 = 7;
    findPoints(l1, r1, l2, r2);
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
 
# Function to find the required points
def findPoints(l1, r1, l2, r2):
 
    x = min(l1, l2) if(l1 != l2) else -1
    y = max(r1, r2) if(r1 != r2) else -1
    print(x, y)
 
# Driver code
if __name__ == "__main__":
     
    l1 = 5
    r1 = 10
    l2 = 1
    r2 = 7
    findPoints(l1, r1, l2, r2)
 
# This code is contributed by ita_c


C#
// C# implementation of the approach
using System;
 
class GFG
{
    // Function to find the required points
    static void findPoints(int l1, int r1,
                            int l2, int r2)
    {
        int x = (l1 != l2) ? Math.Min(l1, l2) : -1;
        int y = (r1 != r2) ? Math.Max(r1, r2) : -1;
        Console.WriteLine(x + " " + y);
    }
     
    // Driver code
    public static void Main()
    {
        int l1 = 5, r1 = 10, l2 = 1, r2 = 7;
        findPoints(l1, r1, l2, r2);
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
1 10

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程