You are given an integer array
. 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
Formally, if element
times in the array at indices
, then the answer for
You are required to calculate the sum of the answer for every such
that occurs in the array.
Refer to sample notes and explanations for better understanding.
- The first line contains an integer that denotes the number of test cases.
- The first line of each test case contains an integer that denotes the number of elements in the array.
- The second line of each test case contains space seperated integers .
For each test case, print a single line as described in the problem statement.
The sum of
over all test cases will not exceed 200000.
The elements which occur in the array are 1,2,3.
Let us consider 1 it has only one occurrence so answer for 1 is 0.
Let us consider 2 it has two occurrence at 2 and 5 so |5-2|=3
Let us consider 3 it has two occurrence at 3 and 4 so |4-3|=1.
So total sum=0+3+1=4.
If there are more than two occurrence we only need to consider the first and last.
Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also
Solution 1 ( C language):-
Solution 2 ( C++ language):-
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .
- Very Cool numbers | Hacker earth solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree