Header Ad

HackerRank Breaking the Records problem solution

In this Breaking the Records problem you have Given the scores for a season, determine the number of times Maria breaks her records for most and least points scored during the season.

HackerRank Breaking the Records problem solution


Problem solution in Python programming.

def solution(arr):
    lo = arr[0]
    hi = arr[0]
    lob = 0
    hib = 0
    for i in range(1,len(arr)):
        if arr[i] < lo:
            lob += 1
            lo = arr[i]
        if arr[i] > hi:
            hib += 1
            hi = arr[i]
    return [hib, lob]

if __name__ == "__main__":
    size = int(input())
    arr = [int(t) for t in input().strip().split()]
    ans = solution(arr)
    print(str(ans)[1:-1].replace(",",""))




Problem solution in Java Programming.

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        int min = a[0];
        int max = a[0];
        int res1 = 0, res2 =0;
        for (int i = 1; i < n; i++) {
            if (a[i] > max) {
                res1++;
                max = a[i];
            }
            if (a[i] < min) {
                res2++;
                min = a[i];
            }
        }
        System.out.println(res1 + " "+ res2);
    }
}


Problem solution in C++ programming.

#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#include <fstream>
#include <deque>
#include <stack>
#include <climits>
#include <string>
#include <queue>
#include <memory.h>
#include <map>
#include <unordered_map>

#define ll long long
#define ld double
#define pii pair <ll, ll>
#define mp make_pair

using namespace std;

int main() {
	int n;

	cin >> n;

	int a = (int)1e9;
	int b = -1000;

	int s = 0, r = 0;

	for (int i = 0; i < n; i++) {
		int x;

		scanf("%d", &x);

		if (x < a) {
			r++;
			a = x;
		}

		if (x > b) {
			s++;
			b = x;
		}
	}

	cout << s - 1 << ' ' << r - 1 << endl;

	return 0;
}


Problem solution in C programming.

#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 min;
    int countmin = 0;
    int max;
    int countmax = 0;
    int *score = malloc(sizeof(int) * n);
    for(int score_i = 0; score_i < n; score_i++){
       scanf("%d",&score[score_i]);
    }
    min = max = score[0];
    for (int i = 1; i < n; i++){
        if (score[i] > max){
            countmax++;
            max = score[i];
        }
        if (score[i] < min){
            countmin++;
            min = score[i];
        }
    }
    printf("%d %d", countmax, countmin);
    return 0;
}


Problem solution in JavaScript programming.

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);
    let min = score[0]; 
    let max = score[0];
    let min_decreased = 0;
    let max_increased = 0;
    
    // your code goes here
    for (let i = 1; i < n; i++) {
        if (score[i] < min) {
            min = score[i];
            min_decreased++
        }
        if (score[i] > max) {
            max = score[i];
            max_increased++;
        }
    }
    console.log(max_increased + ' ' + min_decreased);
}


Post a Comment

2 Comments

  1. PHP codes

    $min = $scores[0];
    $max = $scores[0];
    $min_score = 0;
    $max_score = 0;

    for($i=1; $i < count($scores); $i++){
    if($scores[$i] < $min){
    $min = $scores[$i];
    $min_score++;
    }
    if($scores[$i] > $max){
    $max = $scores[$i];
    $max_score++;
    }
    }
    echo $max_score.' '.$min_score;

    ReplyDelete
  2. def breakingRecords(scores):
    minn = 0
    maxx = 0
    m = []
    x = []
    for e in scores:
    if m == [] and x == []:
    minn += 0
    maxx += 0
    m.append(e)
    x.append(e)
    elif e > max(x):
    maxx += 1
    x.append(e)
    m.append(min(m))
    elif e < min(m):
    minn += 1
    m.append(e)
    x.append(max(x))
    else:
    m.append(min(m))
    x.append(max(x))
    print("*************************")
    print("score - "+str(e))
    print("minArr - "+str(m))
    print("maxArr - "+str(x))
    print("CurrentMin - "+str(min(m)))
    print("CurrentMax - "+str(max(x)))
    print("minCount - "+str(minn)+" maxCount - "+str(maxx))
    print("*************************")
    return [maxx, minn]

    ReplyDelete