You have a bundle of N sticks, the ith stick has a length Li meters. Find the minimum number of sticks you need to add to the bundle such that you can construct some rectangles and each stick of the bundle belongs to exactly one rectangle.
EXPLANATION:
Think of the problem as a combination of 2 tasks:
Pairing every length Li with an equal length.
Ensuring that the total number of sticks after adding the requisite number to complete all the pairs is a multiple of 4.
A rectangle contains pairs of equal and opposite sides. So every length should be present even number of times. Once every length is paired, it has to be ensured that the total number of sides are a multiple of 4. This can be done by simply adding the required number of pairs.
TIME COMPLEXITY:
The above solution required the program to iterate over the given array atleast once. Hence, the solution has a time complexity of O(N) where N is the size of the array.
this will be the scenerio:
adding one stick of length 4 will make a square (rectangle with all sides equal)
adding one stick of length 1 and 3 each will make another rectangle
so answer should be 3
it is still consistent with editorial
I think you have this doubt
when the freq of any ele is odd we added the sticks to make them even
now total number of sticks will be even
if we make some rectangles
remaining sticks will be even
example:
CASE 1: add2
n=4
1 2 3 3
(1 1 2 2 3 3)
total number of string is not multiple of 4
we add 2 here
CASE2: add0
n=4
1 2 3 4
we added the sticks(1 2 3 4)
(1 1 2 2 3 3 4 4)
total number of sticks is multiple of 4
we don’t add 2 here
no you can’t do that
i also had this doubt initially
but then i read this line in the problem statement each side of a rectangle should be formed with exactly one stick.
what if we have all the sticks of same length.
like we have 4 sticks of length 3 3 3 3 ;
the output according to the given solution is coming out to be zero, but it must be 4, since they we will be requiring 2 pair of sticks of different lengths