Get Off-Campus Placement Jobs Info !!!
Programs asked in Mettl Coding Round
Click To Practce List of Programs asked in Nagarro !!!

sum of differences between positions of repeated elements of array

Home > Programs > Array

382 / 23

Given an array of integers with repeating elements, find the sum of differences between positions of repeated elements and store them in an array of the same size.

For e.g. given array {1, 2, 3, 1, 2, 1, 2} we shall have:
For position 0: 1 is on positions 3&5 additional to position 0.
Thus for position 0: |3-0| + |5-0| = 8.
For position 1: 2 is on positions 4&6 additional to position 1.
Thus for position 1: |4-1| + |6-1| = 8.

Answer:


Asked In :: SAP Labs TCS NQT

Post Your Answer Here: