posted 1 month ago

Hi! I'm trying to solve this test:

https://www.testdome.com/questions/java/sorted-search/11890?questionIds=11890,11909&generatorId=27&type=fromtest&testDifficulty=Hard

I can get the basic tests to pass but I fail both performance tests. How can I speed this up?

This is what I have tried:

and

https://www.testdome.com/questions/java/sorted-search/11890?questionIds=11890,11909&generatorId=27&type=fromtest&testDifficulty=Hard

Implement function countNumbers that accepts a sorted array of integers and counts the number of array elements that are less than the parameter lessThan.

For example, SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4) should return 2 because there are two array elements less than 4.

I can get the basic tests to pass but I fail both performance tests. How can I speed this up?

This is what I have tried:

and

posted 1 month ago

A straight binary search may give you the wrong answer. What happens if you have a large array containing multiple 4s? Binary search will find a 4's index, but it will find any 4, not necessarily find the first, so you don't know the index of the first 4, which is one more than the index of the last 3. Once you have the index of the first 4, the count is very simple to calculate.

Carl Anton

Greenhorn

Posts: 8

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |