# Breaking the Record

I am writing a white paper on Agile. Hope to get done by the end of this week. I will be looking where to start posting it and future ones besides this blog.

I received a message for an easy challenge from HackerRank. Just to warm up I decided to tackle it. If interest in the Breaking the Records challenge please take a look at the description and then go for it.

Given that I am trying to get as many problems solved using a white board, decided to scribble on it before writing code in the Eclipse IDE.

We start by keeping track of the highest and lowest cores by initializing the associated variables with the first entry in the scores array.

We then enter a loop starting with the second score.

If the current score larger than the highest score we update the variable holding the value and increment the count of highest records.

The check is repeated for the lowest scores.

The method returns the array with the records.

A couple runs of the code follow:

```9
10 5 20 20 4 5 2 25 1

main <<< scores: 10 5 20 20 4 5 2 25 1
2 4

10
3 4 21 36 10 28 35 5 24 42

main <<< scores: 3 4 21 36 10 28 35 5 24 42
4 0
```

The code for the entire solution follows:

```import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

/*
*
*/
public class Solution {

/*
* Complete the breakingRecords function below.
* records[0] = highest records
* records[1] = lowest records
*/
static int[] breakingRecords(int[] scores) {

// **** to trac number of high and low records ****
int[] records = new int[2];

// **** check if there are no scores ****
if (scores.length == 0)
return records;

// **** initialize the lowest and highest scores ****
int lowestScore  = scores[0];
int highestScore = scores[0];

// **** loop through the scores ****
for (int i = 1; i < scores.length; i++) { // **** check highest score **** if (scores[i] > highestScore) {
highestScore = scores[i];
records[0]++;
}

// **** check lowest score ****
if (scores[i] < lowestScore) {
lowestScore = scores[i];
records[1]++;
}
}

// **** return the records ****
return records;
}

// **** ****
private static final Scanner scanner = new Scanner(System.in);

/*
* Test scaffolding.
*/
public static void main(String[] args) throws IOException {

// **** ****
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));

// **** ****
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

int[] scores = new int[n];

// **** ****
String[] scoresItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {
int scoresItem = Integer.parseInt(scoresItems[i]);
scores[i] = scoresItem;
}

// ???? ????
bufferedWriter.write("main <<< scores: ");
for (int i = 0; i < scores.length; i++) {
bufferedWriter.write(String.valueOf(scores[i]) + " ");
}
bufferedWriter.newLine();

// **** ****
int[] result = breakingRecords(scores);

// **** ****
for (int i = 0; i < result.length; i++) {

bufferedWriter.write(String.valueOf(result[i]));

if (i != result.length - 1) {
bufferedWriter.write(" ");
}
}

// **** ****
bufferedWriter.newLine();

// **** ****
bufferedWriter.close();

// **** close the scanner ****
scanner.close();
}

}
```

The test scaffolding is provided by HackerRank. To make sure all is well I added to the main() method a few lines to display the received scores. They seem to match for each of the test cases. Also I modified how the bufferedWriter was created. I do not like to use system environment variable for these types of exercises. The main() method in the HackerRank site was not modified. I made the modifications on my version running on the Eclipse IDE.

The essence of this challenge is in the implementation of the breakingRecords() method.

We first initialize the array that we will use to keep track of the high and low breaking records. This array will be returned by this method.

Next we initialize the current lowest and highest scores.

The loop is initialized to start processing the second score. We loop traversing the rest of the scores.

When a score is higher than the current high score we update the appropriate variables. The same holds true for the lowest scores.

The entire code for this project can be found in my GitHub repository.

If you have comments or questions regarding this or any other post in this blog, or if you would like me to help with any phase in the SDLC (Software Development Life Cycle) of a product or service, please do not hesitate and leave me a note below. Requests for help will remain private.

Keep on reading and experimenting. It is the best way to learn and refresh your knowledge!

John