Ctrl K

Variety on the Left vs. the Right

Easy
You get an array values of length n. For each index i, cut the array right after it: the left part holds the elements at indices 0 through i, and the right part holds everything after i (it may be empty). Count how many **distinct** values the left part has and subtract the number of distinct values in the right part. Return an array of these n differences, one per index i.
Example 1
values=[4, 4, 7, 1] → [-2, -1, 1, 3]

At index 0 the left part {4} has one value and the right part {4, 7, 1} has three, so 1 − 3 = −2. At index 1 the left is still {4} and the right is {7, 1}: 1 − 2 = −1. Then 2 − 1 = 1 and 3 − 0 = 3.

Example 2
values=[9] → [1]

One value on the left, nothing on the right: 1 − 0 = 1.

Example 3
values=[2, 5, 2, 5, 2] → [-1, 0, 0, 1, 2]
Constraints
  • 1 ≤ len(values) ≤ 50
  • 1 ≤ values[i] ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasyThe Candidate With Most Votes EasyDoes Any Value Repeat EasyRepeat Within Reach EasyThe Lost Ticket Easy
loading Python…
Input
values=[4, 4, 7, 1]
Expected
[-2, -1, 1, 3]

Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.