Skip to main content

Command Palette

Search for a command to run...

Minimum Increment to Make Array Unique

Published
1 min read
Minimum Increment to Make Array Unique
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.

Return the minimum number of moves to make every value in nums unique.

The test cases are generated so that the answer fits in a 32-bit integer.

LeetCode Problem - 945

class Solution {
    public int minIncrementForUnique(int[] nums) {
        int ans = 0; // Initialize the answer variable to track total increments
        Arrays.sort(nums); // Sort the input array to simplify the process

        // Iterate through the array starting from the second element
        for (int i = 1; i < nums.length; i++) {
            // If current number is less than or equal to the previous number
            if (nums[i] <= nums[i - 1]) {
                // Calculate how much to increment the current number
                ans += nums[i - 1] - nums[i] + 1;
                // Increment the current number to make it unique
                nums[i] = nums[i - 1] + 1;
            }
        }

        return ans; // Return the total number of increments needed
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.