Hackerrank âBreaking the Recordâ solution
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
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)
java programming-challenge complexity
add a comment |Â
up vote
1
down vote
favorite
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)
java programming-challenge complexity
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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)
java programming-challenge complexity
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)
java programming-challenge complexity
edited Mar 1 at 13:17
200_success
123k14142399
123k14142399
asked Mar 1 at 11:00
Balkrishan Nagpal
1165
1165
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You should be consistent with your naming: int score
vs. int scores
.
Avoid keeping state in static
variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.
You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.
The following straightforward loop would be much preferable:
public static int brokenRecords(int scores)
int highScore = scores[0],
lowScore = scores[0],
highRecords = 0,
lowRecords = 0;
for (int i = 1; i < scores.length; i++)
if (scores[i] > highScore)
highScore = scores[i];
highRecords++;
if (scores[i] < lowScore)
lowScore = scores[i];
lowRecords++;
return new int highRecords, lowRecords ;
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You should be consistent with your naming: int score
vs. int scores
.
Avoid keeping state in static
variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.
You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.
The following straightforward loop would be much preferable:
public static int brokenRecords(int scores)
int highScore = scores[0],
lowScore = scores[0],
highRecords = 0,
lowRecords = 0;
for (int i = 1; i < scores.length; i++)
if (scores[i] > highScore)
highScore = scores[i];
highRecords++;
if (scores[i] < lowScore)
lowScore = scores[i];
lowRecords++;
return new int highRecords, lowRecords ;
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
add a comment |Â
up vote
3
down vote
accepted
You should be consistent with your naming: int score
vs. int scores
.
Avoid keeping state in static
variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.
You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.
The following straightforward loop would be much preferable:
public static int brokenRecords(int scores)
int highScore = scores[0],
lowScore = scores[0],
highRecords = 0,
lowRecords = 0;
for (int i = 1; i < scores.length; i++)
if (scores[i] > highScore)
highScore = scores[i];
highRecords++;
if (scores[i] < lowScore)
lowScore = scores[i];
lowRecords++;
return new int highRecords, lowRecords ;
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You should be consistent with your naming: int score
vs. int scores
.
Avoid keeping state in static
variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.
You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.
The following straightforward loop would be much preferable:
public static int brokenRecords(int scores)
int highScore = scores[0],
lowScore = scores[0],
highRecords = 0,
lowRecords = 0;
for (int i = 1; i < scores.length; i++)
if (scores[i] > highScore)
highScore = scores[i];
highRecords++;
if (scores[i] < lowScore)
lowScore = scores[i];
lowRecords++;
return new int highRecords, lowRecords ;
You should be consistent with your naming: int score
vs. int scores
.
Avoid keeping state in static
variables. Either pass the state explicitly as parameters in your function calls, or use instance variables. I recommend against mutating instance variables with recursion, since you would have to think about what the call stack looks like, while keeping track of what the mutable global state looks like. Those two styles of programming just don't mix well.
You have way overcomplicated the problem. The task is inherently O(n), because you must consider all n inputs. Furthermore, this task cannot be parallelized. You must consider the inputs in sequence, so divide-and-conquer is not an effective strategy. Basically, your recursion is just a pointlessly complicated way of iterating through the array from front to back.
The following straightforward loop would be much preferable:
public static int brokenRecords(int scores)
int highScore = scores[0],
lowScore = scores[0],
highRecords = 0,
lowRecords = 0;
for (int i = 1; i < scores.length; i++)
if (scores[i] > highScore)
highScore = scores[i];
highRecords++;
if (scores[i] < lowScore)
lowScore = scores[i];
lowRecords++;
return new int highRecords, lowRecords ;
edited Mar 1 at 15:13
answered Mar 1 at 13:27
200_success
123k14142399
123k14142399
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
add a comment |Â
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Do you mean complexity of solution to this problem can't be less than O(n)?
â Balkrishan Nagpal
Mar 1 at 15:33
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
Is it possible to get the answer without considering all n numbers? I think not.
â 200_success
Mar 1 at 15:34
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188591%2fhackerrank-breaking-the-record-solution%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password