Java | Easy Solution with explanation
Intution\n1. The main magic lies in  1 <= nums.length <= 1000 and 1 <= nums[i] <= nums.length. \n2. We can generate all subarrays.\n3. Now we just need to find 
**Intution**\n1. The main magic lies in  `1 <= nums.length <= 1000` and `1 <= nums[i] <= nums.length`. \n2. We can generate all subarrays.\n3. Now we just need to find a way to count the imbalance number in constant time or less than linear time while generating the subarrays.\n4. To acheive this in constant time we know that the max value in array is 1000. Therefore we will create an array of 1000 + 5 length (+5 to avoid boundary conditions).\n5. Create a temp variable which will calculate the current subarray\'s imbalance number and a final count for answer.\n6. Whenever we encounter a number we will check its neighbour(val - 1 and val + 1), if the neighbours are free then increase the temp variable otherwise if both the neighbours are occupied then decrease the temp variable and add it to the final count.\n7. If the number is already marked just add the temp variable to the final count. \n8. Boundary checks if at any point the temp variable is less than zero just make it zero again.\n\n**TC O(n^2)\nSC O(n)**\n\n```\nclass Solution {\n    public int sumImbalanceNumbers(int[] nums) {        \n        int n = nums.length;        \n        int count = 0;\n        \n        for(int i = 0; i < n - 1; i++){            \n            int arr[] = new int[1005];            \n            arr[nums[i]] = 1;            \n            int temp = 0;\n            \n            for(int j = i + 1; j < n; j++){                \n                int val = nums[j];                        \n                if(arr[val] == 1) {                    \n                    count += temp;\n                } else {                    \n                    if(arr[val - 1] == 0 && arr[val + 1] == 0) {\n                        temp++;\n                    } else if(arr[val - 1] == 1 && arr[val + 1] == 1){\n                        temp--;                        \n                    }                     \n                    if(temp < 0) temp = 0;                   \n                    arr[val] = 1;\n                    count += temp;\n                }                \n            }            \n        }        \n        return count;\n    }\n}\n```\n\nPlease Upvote.
['Array', 'Java']