Algorithm · Google · Hard
Requirements The input is an integer array arr with length N. A subarray qualifies as good when every consecutive difference is either +1 or every consecutive difference is -1. Any subarray containing one element is good. Return the sum of the element-sums of all good subarrays, counting each good subarray once. The algorithm must run in O(N) time. Examples For: The good subarrays are: For instance, [3, 5] is not good because its difference is 2, and [6, 7, 6] is not good…
Checking your access…