Maximum Non-Crossing Ropes Problem

In a village with a river at its center, there are ‘N’ houses on each bank of the river. The northern bank’s houses have distinct X-coordinates given by an array A[1], A[2], ..., A[N] and the southern bank’s houses have distinct X-coordinates given by an array B[1], B[2], ..., B[N]. Ninja wants to connect the houses on each bank without the ropes crossing each other. A rope can connect house A[i] with house B[i]. The task is to find the maximum number of such non-crossing rope connections possible.

Input:

The first line contains an integer 'T' representing the number of test cases. Followed by descriptions of each test case:
- An integer ‘N’, the number of houses on each bank.
- N space-separated integers representing the coordinates of the northern houses (array 'A').
- N space-separated integers representing the coordinates of the southern houses (array 'B').

Output:

For each test case, output an integer that represents the maximum number of non-crossing connections Ninja can make.
Print the results for each test case on a new line.

Example:

Input:
N = 3
A = [1, 2, 3]
B = [2, 1, 3]

Output:
2

Explanation:
One of the possible connections is (1, 2) and (3, 3). Another valid configuration is (2, 1) and (3, 3).
With these configurations, two pairs of ropes can be placed without crossing. A complete connection of all 3 pairs (1, 2), (2, 1) and (3, 3) is not possible without crossing.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 10^5
  • 1 <= A[i] <= 10^5
Note:
Your task is to implement the function correctly; no need to print anything explicitly as it's handled elsewhere.
Be the first one to answer
Add answer anonymously...
Newgen Software Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter