Minimum Moves to Equal Array Elements II
Question link.
We will solve this question using only two case. First case when length of array is odd & second when length of array is even. Initially we will sort our array then then follow up with our below logic.
For odd case we will make all elements present left & right of middle element equal to the middle element. For example :
- [1, 2, 3] in this we make 1 & 3 equal to 2.
- [1, 2, 3, 4, 5] in this we make 1, 2 & 4, 5 equal to 3.
Related Post : Data Structures , LeetCode , Competitive Coding
Now coming for even. For this we calculate two sum considering left middle element & right middle element & just return middle of them. For example : [1, 2, 3, 4, 5, 6] in this we will calculate sum using 3 as middle element & 4 as middle element. Then return minimum of both the sum.
Similar Question : link to explanation
Other Related Post : Create CRUD website, Advance SEO settings
No comments:
If you have any doubt or suggestion let me know in comment section.