Find the Distinct Difference Array - Problem

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: nums[i, ..., j] denotes the subarray of nums starting at index i and ending at index j inclusive. If i > j, then nums[i, ..., j] denotes an empty subarray.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,4,1]
Output: [-2,-1,1,3,4]
💡 Note: At i=0: prefix {1} has 1 distinct, suffix {2,3,4,1} has 3 distinct → 1-3 = -2. At i=1: prefix {1,2} has 2 distinct, suffix {3,4,1} has 3 distinct → 2-3 = -1. At i=2: prefix {1,2,3} has 3 distinct, suffix {4,1} has 2 distinct → 3-2 = 1. At i=3: prefix {1,2,3,4} has 4 distinct, suffix {1} has 1 distinct → 4-1 = 3. At i=4: prefix {1,2,3,4,1} has 4 distinct, suffix {} has 0 distinct → 4-0 = 4.
Example 2 — All Same Elements
$ Input: nums = [3,3,3]
Output: [0,0,1]
💡 Note: At i=0: prefix {3} has 1 distinct, suffix {3,3} has 1 distinct → 1-1 = 0. At i=1: prefix {3,3} has 1 distinct, suffix {3} has 1 distinct → 1-1 = 0. At i=2: prefix {3,3,3} has 1 distinct, suffix {} has 0 distinct → 1-0 = 1.
Example 3 — No Duplicates
$ Input: nums = [1,2,3]
Output: [-1,1,3]
💡 Note: At i=0: prefix {1} has 1 distinct, suffix {2,3} has 2 distinct → 1-2 = -1. At i=1: prefix {1,2} has 2 distinct, suffix {3} has 1 distinct → 2-1 = 1. At i=2: prefix {1,2,3} has 3 distinct, suffix {} has 0 distinct → 3-0 = 3.

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 50

Visualization

Tap to expand
Distinct Difference Array INPUT nums array (0-indexed) 1 i=0 2 i=1 3 i=2 4 i=3 1 i=4 For each index i: Prefix: nums[0...i] Elements from start to i Suffix: nums[i+1...n-1] Elements after i to end diff[i] = prefix_dist - suffix_dist n = 5, length of array ALGORITHM STEPS 1 Build Suffix Counts Hash all elements right-to-left suffix_set = {1,2,3,4} initially suffix_count[i] = distinct after i [4, 3, 2, 1, 0] (right to left) 2 Build Prefix Counts Hash elements left-to-right prefix_set starts empty {} prefix_count: [1,2,3,4,4] 3 Calculate Differences diff[i] = prefix[i] - suffix[i] i=0: 1-4 = -3? No, suffix[i+1]! i=0: 1-3 = -2 (suffix after 0) i=4: 4-0 = 4 (no suffix left) 4 Return Result Array Time: O(n), Space: O(n) FINAL RESULT Calculation for each index: i Pref Suff diff[i] 0 1 3 -2 1 2 3 -1 2 3 1 2 3 4 1 3 4 4 0 4 Output Array: -2 -1 2 3 4 OK - Verified! [-2, -1, 2, 3, 4] Key Insight: Hash Set for O(1) Distinct Counting Using hash sets allows us to track distinct elements efficiently. We precompute suffix distinct counts in one pass (right-to-left), then compute prefix counts while calculating differences (left-to-right). This two-pass approach achieves O(n) time with O(n) space complexity. TutorialsPoint - Find the Distinct Difference Array | Hash Approach
Asked in
Google 15 Amazon 10 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
421 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen