Need more efficient way
Following are the results:
Example case: Correct answer
Various small arrays: Correct answer
Performance test when sortedArray contains lessThan: Time
limit exceeded
Performance test when sortedArray doesn't contain lessThan: Time limit exceeded
Following is the code:
import java.util.Arrays;
public class SortedSearch {
public static int countNumbers(int[] sortedArray, int lessThan) {
Arrays.sort(sortedArray);
int count =0;
for(int num :sortedArray){
if(num < lessThan)
count++;
}
return count;
}
public static void main(String[] args) {
System.out.println(SortedSearch.countNumbers(new int[] { 1, 3, 5, 7 }, 4));
}
}
Ref Link : Try here
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…