Count of distinct differences between two maximum elements of every Subarray
Given an array arr[] of size N. The task is to count the number of unique differences between the two maximum elements of every subarray of size at least 2 of the given array.
Examples:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: arr[] = { 5, 1, 3 }, N = 3
Output: 2
Explanation: The subarrays are {5, 1}, {5, 1, 3}, {1, 3}.
{5, 1} – First max = 5; Second max = 1; difference = (5 – 1) = 4
{5, 1, 3} – First max = 5; Second max = 3; difference = (5 – 3) = 2
{1, 3} – First max = 3; Second max = 1; difference = (3 – 1) = 2
Unique height differences are {4, 2} = 2Input: arr[] = {5, 2, 3, 8}, N = 4
Output: 4
Explanation: The subarrays are: {5, 2}, {5, 2, 3}, {5, 2, 3, 8}, {2, 3}, {2, 3, 8}, {3, 8}
{5, 2} – First max = 5; Second max = 2; difference = (5 – 2) = 3
{5, 2, 3} – First max = 5; Second max = 3; difference = (5 – 3) = 2
{5, 2, 3, 8} – First max = 8; Second max = 5; difference = (8 – 5) = 3
{2, 3} – First max = 3; Second max = 2; difference = (3 – 2) = 1
{2, 3, 8} – First max = 8; Second max = 3; difference = (8 – 3) = 5
{3,8} – First max = 8; Second max = 3; difference = (8 – 3) = 5
Unique height differences are {3, 2, 1, 5} = 4
Approach: The problem can be solved in basis of the following observation. Only the first and the second maximum are required for each subarray. When one other maximum element comes in the subarray the maximum values need to be updated. The concept of stack is used to implement this observation. Follow the steps below to solve this problem.
- Store the next greater element to the left and next greater element to the right of every array element in two arrays.
- Find the difference between next greater element to left and original element at that index ,and difference between next greater element to right and original element at that index and store in a set which contains unique values.
- Print the size of the set
Below is the implementation of the above approach.
Java
// Java code to implement above approachimport java.io.*;import java.util.*; class GFG { // Function to count the number // of unique differences public static int countUnique(int arr[], int n) { // Arrays to store next greater // to the left and next greater // to the right for every arr[i] int[] ngl = new int[n]; int[] ngr = new int[n]; Stack<Integer> st = new Stack<>(); HashSet<Integer> s = new HashSet<>(); // Loop to find next greater element // to the left of arr[i] ngl[0] = -1; st.push(arr[0]); for (int i = 1; i < n; i++) { while (st.size() > 0 && arr[i] > st.peek()) { st.pop(); } if (st.size() == 0) { ngl[i] = -1; } else { ngl[i] = st.peek(); } st.push(arr[i]); } while (st.size() > 0) { st.pop(); } // Loop to find next greater element // to the left of arr[i] ngr[n - 1] = -1; st.push(arr[n - 1]); for (int i = n - 2; i >= 0; i--) { while (st.size() > 0 && arr[i] >= st.peek()) { st.pop(); } if (st.size() != 0) { ngr[i] = st.peek(); } else { ngr[i] = -1; } st.push(arr[i]); } for (int i = 0; i < n; i++) { if (ngl[i] != -1) { s.add(ngl[i] - arr[i]); } if (ngr[i] != -1) { s.add(ngr[i] - arr[i]); } } return s.size(); } // Driver code public static void main(String[] args) { int N = 4; int arr[] = { 5, 2, 3, 8 }; System.out.println(countUnique( arr, N)); }} |
4
Time Complexity: O(N)
Auxiliary Space: O(N)

