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

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...