Make all elements of the array distinct.
You have been given an array/list ‘ARR’ of integers consisting of ‘N’ integers. In one operation you can increase an element by 1. Your task is to return the minimum number of operations to make the array distinct.
Example:
Let’s say you have an array/list [1,4,4,5]. We can increase the third element by 1 and the fourth element by 1. Finally, our array will look like [1,4,5,6] where all elements are distinct. Therefore the minimum number of operations is 2.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’ representing the size of the array/list ‘ARR’.
The second line and the last line of input contain ‘N’ single space-separated integers representing the array/list elements.
Output Format:
For each test case, return the minimum number of operations to make the array distinct.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= N <= 10^3
1 <= ‘ARR[i]’ <= 10^4
Time Limit: 1 sec
CodingNinjas
author
2y
Steps:
1) The idea is to sort the input , then we move forward from the beginning of the array till the end.
2) As soon as we found a condition that the current element is less than or equal to the prev...read more
CodingNinjas
author
2y
Maintaining Count of Each Element.
We will maintain the count of each element present in an auxiliary array. Finally, each element must have at most one occurrence.
We will apply the algorithm as follow...read more
CodingNinjas
author
2y
Sorting
We will sort the array/list ‘ARR’. As we can only apply the increment operation we just need to make the array/list increasing.
Following is the algorithm for this approach:
- Sort ‘ARR’.
- Declare ‘...read more
Add answer anonymously...
Top Walmart Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Walmart Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app