← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Given a 0-indexed array nums of length n, you are to determine the number of indices i such that there is no other number in the array with the value nums[i] - 1. In other words, find the number of local maxima from the given array.
n == nums.length1 <= n <= 1050 <= nums[i] <= 105Input:
nums = [2, 1, 2]
Output:
2
Explanation:
i = 0, nums[0] - 1 = 1, which is present in the array. Hence, it is not a local maxima.i = 1, nums[1] - 1 = 0, which is not present in the array. Hence, it is a local maxima.i = 2, nums[2] - 1 = 1, which is present in the array. Hence, it is not a local maxima.Input:
nums = [8, 10, 1, 8, 7, 7]
Output:
3
Explanation:
i = 0, nums[0] - 1 = 7, which is present in the array. Hence, it is not a local maxima.i = 1, nums[1] - 1 = 9, which is not present in the array. Hence, it is a local maxima.i = 2, nums[2] - 1 = 0, which is not present in the array. Hence, it is a local maxima.i = 3, nums[3] - 1 = 7, which is present in the array. Hence, it is not a local maxima.i = 4, nums[4] - 1 = 6, which is not present in the array. Hence, it is a local maxima.i = 5, nums[5] - 1 = 6, which is not present in the array. Hence, it is a local maxima.Input:
nums = [6, 6, 5, 6, 5]
Output:
1
Explanation:
i = 0, nums[0] - 1 = 5, which is present in the array. Hence, it is not a local maxima.i = 1, nums[1] - 1 = 5, which is present in the array. Hence, it is not a local maxima.i = 2, nums[2] - 1 = 4, which is not present in the array. Hence, it is a local maxima.i = 3, nums[3] - 1 = 5, which is present in the array. Hence, it is not a local maxima.i = 4, nums[4] - 1 = 4, which is not present in the array. Hence, it is a local maxima.nums[i] - 1 is present in the hash map. If not, increment your count.