HackerRank Breaking The Records Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Breaking The Records Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Breaking The Records Solution
HackerRank Breaking The Records Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Breaking The Records Solution

Task

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

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.

Function Description

Complete the breakingRecords function in the editor below.

breakingRecords has the following parameter(s):

  • int scores[n]: points scored per game

Returns

  • int[2]: An array with the numbers of times she broke her records. Index 0 is for breaking most points records, and index 1 is for breaking least points records.

Input Format

The first line contains an integer n, the number of games.
The second line contains n space-separated integers describing the respective values of score0score1, . . . , scoren – 1

Constraints

  • 1 <= n <= 1000
  • 0 <= scores[i] <= 108

Sample Input 0

9
10 5 20 20 4 5 2 25 1

Sample Output 0

2 4

Explanation 0

The diagram below depicts the number of times Maria broke her best and worst records throughout the season:

image

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 4 as our answer. Note that she did not break her record for best score during game 3, as her score during that game was not strictly greater than her best record at the time.

Sample Input 1

10
3 4 21 36 10 28 35 5 24 42

Sample Output 1

4 0

Explanation 1

The diagram below depicts the number of times Maria broke her best and worst records throughout the season:

image

She broke her best record four times (after games 1, 2, 3, and 9 ) and her worst record zero times (no score during the season was lower than the one she earned during her first game), so we print 4 0 as our answer.

HackerRank Breaking The Records Solution

Breaking The Records Solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
    int n; 
    scanf("%d",&n);
    int a[10000];
    for(int score_i = 0; score_i < n; score_i++){
       scanf("%d",&a[score_i]);
    }
    int i,min,max,c1,c2;
    min=a[0];
    max=a[0];
    c1=0;
    c2=0;
    for(i=1;i<n;i++)
        {
        if(a[i]>max)
            {
            c1++;
            max=a[i];
        }
        if(a[i]<min)
            {
            c2++;
            min=a[i];
        }
    }
    printf("%d %d\n",c1,c2);
    // your code goes here
    return 0;
}

Breaking The Records Solution in Cpp

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> score(n);
    for(int score_i = 0; score_i < n; score_i++){
       cin >> score[score_i];
    }
    // your code goes here
    int ma = score[0];
    int mi = score[0];
    int cnta=0;
    int cntb=0;
    for(int i=1;i<n;++i){
        if(score[i]>ma){
            ++cnta;
            ma=score[i];
        }
        if(score[i]<mi){
            ++cntb;
            mi=score[i];
        }
    }
    
    cout << cnta << " "<<cntb;
    return 0;
}

Breaking The Records Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] score = new int[n];
        for(int score_i=0; score_i < n; score_i++){
            score[score_i] = in.nextInt();
        }
        int max=score[0];
        int min=score[0];
        int maxcount=0;
        int mincount=0;
        for(int i=1;i<n;i++){
            if(score[i]>max){
                max=score[i];
                maxcount=maxcount+1;
            }
            if(score[i]<min){
                min=score[i];
                mincount=mincount+1;
            }
        }
        System.out.println(maxcount+" "+mincount);
        // your code goes here
    }
}

Breaking The Records Solution in Python

#!/bin/python
import sys
n = int(raw_input().strip())
score = map(int, raw_input().strip().split(' '))
a = score[0]
b = score[0]
r1 = 0
r2 = 0
for i in score[1:]:
    if i > a:
        a = i
        r1 += 1
    if i < b:
        b = i
        r2 += 1
print r1,r2

Breaking The Records Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
    input_stdin += data;
});
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});
function readLine() {
    return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
    var n = parseInt(readLine());
    scores = readLine().split(' ');
    scores = scores.map(Number);
    let min = scores[0]
    let max = scores[0]
    let increases = 0
    let decreases = 0
    
    scores.forEach(score => {
        if (score < min) {
            min = score
            decreases++
        }
        if (score > max) {
            max = score
            increases++
        }
    })
    console.log(increases, decreases)
}

Breaking The Records Solution in Scala

import scala.io.StdIn
    
object Solution {
    def main(args: Array[String]) {
        val n = StdIn.readInt()
        val scores = StdIn.readLine().split(" ").map(_.toInt)
        val record = scores.tail.foldLeft((scores.head, 0, scores.head, 0)) { (r, score) =>
            if (score > r._1) (score, r._2 + 1, r._3, r._4)
            else if (score < r._3) (r._1, r._2, score, r._4 + 1)
            else r
        }
        printf("%d %d\n", record._2, record._4)
    }
}

Breaking The Records Solution in Pascal

var f:text; n,a,w,l,max,min:int64; i:longint;
begin
  assign(f,''); reset(f); read(f,n);
  w:=0; l:=0;
  for i:=1 to n do
  begin
    read(f,a); 
    if i=1 then
    begin max:=a; min:=a end
    else
    begin
      if a>max then
      begin max:=a; inc(w) end;
      if a<min then
      begin min:=a; inc(l) end
    end
  end;
  close(f);
  assign(f,''); rewrite(f); write(f,w,' ',l); close(f)
end.

Disclaimer: This problem (Breaking The Records) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Between Two Sets Solution

Leave a Reply

Your email address will not be published. Required fields are marked *