I have solved the hacker rank problem listed here. Given a sequence of scores, I need to count the number of times the records for the high and low scores have been broken.
Easiest way for me was to iterate the input for find the answer but i guess that would have meant O(n) complexity, so I thought of solving the problem using divide and conquer approach hoping it would reduce the complexity of the solution.
I have implemented the solution and it is working, but I am not sure whether I can reduce the complexity. Can you please review the code and provide feedback?
private static int minScore, maxScore;
static int[] breakingRecords(int[] score) {
int firstScore = score[0];
int[] result = new int[2];
minScore = maxScore = firstScore;
count(score,1, score.length - 1, result);
return result;
}
private static void count(int[] scores, int start, int end, int[] result) {
if(start == end || end < start) {
int score = scores[start];
if(score > maxScore) {
maxScore = score;
result[0] = result[0] + 1;
} else if(score < minScore) {
minScore = score;
result[1] = result[1] + 1;
}
return;
}
int partition = (end + start) / 2;
count(scores, start, partition, result);
count(scores,partition + 1, end, result);
}