Multiple occurrences
Practice
3.5 (79 votes)
Basic programming
Problem
92% Success 21282 Attempts 10 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an integer array \(A\). Your task is to calculate the sum of absolute difference of indices of first and last occurrence for every integer that is present in array \(A\).

Formally, if element \(x\) occurs \(m\) times in the array at indices \(B_1,\ B_2,\ B_3,\ ...,B_m\), then the answer for \(x\) will be \(B_m-B_1\) if array \(B\) is sorted.

You are required to calculate the sum of the answer for every such \(x\) that occurs in the array.

Refer to sample notes and explanations for better understanding.

Input format

  • The first line contains an integer \(T\) that denotes the number of test cases.
  • The first line of each test case contains an integer \(x\) that denotes the number of elements in the array.
  • The second line of each test case contains \(x\) space seperated integers \(A_1,\ A_2,\ A_3,\ ...,A_n\).

Output format

For each test case, print a single line as described in the problem statement.

Constraints

\(1 \leq T \leq 1000 \)

\(1 \leq N \leq 200000\)

\(1 \leq A_i \leq 1e9 \forall i \in [0,n-1]\)

The sum of \(N\) over all test cases will not exceed 200000.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:10
12 votes
Tags:
StringBasic ProgrammingImplementation
Points:10
116 votes
Tags:
Basic ProgrammingBasics of ImplementationImplementation
Points:10
273 votes