Approach
Consensus
- Given an array , and a permutation array , both have the same size of
- Then we perform operation of the 2 given arrays, our goal is to have the highest number of elements that have the same value after the operation
Property of The Given Permutation
An array consisting of distinct integers from 1 to 𝑛 in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
Idea
-
First we can remove all the duplicate values in the given , because each is unique, so the element with same value in the given will 100% end up with a different value. And we only want to find the highest occurrence of one value. The duplicate doesn’t help us to do so, so we can just remove them to make the problem simpler
-
Ok, so what is next? How are we going to be sure that after and operations, .
-
First, lets be sure that both and are going to be in the range and inclusive, , and
-
Assume , if , then
-
Since and , so
-
So we can conclude that for any two elements from the given , their absolute difference is . The lower bound is guaranteed to be , since
-
So what is next? With the above information, how do we obtain the the answer? The next step is to sort the from smallest to biggest to obtain the Optimal Substructure (最优子结构), values that can be the same are arranged in a continuous manner. It makes implementation much easier
-
So now, we just need to find the longest subarray in the maximum value - minimum value . And since we sorted , the maximum value is the element at the most right side of the subarray and the minimum value is the element at the most left side of the subarray
-
Now imagine we explore the next element in , and realise the most right element - the most left element . What should we do? Should we call it a day? The answer is no, we should throw away elements from left hand side until the difference of the maximum value and minimum value of the subarray meet again, because we may have a a lot of elements on the right hand side that can fulfil
-
So just keep expanding the subarray to the right side and shrink from left if the sub-array fails , and record down the longest subarray until we finish go through the entire
-
We make use of Two-Pointers Sliding Window to achieve this behaviour in a in
Conclusion
- We first remove all the duplicate values inside since they don’t attribute to the final answer
- Then sort the remaining elements in from smallest to biggest to have the optimal substructure
- Then we make use of sliding window to find the longest sub-array in , and the size of the longest sub-array is the answer
Space & Time Analysis
The analysis method we are using is Algorithm Complexity Analysis
Space - O(1)
- Ignore input size & language dependent space
- Sliding windows take space
Time - O(nlogn)
- The sorting takes
Codes
1st Attempt (Java)
Personal Reflection
- Why it takes so long to solve: Unable to figure out what requirements the elements in the need to fulfil to become the same after adding elements from
- What you could have done better: Abstract the problem and constraints into math model
- What you missed: How the final same values can be derived logically
- Ideas you’ve seen before: Make use of sliding window to find the longest subarray and greedy approach
- Ideas you found here that could help you later: Abstract problems and constraints into math model
- Ideas that didn’t work and why: Sort the and see how to create elements that are the same with , but unable to proceed, because didn’t have clues to proceed, making a math model will make it much easier