![HackerRank Breaking the Records Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Breaking-the-Records-Problem-Solution.jpg)
HackerRank Breaking the Records Solution
In this post, We are going to solve HackerRank Breaking the Records Problem. 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.
breaking records has the following parameter(s):
- int scores[n]: points scored per game
Returns
- int[2]: An array with the number of times she broke her records. Index 0 is for breaking the most points records, and index 1 is for breaking the least points records.
Sample Input 0
9 10 5 20 20 4 5 2 25 1
Sample Output 0
2 4
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 score 0, score 1, and score n-1.
Constraints
1 < n < 1000
0 < scores[I] < 10 power 8
Explanation 0
The diagram below depicts the number of times Maria broke her best and worst records throughout the season:
Explanation 0
The diagram below depicts the number of times Maria broke her best and worst records throughout the season:
She broke her best record twice (after 2 games 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 the 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:
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 Problem Solution](https://thecscience.com/wp-content/uploads/2023/04/HackerRank-Breaking-the-Records-Problem-Solution-1024x576.jpg)
Breaking the Records C Solution
#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 *score = malloc(sizeof(int) * n);
for(int score_i = 0; score_i < n; score_i++){
scanf("%d",&score[score_i]);
}
int g = 0;
int b = 0;
int i = -1;
int bs = score[0];
int ws = score[0];
while (++i < n)
{
if (score[i] > bs)
{
bs = score[i];
++g;
}
if (score[i] < ws)
{
ws = score[i];
++b;
}
}
printf("%d %d",g,b);
return 0;
}
Breaking the Records C++ Solution
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const long double EPS = 1e-9;
const int MAX = 1010;
int n;
int s[MAX];
int main(void) {
scanf("%d", &n);
REP(i, n) scanf("%d", s + i);
int bcnt = 0, wcnt = 0;
int best = 0, worst = 0;
REP(i, n) {
if (i) {
if (best < s[i]) {
best = s[i];
bcnt++;
}
if (s[i] < worst) {
worst = s[i];
wcnt++;
}
} else {
best = s[i];
worst = s[i];
}
}
cout << bcnt << " " << wcnt << endl;
}
Breaking the Records C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int n = int.Parse(Console.ReadLine());
string[] game = Console.ReadLine().Split(' ');
int[] gamePlayed = Array.ConvertAll(game,int.Parse);
int check = gamePlayed[0];
int highestCount = 0;
for (int i=0;i<n;i++)
{
if(check<gamePlayed[i])
{
check = gamePlayed[i];
highestCount++;
}
}
check = gamePlayed[0];
int lowestCount = 0;
for (int i = 0; i < n; i++)
{
if (check > gamePlayed[i])
{
check = gamePlayed[i];
lowestCount++;
}
}
Console.WriteLine(highestCount+" "+lowestCount);
}
}
Breaking the Records Java Solution
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 {
/*
* Complete the 'breakingRecords' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY scores as parameter.
*/
public static List<Integer> breakingRecords(List<Integer> scores) {
List<Integer> records = new ArrayList<Integer>();
int max = scores.get(0);
int min = scores.get(0);
int countMax = 0;
int countMin = 0;
for(int score : scores){
if(score > max){
max = score;
countMax++;
}
if(score < min){
min = score;
countMin++;
}
}
records.add(countMax);
records.add(countMin);
return records;
}
}
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();
}
}
Breaking the Records JavaScript Solution
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());
score = readLine().split(' ');
score = score.map(Number);
// your code goes here
var min = max = score[0];
var breakmin = breakmax = 0;
for ( var i = 1; i < n; i++ ) {
if ( score[i] > max ) {
max = score[i];
breakmax++;
} else if ( score[i] < min ) {
min = score[i];
breakmin++;
}
}
console.log(breakmax+' '+breakmin);
}
Breaking the Records Python Solution
#!/bin/python3
import sys
n = int(input().strip())
score = list(map(int, input().strip().split(' ')))
# your code goes here
count1=0
count2=0
init1 =score[0]
init2=score[0]
for i in range(1,len(score)):
if(init1<score[i]):
#print(str(init1)+ " Greater> "+str(score[i]))
init1=score[i]
count1+=1
if(init2>score[i]):
#print(str(init2)+ " Greater> "+str(score[i]))
init2=score[i]
count2+=1
print(count1,count2)
Other Solutions