Skip to main content

Command Palette

Search for a command to run...

Find the Distinct Difference Array

Published
2 min read
Find the Distinct Difference Array
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 a 0-indexed array nums of length n.

The distinct difference array of nums is an array diff of length n such that diff[i] is equal to the number of distinct elements in the suffix nums[i + 1, ..., n - 1] subtracted from the number of distinct elements in the prefix nums[0, ..., i].

Return the distinct difference array of nums.

Note that nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j inclusive. Particularly, if i > j then nums[i, ..., j] denotes an empty subarray.

LeetCode Problem - 2670

class Solution {
    // Method to calculate distinct difference array
    public int[] distinctDifferenceArray(int[] nums) {
        int[] answer = new int[nums.length]; // Initialize array to store results

        // Iterate through each element in the input array
        for(int i=0; i<nums.length; i++){
            // Calculate distinct elements in the prefix (from start to current index)
            int prefix = findDistinctElement(Arrays.copyOfRange(nums, 0, i+1));
            // Calculate distinct elements in the suffix (from current index to end)
            int suffix = findDistinctElement(Arrays.copyOfRange(nums, i+1, nums.length));
            // Store the difference of distinct elements between prefix and suffix
            answer[i] = prefix - suffix;
        }
        return answer; // Return the resulting array
    }

    // Method to find number of distinct elements in an array
    public int findDistinctElement(int[] arr){
        List<Integer> al = new ArrayList<>(); // Create a list to store distinct elements
        int count = 0; // Initialize count of distinct elements

        // Iterate through the array
        for(int i=0; i<arr.length; i++){
            if(!al.contains(arr[i])){ // Check if element is not already in list
                al.add(arr[i]); // Add element to list
                count++; // Increment count of distinct elements
            }
        }
        return count; // Return total count of distinct elements
    }
}

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.