C++ program to find two points from two lines who are not same

C++Server Side ProgrammingProgramming

Suppose we have two ranges (l1, r1), (l2, r2) represents two lines on x-axis. l1 < r1 and l2 < r2. These segments may intersect, overlap or coincide with each other. We have to find two numbers a and b, such that a is in range (l1, r1) and b is in (l2, r2) and a and b are distinct.

So, if the input is like l1 = 2; r1 = 6; l2 = 3; r2 = 4, then the output will be a = 3, b = 4, other answers are also possible.

Steps

To solve this, we will follow these steps −

if l1 is same as l2, then:
   (increase l1 by 1)
return l1 and l2

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

void solve(int l1, int r1, int l2, int r2) {
   if (l1 == l2)
      l1++;
   cout << l1 << ", " << l2;
}
int main() {
   int l1 = 2;
   int r1 = 6;
   int l2 = 3;
   int r2 = 4;
   solve(l1, r1, l2, r2);
}

Input

2, 6, 3, 4

Output

2, 3
raja
Updated on 03-Mar-2022 06:58:04

Advertisements