Breaking the Records


Problem Statement :


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 n space-separated integers describing the respective values of score0, score1, ....., 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.



Solution :



title-img


                            Solution in C :

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;
}







C++  :

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lim 10000007
#define pb push_back
#define S second
#define pb push_back
#define mp make_pair
#define INF 1e18
#define fr(i,j,k) for(ll i=j;i<=k;i++)
#define frd(i,j,k) for(ll i=j;i>=k;i--)
#define F first
#define sd(n) scanf("%lld",&n)
#define pd(n) printf("%lld\n",n)
#define db double
#define mod 1000000007
#define pii pair<ll,ll>
int main()
{
    ll n;
    cin>>n;
    ll mx;
    cin>>mx;
    ll mn=mx;
    ll a1=0,a2=0;
    n--;
    ll x;
    while(n--)
    {
        cin>>x;
        if(x>mx)
            a1++;
        if(x<mn)
            a2++;
        mx=max(mx,x);
        mn=min(mn,x);
    }
    cout<<a1<<" "<<a2<<endl;
}








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();
        }
        // your code goes here
        int most = score[0];
        int least = score[0];
        int mr = 0;
        int lr = 0;
        for( int i = 1; i < n; i++ ){
            if( score[i] > most ){
                mr++;
                most = score[i];
            }
            if( score[i] < least ){
                lr++;
                least = score[i];
            }
        }
        
        System.out.print(mr + " " + lr);
    }
}







python3  :

input()
a = list(map(int, input().split()))
m = M = a[0]
x = y = 0
for i in a[1:]:
    if i > M:
        M = i
        x += 1
    elif i < m:
        m = i
        y += 1
print(x, y)
                        








View More Similar Problems

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →