Sponsored
Sponsored
Solve with full IDE support and test cases
Use these hints if you're stuck. Try solving on your own first.
Let <code>dp[i]</code> denote the length of the longest subarray ending at index <code>i</code> that has consecutive and sorted elements.
Use a TreeMap with a sliding window to check if there are <code>k</code> elements in the subarray ending at index <code>i</code>.
If TreeMap has less than <code>k</code> elements and <code>dp[i] < k</code>, the subarray has power equal to -1.
Is it possible to achieve <code>O(nums.length)</code> using a Stack?