Sum of absolute differences of indices of occurrences of each array element | Set 2
Given an array, arr[] consisting of N integers, the task for each array element arr[i] is to print the sum of |i – j| for all possible indices j such that arr[i] = arr[j].
Examples:
Input: arr[] = {1, 3, 1, 1, 2}
Output: 5 0 3 4 0
Explanation:
For arr[0], sum = |0 – 0| + |0 – 2| + |0 – 3| = 5.
For arr[1], sum = |1 – 1| = 0.
For arr[2], sum = |2 – 0| + |2 – 2| + |2 – 3| = 3.
For arr[3], sum = |3 – 0| + |3 – 2| + |3 – 3| = 4.
For arr[4], sum = |4 – 4| = 0.
Therefore, the required output is 5 0 3 4 0.
Input: arr[] = {1, 1, 1}
Output: 3 2 3
Naive Approach: Please refer to the previous post of this article for the simplest approach to solve the problem.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Map-based Approach: Please refer to the previous post of this article to solve the problem using Map.
Time Complexity: O(N*L)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by storing the previous index and the count of occurrences of every element in a HashMap. Follow the steps below to solve the problem:
- Initialize a HashMap, say M to store arr[i] as key and (count, previous index) as value.
- Initialize two arrays L[] and R[] of size N where L[i] denotes the sum of |i – j| for all possible indices j < i and arr[i] = arr[j] and R[i] denotes the sum of |i – j| for all possible indices j > i and arr[i] = arr[j].
- Traverse the given array arr[] over the range [0, N – 1] and perform the following steps:
- If arr[i] is present in the map M, then update the value of L[i] to 0 and M[arr[i]] to store pair {1, i} where the first element denotes the count of occurrences and the second element denotes the previous index of the element.
- Otherwise, find the value of arr[i] from the map M, and store the count and previous index in variables cnt and j respectively.
- Update the value of L[i] to (cnt * (i – j) + L[j]) and the value of arr[i] in M to store pair (cnt + 1, i).
- Repeat the same process to update the values in the array R[].
- Iterate over the range [0, N – 1] using the variable i and print the value (L[i] + R[i]) as the result.
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
#include <map>
using namespace std;
void findSum( int arr[], int n)
{
map< int , pair< int , int > > map;
int left[n], right[n];
for ( int i = 0; i < n; i++) {
if (map.count(arr[i]) == 0) {
left[i] = 0;
map[arr[i]] = make_pair(1, i);
}
else {
pair< int , int > tmp = map[arr[i]];
left[i] = (tmp.first) * (i - tmp.second)
+ left[tmp.second];
map[arr[i]] = make_pair(tmp.first + 1, i);
}
}
map.clear();
for ( int i = n - 1; i >= 0; i--) {
if (map.count(arr[i]) == 0) {
right[i] = 0;
map[arr[i]] = make_pair(1, i);
}
else {
pair< int , int > tmp = map[arr[i]];
right[i] = (tmp.first) * ( abs (i - tmp.second))
+ right[tmp.second];
map[arr[i]] = make_pair(tmp.first + 1, i);
}
}
for ( int i = 0; i < n; i++)
cout << left[i] + right[i] << " " ;
}
int main()
{
int arr[] = { 1, 3, 1, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
findSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class pair {
int count, prevIndex;
pair( int count, int prevIndex)
{
this .count = count;
this .prevIndex = prevIndex;
}
}
static void findSum( int [] arr, int n)
{
Map<Integer, pair> map = new HashMap<>();
int [] left = new int [n];
int [] right = new int [n];
for ( int i = 0 ; i < n; i++) {
if (!map.containsKey(arr[i])) {
left[i] = 0 ;
map.put(arr[i], new pair( 1 , i));
}
else {
pair tmp = map.get(arr[i]);
left[i] = (tmp.count)
* (i - tmp.prevIndex)
+ left[tmp.prevIndex];
map.put(
arr[i], new pair(
tmp.count + 1 , i));
}
}
map.clear();
for ( int i = n - 1 ; i >= 0 ; i--) {
if (!map.containsKey(arr[i])) {
right[i] = 0 ;
map.put(arr[i], new pair( 1 , i));
}
else {
pair tmp = map.get(arr[i]);
right[i]
= (tmp.count)
* (Math.abs(i - tmp.prevIndex))
+ right[tmp.prevIndex];
map.put(
arr[i], new pair(
tmp.count + 1 , i));
}
}
for ( int i = 0 ; i < n; i++)
System.out.print(
left[i] + right[i] + " " );
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 1 , 1 , 2 };
int N = arr.length;
findSum(arr, N);
}
}
|
Python3
class pair:
def __init__( self , count,prevIndex):
self .count = count;
self .prevIndex = prevIndex;
def findSum(arr,n):
map = {};
left = [ 0 for i in range (n)];
right = [ 0 for i in range (n)];
for i in range (n):
if (arr[i] not in map ):
left[i] = 0 ;
map [arr[i]] = pair( 1 , i);
else :
tmp = map [arr[i]];
left[i] = (tmp.count) * (i - tmp.prevIndex) + left[tmp.prevIndex]
map [arr[i]] = pair( tmp.count + 1 , i);
map .clear();
for i in range (n - 1 , - 1 , - 1 ):
if (arr[i] not in map ):
right[i] = 0 ;
map [arr[i]] = pair( 1 , i);
else :
tmp = map [arr[i]];
right[i] = (tmp.count) * ( abs (i - tmp.prevIndex)) + right[tmp.prevIndex];
map [arr[i]] = pair(tmp.count + 1 , i);
for i in range (n):
print (left[i] + right[i], end = " " );
arr = [ 1 , 3 , 1 , 1 , 2 ];
N = len (arr);
findSum(arr, N);
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
class pair {
public int count, prevIndex;
public pair( int count, int prevIndex)
{
this .count = count;
this .prevIndex = prevIndex;
}
}
static void findSum( int [] arr, int n)
{
Dictionary< int , pair> map = new Dictionary< int , pair>();
int [] left = new int [n];
int [] right = new int [n];
for ( int i = 0; i < n; i++) {
if (!map.ContainsKey(arr[i])) {
left[i] = 0;
map.Add(arr[i], new pair(1, i));
}
else {
pair tmp = map[arr[i]];
left[i] = (tmp.count)
* (i - tmp.prevIndex)
+ left[tmp.prevIndex];
map[arr[i]] = new pair(
tmp.count + 1, i);
}
}
map.Clear();
for ( int i = n - 1; i >= 0; i--) {
if (!map.ContainsKey(arr[i])) {
right[i] = 0;
map.Add(arr[i], new pair(1, i));
}
else {
pair tmp = map[arr[i]];
right[i]
= (tmp.count)
* (Math.Abs(i - tmp.prevIndex))
+ right[tmp.prevIndex];
map[arr[i]] = new pair(
tmp.count + 1, i);
}
}
for ( int i = 0; i < n; i++)
Console.Write(
left[i] + right[i] + " " );
}
public static void Main(String[] args)
{
int [] arr = { 1, 3, 1, 1, 2 };
int N = arr.Length;
findSum(arr, N);
}
}
|
Javascript
<script>
class pair
{
constructor(count,prevIndex)
{
this .count = count;
this .prevIndex = prevIndex;
}
}
function findSum(arr,n)
{
let map = new Map();
let left = new Array(n);
let right = new Array(n);
for (let i = 0; i < n; i++) {
if (!map.has(arr[i])) {
left[i] = 0;
map.set(arr[i], new pair(1, i));
}
else {
let tmp = map.get(arr[i]);
left[i] = (tmp.count)
* (i - tmp.prevIndex)
+ left[tmp.prevIndex];
map.set(
arr[i], new pair(
tmp.count + 1, i));
}
}
map.clear();
for (let i = n - 1; i >= 0; i--) {
if (!map.has(arr[i])) {
right[i] = 0;
map.set(arr[i], new pair(1, i));
}
else {
let tmp = map.get(arr[i]);
right[i]
= (tmp.count)
* (Math.abs(i - tmp.prevIndex))
+ right[tmp.prevIndex];
map.set(
arr[i], new pair(
tmp.count + 1, i));
}
}
for (let i = 0; i < n; i++)
document.write(
left[i] + right[i] + " " );
}
let arr=[1, 3, 1, 1, 2];
let N = arr.length;
findSum(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
22 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...