Question

You just bought a public transit card that allows you to ride the Metro for a...

You just bought a public transit card that allows you to ride the Metro for a certain number of days.

Here is how it works: upon first receiving the card, the system allocates you a 31-day pass, which equals the number of days in January. The second time you pay for the card, your pass is extended by 28 days, i.e. the number of days in February (note that leap years are not considered), and so on. The 13th time you extend the pass, you get 31 days again.

You just ran out of days on the card, and unfortunately you've forgotten how many times your pass has been extended so far. However, you do remember the number of days you were able to ride the Metro during this most recent month. Figure out the number of days by which your pass will now be extended, and return all the options as an array sorted in increasing order.

Example

For lastNumberOfDays = 30, the output should be
metroCard(lastNumberOfDays) = [31].

There are 30 days in April, June, September and November, so the next months to consider are May, July, October or December. All of them have exactly 31 days, which means that you will definitely get a 31-days pass the next time you extend your card.

Input/Output

  • [execution time limit] 3 seconds (java)

  • [input] integer lastNumberOfDays

    A positive integer, the number of days for which the card was extended the last time.

    Guaranteed constraints:
    lastNumberOfDays = 28 or lastNumberOfDays = 30 or lastNumberOfDays = 31.

  • [output] array.integer

    • An array of positive integers, the possible number of days for which you will extend your pass. The elements of the array can only be equal to 28, 30 or 31 and must be sorted in increasing order.

int[] metroCard(int lastNumberOfDays) {
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please have a look at the metrocard function :

public static int[] metroCard(int lastNumberOfDays) {
    if(lastNumberOfDays == 31)
    {

      // A month of 31 days can be followed by month of 30 days or month of 28 days(february) or 31 days(august)
      int A[] = {28,30,31};
      return A;
   }
    else if(lastNumberOfDays == 30)
    {

//    A month of 30 days can only be followed by month of 31 days
      int A[] = {31};
      return A;
   }
    else
    {

//   A month of 28 days is only followed by month of 31 days(march)
      int A[] = {31};
      return A;
   }
}

Add a comment
Know the answer?
Add Answer to:
You just bought a public transit card that allows you to ride the Metro for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

    Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you...

    Map, Filter, and Reduce are three functions commonly used in functional programming. For this assignment you will be implementing all three, along with three minor functions that can be passed to them. Map The map function takes as arguments a function pointer and a integer vector pointer. It applies the function to every element in the vector, storing the results in-order in a new vector. It then returns a pointer to a new vector. You should pass your square function...

  • Write a code for a ride tracking app for Large Resort parks. In your app users...

    Write a code for a ride tracking app for Large Resort parks. In your app users are going to be able to track wait times for up to five attractions, find the shortest wait time, and find the average wait time. //-----------------------------Sample Program Behavior with five attractions entered--------------------- Welcome to Large Resort Parks! Please enter the attractions you plan to visit and their wait times Ride 1 info: Attraction Name: 80 ft drop Wait time (in minutes): 40 Enter another...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define...

    Create a C program to implement a grade book. Must follow these guidelines: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct { char name[MAX_SIZE1]; int scores[MAX_SIZE2]; } 3. The program accepts from the comand line an integer n (<= 30) as the number of students in the class. You may assume that the input will always be valid. 4. Dynamically allocate memory for an array of...

  • Use while loops, for loops, and if statements only- python 3 In order to pass time...

    Use while loops, for loops, and if statements only- python 3 In order to pass time during your vacation, you decided to go on a hike to visit a scenic lake up in the mountains. Hiking to the lake will take you a full day, then you will stay there for a day to rest and enjoy the scenery, and then spend another day hiking home, for a total of three days. However, the accursed weather this summer is ridiculously...

  • A new city “Ballymanus” has been announced in a state in Australia and the state government has planned to extend the public transport where the fares will be calculated according to the existing fare...

    A new city “Ballymanus” has been announced in a state in Australia and the state government has planned to extend the public transport where the fares will be calculated according to the existing fare rule of the state. As per the existing fare system, everyone must have to buy the “Move Card” in order to travel on trains, tram, and buses in different parts of the state, including the new city. There are 2 options in “Move Card”  “Move...

  • IN C++ In a previous assignment you wrote pseudo code for the calculation of the days...

    IN C++ In a previous assignment you wrote pseudo code for the calculation of the days into a year given the date. Write a function that returns an int. It takes three parameters , all int(s), int DaysIntoYear ( int Year, int Month, int day); The return value is the number of days in the year from January 1 to the specified date.   Year is included so you can determine if this is a leap year ( see below).   If...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT