Full Explanation || No HashMap || Simplest Approach
Intuition\n\nNo hashmap Simple Iteration\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. The problem asks to find the distinct d
# Intuition\n\nNo hashmap Simple Iteration\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. The problem asks to find the distinct difference array of a given array "nums". The distinct difference array is an array of the same length as "nums", where each element "diff[i]" is calculated by subtracting the number of distinct elements in the suffix of "nums" (i.e., elements from index i+1 to n-1) from the number of distinct elements in the prefix of "nums" (i.e., elements from index 0 to i).\n 2. To solve the problem, the above code defines a class "Solution" with a public method "distinctDifferenceArray" that takes an integer array "nums" as input and returns an integer array "diff". \n 3. The "distinctDifferenceArray" method loops through each index of the "nums" array and calculates the difference between the number of distinct elements in the prefix and the number of distinct elements in the suffix, and stores the result in the "diff" array. \n 4. To count the number of distinct elements in a given subarray of "nums", the code defines a helper method "countDistinctElements" that takes an integer array "nums", a starting index "start", and an ending index "end" as input, and returns the count of distinct elements in the subarray from index "start" to index "end". \n 5. The method uses a boolean array "visited" to keep track of the elements that have been visited before. Overall, the code solves the problem by calculating the distinct difference array for the given "nums" array, where each element in the array is calculated using the "countDistinctElements" helper method to count the number of distinct elements in the prefix and suffix of the current element.\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int[] distinctDifferenceArray(int[] nums) {\n      \n        int n = nums.length;\n        int[] diff = new int[n];\n        \n        // iterate through each index\n        for (int i = 0; i < n; i++) {\n            int countPrefix = countDistinctElements(nums, 0, i);\n            int countSuffix = countDistinctElements(nums, i + 1, n - 1);\n            diff[i] = countPrefix - countSuffix;\n        }\n        \n        return diff;\n    }\n    \n    private static int countDistinctElements(int[] nums, int start, int end) {\n        boolean[] visited = new boolean[100001]; // assuming values are less than 100001\n        int count = 0;\n        for (int i = start; i <= end; i++) {\n            if (!visited[nums[i]]) {\n                visited[nums[i]] = true;\n                count++;\n            }\n        }\n        return count;\n}}\n```\n![images.jpeg](https://assets.leetcode.com/users/images/22b6e345-a194-4819-9449-bed0b0eb321b_1683435836.4436464.jpeg)\n
['Java']