Skip to main content

Breaking the Records - HackerRank- Problem Solving in Java

Difficulty : EASY
Problem :

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.

Visual representation of the table

Game Score Minimum Maximum Count Min Count 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
Ouput : [1 , 2]

Imporant Points to note

  • We canculate the record determining her performance on Game 0(which is highlited in green) which will the value of minimum and maximum.
  • We change the value of maximum or minimum only if her score breaks our current record of maximim or minimum.
  • in case she breaks record then we increment the counter respectively.
  • return the Count max and Count min in the same order as a List.

Algorithm

  • inputs recived : List score ,int games
  • Set Min and Max to scores 1st element or 0th index element.
  • Loop through the scores from the 2nd element or the 1st index till the scores.size()-1
  • inside the above loop check if the value of current index is larger maximum if so then increase the counter of the maximum and get the value of the maximum to the current value.
  • else if statement to check if the value is smaller than current minimum if so then increase the counter of the minimum and set the value of minimum to the current value
  • outside the loop create an array and add the two value in the order of maxcount then min count and add the list to the return statement.

I Highly recommend you dont go through the below code now but try to solve it in your own to get the ability to solve problems but im just pasting it here for those who hasnt manged to find the solution despite many trys

List brakingTheRecord(List score)
{

 int currentMax = score.get(0);
 int currentMin = score.get(0);

int surpassed = 0;
int backlogged = 0;

 for(int index = 0 ;index < score.size(); index++)
 {
  if(score.get(index) > currentMax)
  {
   surpassed++;
   currentMax = score.get(index);
  }
  else if(score.get(index) < currentMin)
  {
    backlogged++;
    currentMin = score.get(index);
  }

}
List result = new ArrayList<>();
result.add( surpassed);
result.add( backlogged);
System.out.println(surpassed +" "+ backlogged);
return result;
}

Comments

Popular posts from this blog

Designer PDF Viewer - HackerRank Problems

Difficulty: EASY Problem : The objective here is to find the size of the highlighted area, and we are given the size's of all the alphabets, we have to find the largest alphabet in the highlighted word and then calculate the size of the rectangle so if the tallest character is 3 then the size of the box will be 3 * number of characters given. Visual representation of the selection : abc def ghij Inputs An array with the sizes of all alphabets a-z in order. A String of highlighted words. Important points to note The array which holds the height of each character in ascending order which means the arrays 0th index will have the height of a 1st index will have the height of b and so on and so forth in the end the hight of z will be there so it's easy to locate each character. A String with the highlighted word. This means we have got the characters inside the rectangle, all we have to find is ...

Literals of Base numbers in Java ( Octal , Hexadecimal, Decimal)

1. Overview: A literal key indicates the compiler how to interpret the value of the given data type, for numbers we can calculate the value by using Octal representation or hexadecimal representation but just typing out a hexadecimal value to an int will throw us an error because the compiler has no idea how to handle it but if we assign the java specified prefix for the required bases with some literals then the compiler will not throw us any error as it understands how to interpret the value.  Base Litrals Values Example Eg. Value Decimal none 0-9 int x = 10; x is 10 Octal 0 (zero) as the prefix 0-7 int x = 12; x is 10 Hexadecimal 0x or (zero) along with an x 0-9 and a-f or A-F int x = 0XA; x is 10 Binary or Base(2) Allowed Digits 0 and 1 int i = 10; and now the variable i has value 10. i...

Array List - Collections Framework in Java - DSA

Gist: An array list can store individual objects by following insertion order, here the initial capacity is 10 by default but can be modified as per the requirement, once the array list reaches its load factor then internal all the elements of the current array is copied to a new array with the new capacity and the reference variable will now be referring to this new array list and the old array will be dealt by the garbage collector. Hierarchical order Type of constructors Empty argument constructor or the default constructor is the same as invoking any other object here a new ArrayList is created with a default size of 10. Below is the most commonly used constructor by beginners and others alike. ArrayList array = new ArrayList(); //array has a capacity of 10 The default constructor above will allot only 10 slots but if you want the initial size to be 20 or 1000 you can do so with the following constructor this is ideal w...