BackeasyBinary Search Capgemini

Find First Greater Element Index Solution

Problem Statement

Given a sorted array of distinct integers `values` and a target integer `target`, find the index of the first element in `values` that is strictly greater than `target`. If no such element exists, return -1.

Example 1
Input
{"values":[2,5,8,12,16,23,38,56,72,91],"target":15}
Output
4

Explanation: Find the index of the first element strictly greater than 15. The elements greater than 15 are 16, 23, 38, 56, 72, 91. The first of these is 16, which is at index 4.

Example 2
Input
{"values":[10,20,30,40,50],"target":50}
Output
-1

Explanation: Find the index of the first element strictly greater than 50. There is no element in the array strictly greater than 50. Thus, the output is -1.

Example 3
Input
{"values":[5,10,15,20],"target":3}
Output
0

Explanation: Find the index of the first element strictly greater than 3. The first element, 5, is greater than 3. Thus, the index is 0.

Constraints

  • 1 <= values.length <= 10^5
  • 1 <= values[i] <= 10^9
  • values is sorted in strictly increasing order.
  • -10^9 <= target <= 10^9
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free