Company: IBM India Software engineer_27april
Difficulty: medium
You are given an integer array heights of length n , where heights[i] is the height of the i th skyscraper. Count how many contiguous sequences (subarrays) form a step pattern. A subarray heights[l..r] is a valid step pattern if: Its length is at least 2. For every index i in [l+1, r] , the following condition holds: heights[i] = heights[i-1] - 1 Return the total number of such subarrays. Example n = 5 heights = [5, 4, 3, 1, 5] There are 3 valid step patterns: [5, 4]: 4 = 5 - 1 [4, 3]: 3 = 4 - 1 [5, 4, 3]: Both 4 = 5 - 1 and 3 = 4 - 1 Note that [4, 3, 1] is not a valid step pattern because at index 2, the condition is not satisfied: 3 - 1 ≠ 1. Function Description Complete the function countSteps in the editor with the following parameters: int heights[n] : the building heights Returns long int : the number of contiguous sequences (subarrays) of buildings that are "step patterns" Constraints 2 ≤ n ≤ 10 6 1 ≤ heights[i] ≤ 10 9 Input Format For Custom Testing The first lin