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 a 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 the each element of the array arr[] in an HashMap.
- Now find the maximum and minimum value of the array whose frequency is 1 using the above HashMap created.
- Print the difference of the maximum and minimum value obtained in the above step.
Below is the implementation of the above approach:
Java
// Java program for the above approach import java.util.*; class GFG { // Function to find the maximum // absolute difference between // distinct elements in arr[] static int MaxAbsDiff(int[] arr, int n) { // HashMap to store each element // with their occurrence in array Map<Integer, Integer> map = new HashMap<>(); // maxElement and minElement to // store maximum and minimum // distinct element in arr[] int maxElement = Integer.MIN_VALUE; int minElement = Integer.MAX_VALUE; // Traverse arr[] and update each // element frequency in HashMap for (int i = 0; i < n; i++) { map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); } // Traverse HashMap and check if // value of any key appears 1 // then update maxElement and // minElement by that key 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 absolute difference of // maxElement and minElement return Math.abs(maxElement - minElement); } // Driver Code public static void main(String[] args) { // Given array arr[] int[] arr = { 12, 10, 9, 45, 2, 10, 10, 45, 10 }; int n = arr.length; // Function Call System.out.println(MaxAbsDiff(arr, n)); } } |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the maximum // absolute difference between // distinct elements in []arr static int MaxAbsDiff(int[] arr, int n) { // Dictionary to store each element // with their occurrence in array Dictionary<int, int> map = new Dictionary<int, int>(); // maxElement and minElement to // store maximum and minimum // distinct element in []arr int maxElement = int.MinValue; int minElement = int.MaxValue; // Traverse []arr and update each // element frequency in Dictionary 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); } // Traverse Dictionary and check if // value of any key appears 1 // then update maxElement and // minElement by that key foreach (KeyValuePair<int, int> k in map) { if (k.Value == 1) { maxElement = Math.Max(maxElement, k.Key); minElement = Math.Min(minElement, k.Key); } } // Return absolute difference of // maxElement and minElement return Math.Abs(maxElement - minElement); } // Driver Code public static void Main(String[] args) { // Given array []arr int[] arr = { 12, 10, 9, 45, 2, 10, 10, 45, 10 }; int n = arr.Length; // Function call Console.WriteLine(MaxAbsDiff(arr, n)); } } // This code is contributed by Princi Singh |
10
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum distance between two elements whose absolute difference is K
- Count maximum elements of an array whose absolute difference does not exceed K
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Find K elements whose absolute difference with median of array is maximum
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Absolute distinct count in a sorted array
- Maximum absolute difference between sum of two contiguous sub-arrays
- Maximum absolute difference between sum of subarrays of size K
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Maximum distinct elements after removing k elements
- k-th smallest absolute difference of two elements in an array
- Minimum absolute difference of adjacent elements in a circular array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

