For this Question: Given an array Arr of N elements, your task is to find the sum of the difference of maximum and minimum element of all subarrays.The first line of input contains a single integer N, and the next line of input contains N space-separated integers depicting the values of the array.

<b>Constraints:-</b>
1 &le; N &le; 5*10<sup>4</sup>
0 &le; Arr[i] &le; 10<sup>5</sup>Print the sum of the difference of all the possible subarrays.Sample Input:-
4
3 1 4 2

Sample Output:-
16

Explanation:-
Subarrays of size 1:- [3], [1], [4], [2], sum = 0 + 0 + 0 + 0 = 0
Subarrays of size 2:- [3, 1], [1, 4], [4, 2], sum = 2 + 3 + 2 = 7
Subarrays of size 3:- [3, 1, 4], [1, 4, 2], sum = 3 + 3 = 6
Subarrays of size 4:- [3, 1, 4, 2], sum = 3
Total sum = 16

Sample Input:-
4
5 2 0 6

Sample Output:-
28, I have written this Solution Code: import java.io.*;
import java.util.*;



class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a [] = new int[n];
        for(int i = 0;i<n;i++){
            a[i] = sc.nextInt();
        }
        System.out.print(subArrayRanges(a));
    }
    public static long subArrayRanges(int[] nums) {
	int n = nums.length;
	long sum = 0;
	Deque<Integer> q = new ArrayDeque<>();

	q.add(-1);
	for (int i = 0; i <= n; i++) {
		while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] <= nums[i])) {
			int cur = q.removeLast();
			int left = q.peekLast();
			int right = i;
			sum += 1L * (cur - left) * (right - cur) * nums[cur];
		}
		q.add(i);
	}

	q.clear();
	q.add(-1);
	for (int i = 0; i <= n; i++) {
		while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] >= nums[i])) {
			int cur = q.removeLast();
			int left = q.peekLast();
			int right = i;
			sum -= 1L * (cur - left) * (right - cur) * nums[cur];
		}
		q.add(i);
	}
	return sum;
}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?