Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data, Mining, And Analytics Components Of Strategic Decision Making

Authors: Stephan Kudyba

1st Edition

1466568704, 9781466568709

More Books

Students also viewed these Databases questions

Question

What is a job analysis?

Answered: 1 week ago

Question

What are the main provisions of the Fair Labor Standards Act?

Answered: 1 week ago