Skip to content
Advertisement

Find index in array based on given value

I have a sorted array of numbers with array length as n. For a given item k find index i in the sorted array where elements from index i to n are greater than the given item k. If there is no index present then return -1

This is my program:

JavaScript

The time complexity of this approach is O(n), and I want to reduce it further.

Advertisement

Answer

Since the array is sorted, use binary search for a time complexity of O(log N).

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement