Class Solution
java.lang.Object
g2601_2700.s2670_find_the_distinct_difference_array.Solution
2670 - Find the Distinct Difference Array.<p>Easy</p>
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>distinct difference</strong> array of <code>nums</code> is an array <code>diff</code> of length <code>n</code> such that <code>diff[i]</code> is equal to the number of distinct elements in the suffix <code>nums[i + 1, ..., n - 1]</code> <strong>subtracted from</strong> the number of distinct elements in the prefix <code>nums[0, ..., i]</code>.</p>
<p>Return <em>the <strong>distinct difference</strong> array of</em> <code>nums</code>.</p>
<p>Note that <code>nums[i, ..., j]</code> denotes the subarray of <code>nums</code> starting at index <code>i</code> and ending at index <code>j</code> inclusive. Particularly, if <code>i > j</code> then <code>nums[i, ..., j]</code> denotes an empty subarray.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [1,2,3,4,5]</p>
<p><strong>Output:</strong> [-3,-1,1,3,5]</p>
<p><strong>Explanation:</strong></p>
<p>For index i = 0, there is 1 element in the prefix and 4 distinct elements in the suffix. Thus, diff[0] = 1 - 4 = -3.</p>
<p>For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.</p>
<p>For index i = 2, there are 3 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 3 - 2 = 1.</p>
<p>For index i = 3, there are 4 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 4 - 1 = 3.</p>
<p>For index i = 4, there are 5 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 5 - 0 = 5.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [3,2,3,4,2]</p>
<p><strong>Output:</strong> [-2,-1,0,2,3]</p>
<p><strong>Explanation:</strong></p>
<p>For index i = 0, there is 1 element in the prefix and 3 distinct elements in the suffix. Thus, diff[0] = 1 - 3 = -2.</p>
<p>For index i = 1, there are 2 distinct elements in the prefix and 3 distinct elements in the suffix. Thus, diff[1] = 2 - 3 = -1.</p>
<p>For index i = 2, there are 2 distinct elements in the prefix and 2 distinct elements in the suffix. Thus, diff[2] = 2 - 2 = 0.</p>
<p>For index i = 3, there are 3 distinct elements in the prefix and 1 distinct element in the suffix. Thus, diff[3] = 3 - 1 = 2.</p>
<p>For index i = 4, there are 3 distinct elements in the prefix and no elements in the suffix. Thus, diff[4] = 3 - 0 = 3.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == nums.length <= 50</code></li>
<li><code>1 <= nums[i] <= 50</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
distinctDifferenceArray
public int[] distinctDifferenceArray(int[] nums)
-