HackerRank Breaking The Records Solution

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

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Breaking The Records problem solution
HackerRank Breaking The Records Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Breaking The Records

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 Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment