Question: There are N rectangles numbered from 0 to N - 1 . The K - th rectangle has size A [ K ] x B

There are N rectangles numbered from 0 to N-1. The K-th rectangle has size A[K] x B[K]
We call two rectangles similar when:
they share a side of the same length, and
the length of the other sides differs by at most 1.
For example:
rectangles 2x3 and 2x4 are similar,
rectangles 2x3 and 4x2 are also similar (rectangles can be rotated),
rectangles 2x3 and 2x5 are not similar (other sides differ by 2).
rectangles 2x2 and 3x3 are not similar either (more than one side differing in length).
The task is to choose as many rectangles as possible in such a way that every two chosen rectangles are similar to each other. Note that rectangles can be rotated.
Write a function:
class Solution { public int solution(int[] A, int[] B); }
that, given two arrays A and B of N integers each, returns the maximum number of similar rectangles that can be chosen.
Examples:
A 1. Given A (500,500) and B (500,500), the function should return 2. Both rectangles can be chosen.
2. Given A(5,5,1,6,4) and B (2,1.6,1,1] the function should return 3. The best choice is to take the rectangles in positions 1 to 3. After rotation, their sizes are respectively 5x1,61 and 6x1.
3. Given A (3,3,3,3,5,7,1,1) and B (4,6,8,10,10,10,2,3) the function should return 2. The optimal choice is to take last two rectangles (sizes 1x2 and 1x3)
4. Given A (4,4,2,2,2,4,4) and B =13,4,6,6,6,3,4] the function should return 4. The best choice is to take the first two and the last two rectangles (sizes 3x4,4x4,3x4 and 4x4).
Write an efficient algorithm for the following assumptions
N is an integer within the range [1..200,000];
arrays A and B consist of N integers within the range [1..500]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!