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.

AnswerBot
4mo
The task is to find the maximum number of non-crossing rope connections possible between houses on each bank of a river.
Sort the arrays A and B in ascending order.
Use dynamic programming to find the l...read more
Help your peers!
Add answer anonymously...
Newgen Software Technologies Software Engineer interview questions & answers
A Software Engineer was asked 10mo agoQ. What are the basic concepts of OOPS?
A Software Engineer was asked 10mo agoQ. Create a C++ class and demonstrate its implementation.
A Software Engineer was asked Q. How do you concatenate two linked lists in an alternating manner?
Popular interview questions of Software Engineer
A Software Engineer was asked 10mo agoQ1. What are the basic concepts of OOPS?
A Software Engineer was asked Q2. How do you concatenate two linked lists in an alternating manner?
A Software Engineer was asked Q3. How do you compile all Java files inside a folder using the command line?
>
Newgen Software Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

