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 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 Maria's scores for a season, find and print the number of times she breaks her records for most and least points scored during the season.


Question from hackerrank.


Solution in C# - 

class Solution {

    static int[] breakingRecords(int[] arr) {

        int min = arr[0], max = arr[0];
        int cmin = 0, cmax = 0;
        int[] ans = new int[2];
        foreach(int x in arr)
        {
            if(x < min)
            {
                cmin++;
                min = x;
            }
            else if( x > max )
            {
                cmax++;
                max = x;
            }
        }
        ans[0] = cmax;
        ans[1] = cmin;
        return ans;

    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int n = Convert.ToInt32(Console.ReadLine());

        int[] scores = Array.ConvertAll(Console.ReadLine().Split(' '), scoresTemp => Convert.ToInt32(scoresTemp));
        int[] result = breakingRecords(scores);

        textWriter.WriteLine(string.Join(" ", result));

        textWriter.Flush();
        textWriter.Close();
    }

}

Post a Comment

Previous Post Next Post