Skip to main content

Breaking the Records HackerRank solution in Java

Java solution for Breaking best and worst Records HackerRank problem

Breaking the Records HackerRank solution in Java
 

Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.

Example 1 :

scores = [12, 24, 10, 24] 

Scores are in the same order as the games played. She tabulates her results as follows:

                                     Count
    Game  Score  Minimum  Maximum   Min Max
     0      12     12       12       0   0
     1      24     12       24       0   1
     2      10     10       24       1   1
     3      24     10       24       1   1

 Given the scores for a season, determine the number of times Maria breaks her records for most and least points scored during the season.

Example 2 :

Input :
scores = [10, 5, 20, 20, 4, 5, 2, 25, 1] 

Output :
2 4


breaking best and worst records

 

 

 


She broke her best record twice (after games 2 and 7) and her worst record four times (after games 1, 4, 6 and 8) so we print 2 and 4 as our answer. Note that she did not break her record for best score during game, as her score during that game was not strictly greater than her best record at the time.

See full problem description on HackerRank :

In this problem, simply we have to find total maximum and minimum record break.

Solution 1

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    public static List<Integer> breakingRecords(List<Integer> scores) {
      
        List<Integer> output = new ArrayList<>();
        int minRecord = scores.get(0);
        int minRecordTotal = 0;
        int maxRecord = scores.get(0);
        int maxRecordTotal = 0;
       
        for (int i = 1; i < scores.size(); i++) {
            if (scores.get(i) < minRecord) {
                minRecord = scores.get(i);
                minRecordTotal++;
            }
                
            if (scores.get(i) > maxRecord) {
                maxRecord = scores.get(i);
                maxRecordTotal++;
            }
        }
        
        output.add(maxRecordTotal);
        output.add(minRecordTotal);

        return output;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(
                new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> scores = Stream
            .of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        List<Integer> result = Result.breakingRecords(scores);

        bufferedWriter.write(result.stream()
                .map(Object::toString)
                .collect(joining(" ")) + "\n");

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Solution explanation :

  • First we are declaring 4 integer variable and initialize 2 variable as 0 and other 2 as 0th index of list means first list value.
  • Traverse through given scores list and check two below conditions
    1. If current score is less than minRecord variable value, then store current list value to minRcord and increment minRecordTotal variable value.
    2. If current score is greater than maxRecord variable value, then store current list value to maxRecord and increment maxRecordTotal variable value.
  • Store maxRecordTotal at 0th index and minRecordTotal to 1st index of output list. Return output.

Output explanation :

scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]

  • minRecord = 10, maxRecord = 10, minRecordTotal = 0, maxRecordTotal = 0 
  • i = 1
    • recordList.get(i) < minRecord | 5 < 10 becomes true
      • minRecord = recordList.get(i) | minRecord = 5
      • minRecordTotal++ | minRecordTotal = 1
    • recordList.get(i) > maxRecord | 5 > 10 becomes false

  • i = 2, minRecord = 5, maxRecord = 10, minRecordTotal = 1, maxRecordTotal = 0
    • 20 < 5 becomes false
    • 20 > 10 becomes true
      • maxRecord = 20
      • maxRecordTotal = 1

  • i = 3, minRecord = 5, maxRecord = 20, minRecordTotal = 1, maxRecordTotal = 1
    • 20 < 5 becomes false
    • 20 > 20 becomes false

  • i = 4, minRecord = 5, maxRecord = 20, minRecordTotal = 1, maxRecordTotal = 1
    • 4 < 5 becomes true
      • minRecord = 4
      • minRecordTotal = 2
    • 4 > 20 becomes false

  • i = 5, minRecord = 4, maxRecord = 20, minRecordTotal = 2, maxRecordTotal = 1
    • 5 < 4 becomes false
    • 5 > 20 becomes false

  • i = 6, minRecord = 4, maxRecord = 20, minRecordTotal = 2, maxRecordTotal = 1
    • 2 < 4 becomes true
      • minRecord = 2
      • minRecordTotal = 3
    • 2 > 20 becomes false

  • i = 7, minRecord = 2, maxRecord = 20, minRecordTotal = 3, maxRecordTotal = 1
    • 25 < 2 becomes false
    • 25 > 20 becomes true
      • maxRecord = 25
      • maxRecordTotal = 2

  • i = 8, minRecord = 2, maxRecord = 25, minRecordTotal = 3, maxRecordTotal = 2
    • 1 < 2 becomes true
      • minRecord = 1
      • minRecordTotal = 4
    • 1 > 25 becomes false

  • minRecordTotal = 4, maxRecordTotal = 2

In above solution we are using 2 if condition, so we can use else if condition for better run time. because both condition can not be true at same time (You can see in output explanation).

Solution 2

for (int i = 1; i < scores.size(); i++) {
    if (scores.get(i) < minRecord) {
        minRecord = scores.get(i);
        minRecordTotal++;
    }
        
    else if (scores.get(i) > maxRecord) {
        maxRecord = scores.get(i);
        maxRecordTotal++;
    }
}


Happy Coding

See other HackerRank problem and its solution :

Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last