699. Array Upper Bound

Write code that enhances all arrays such that you can call the upperBound() method on any array and it will return the last index of a given target number.

nums is a sorted ascending array of numbers that may contain duplicates. If the target number is not found in the array, return -1.

Example 1:

Input: nums = [3, 4, 5], target = 5

Output: 2

Explanation:

Last index of target value is 2

Example 2:

Input: nums = [1, 4, 5], target = 2

Output: -1

Explanation:

Because there is no digit 2 in the array, return -1.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [3, 4, 6, 6, 6, 6, 7], target = 6

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i], target <= 104
  • nums is sorted in ascending order.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int upperBound(vector<int>& nums, int target) {
 
}
};
Test Case

Input:

Nums
Target