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

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →