Maximum absolute difference between distinct elements in an Array
Given an array arr[] of N integers, the task is to find the maximum absolute difference between distinct elements of the array.
Examples:
Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45, 10}
Output: 10
Explanation:
Distinct elements of given array are 12, 9, 2.
Therefore, the maximum absolute difference between them is (12 – 2) = 10.
Input: arr[] = {2, -1, 10, 3, -2, -1, 10}
Output: 5
Explanation:
Distinct elements of given array are 2, 3, -2.
Therefore, the maximum absolute difference between them is (3 – (-2)) = 5.
Naive Approach: The naive approach is to store the distinct element in the given array in an array temp[] and print the difference of maximum and minimum element of the array temp[].
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The above naive approach can be optimized using Hashing. Below are the steps:
- Store the frequency of each element of the array arr[] in a HashMap.
- Now find the maximum and minimum value of the array whose frequency is 1 using the above HashMap created.
- Print the difference between the maximum and minimum value obtained in the above step.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaxAbsDiff( int arr[], int n)
{
unordered_map< int , int > map;
int maxElement = INT_MIN;
int minElement = INT_MAX;
for ( int i = 0; i < n; i++)
{
map[arr[i]]++;
}
for ( auto itr = map.begin();
itr != map.end(); itr++)
{
if (itr -> second == 1)
{
maxElement = max(maxElement,
itr -> first);
minElement = min(minElement,
itr -> first);
}
}
return abs (maxElement - minElement);
}
int main()
{
int arr[] = { 12, 10, 9, 45, 2,
10, 10, 45, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << MaxAbsDiff(arr, n) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int MaxAbsDiff( int [] arr, int n)
{
Map<Integer, Integer> map
= new HashMap<>();
int maxElement = Integer.MIN_VALUE;
int minElement = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
map.put(arr[i],
map.getOrDefault(arr[i], 0 )
+ 1 );
}
for (Map.Entry<Integer, Integer> k :
map.entrySet()) {
if (k.getValue() == 1 ) {
maxElement
= Math.max(maxElement,
k.getKey());
minElement
= Math.min(minElement,
k.getKey());
}
}
return Math.abs(maxElement
- minElement);
}
public static void main(String[] args)
{
int [] arr = { 12 , 10 , 9 , 45 , 2 ,
10 , 10 , 45 , 10 };
int n = arr.length;
System.out.println(MaxAbsDiff(arr, n));
}
}
|
Python3
import sys
from collections import defaultdict
def MaxAbsDiff(arr, n):
map = defaultdict ( int )
maxElement = - sys.maxsize - 1
minElement = sys.maxsize
for i in range (n):
map [arr[i]] + = 1
for k in map :
if ( map [k] = = 1 ):
maxElement = max (maxElement, k)
minElement = min (minElement, k)
return abs (maxElement - minElement)
if __name__ = = "__main__" :
arr = [ 12 , 10 , 9 , 45 , 2 ,
10 , 10 , 45 , 10 ]
n = len ( arr)
print (MaxAbsDiff(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MaxAbsDiff( int [] arr, int n)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
int maxElement = int .MinValue;
int minElement = int .MaxValue;
for ( int i = 0; i < n; i++)
{
if (map.ContainsKey(arr[i]))
map[arr[i]] = map[arr[i]] + 1;
else
map.Add(arr[i], 1);
}
foreach (KeyValuePair< int , int > k in map)
{
if (k.Value == 1)
{
maxElement = Math.Max(maxElement,
k.Key);
minElement = Math.Min(minElement,
k.Key);
}
}
return Math.Abs(maxElement - minElement);
}
public static void Main(String[] args)
{
int [] arr = { 12, 10, 9, 45, 2,
10, 10, 45, 10 };
int n = arr.Length;
Console.WriteLine(MaxAbsDiff(arr, n));
}
}
|
Javascript
<script>
function MaxAbsDiff(arr, n)
{
var map = new Map();
var maxElement = -1000000000;
var minElement = 1000000000;
for ( var i = 0; i < n; i++)
{
if (map.has(arr[i]))
map.set(arr[i], map.get(arr[i])+1)
else
map.set(arr[i], 1);
}
map.forEach((value, key) => {
if (value == 1)
{
maxElement = Math.max(maxElement,
key);
minElement = Math.min(minElement,
key);
}
});
return Math.abs(maxElement - minElement);
}
var arr = [12, 10, 9, 45, 2,
10, 10, 45, 10 ];
var n = arr.length;
document.write( MaxAbsDiff(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
18 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...