Breaking the Records HackerRank Solution in C, C++, Java, Python

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.

For example, assume her scores for the season are represented in the array scores = [12,24,10,24]. Scores are in the same order as the games played. She would tabulate 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, find and print 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. It must return an integer array containing 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.

breakingRecords has the following parameter(s):

  • scores: an array of integers

Input Format

The first line contains an integer n, the number of games.

The second line contains  space-separated integers describing the respective values of .score(0),score(1),….score(n-1)

Constraints

  • 1<=n<=1000
  • 0<=scores[i]<=10^8

Output Format

Print two space-seperated integers describing the respective numbers of times the best (highest) score increased and the worst (lowest) score decreased.

Sample Input 0

9

10 5 20 20 4 5 2 25 1

 

Sample Output 0

2 4

Breaking the Records HackerRank 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 HackerRank Solution in C++

#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 HackerRank 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 HackerRank 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 HackerRank Solution in C#

using System;
using System.Linq;
using System.Collections.Generic;

class Solution
{
public static void Main() {
Console.ReadLine ();
int[] nums = Console.ReadLine ().Split (' ').Select (int.Parse).ToArray ();
int best = nums [0];
int worst = nums [0];
int bestCount = 0;
int worstCount = 0;
foreach (var n in nums) {
if (n > best) {
best = n;
bestCount++;
}
if (n < worst) {
worst = n;
worstCount++;
}
}
Console.WriteLine (bestCount + " " + worstCount);
}
}

 

Attempt – Breaking the Records HackerRank Challenge

Link – https://www.hackerrank.com/challenges/breaking-best-and-worst-records

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/sub-array-division-hackerrank-solution/

 

Leave a Comment