Last Updated: 17 Apr, 2021

Money In Bank

Easy
Asked in companies
PhonePeDaffodil SoftwareD.E.Shaw

Problem statement

Harshit wants to save money for his first car. So, he puts money in the Ninja bank every day.

He starts by putting in ‘1’ rupee on Monday, the first day. Every day from Tuesday to Sunday, he will put in ‘1’ rupee more than the day before. On every subsequent Monday, he will put in ‘1’ rupee more than the previous Monday.

You are given an integer ‘N’, your task is to return the total amount of money he will have in the Ninja bank at the end of the ‘N’th day.

For example :

Given ‘N’ = 2 
On Day 1 = 1
On Day 2 = 2 
Total Amount = 1 + 2 = 3.
Therefore the answer is 3.

Input Format :

The first line of input contains an integer ‘T’ denoting the number of test cases. The 'T' test cases follow.

The first and the only line of each test case contains a single integer ‘N’ denoting the number of days.

Output Format :

For each test case, print an integer denoting the total amount of money after N days.

The output of each test case will be printed in a separate line.

Constraints :

1 <= T <= 10
1 <= N <= 5000

Time limit: 1sec.

Approaches

01 Approach

The main idea is to loop from 1 to ‘N’ and add 1 coin more than we did yesterday and every time we complete a week, set the coin value to the number of weeks that have passed.

  • Maintain a variable ‘DAY’ and loop from 1 to ‘N’
  • For every ‘DAY’ increase the value of ‘COIN’ by 1.
  • Maintain a variable ‘RES’ and add the value of ‘COIN’ for every ‘DAY’
  • If the ‘DAY’ % 7 is 0, set the value of ‘COIN’ to ‘DAY’/7.
  • Return ‘RES’ as the final answer.

02 Approach

The main idea is to observe that for every full week we complete, we add an extra rupee than we did the last Monday. Therefore formulate the problem as taught below:

 

  • We calculate ‘FULL_WEEKS’ = ‘N’ / 7 denoting the number of full weeks.

 

  • The first week we deposit (1 + 7) * 7 / 2 = 28 rupees.

 

  • In the ‘Wth’ week, we deposit (W + W + 6) * 7 / 2 = (W + 3) * 7 rupees. In the (W + 1)th week we deposit 7 rupees more.

 

  • So the money we deposit each week is also an arithmetic sequence, whose sum is (28 + 28 + 7 * (‘FULL_WEEKS’ - 1)) * ‘FULL_WEEKS’ / 2 = (49 + 7 * ‘FULL_WEEKS’) * ‘FULL_WEEKS’ / 2.

 

  • The last non-full week has ‘REMAINING_DAYS’ = ‘N’ % 7 days.

 

  • We deposit ‘FULL_WEEKS’ + 1 rupees on its Monday. So, we deposit (‘FULL_WEEK’ + 1 + (‘FULL_WEEK’ + 1 + ‘REMAINING_DAYS’ - 1)) * ‘REMAINING_DAYS’ / 2 = (2 * ‘FULL_WEEKS’ + ‘REMAINING_DAYS’ + 1) * ‘REMAINING_DAYS’ / 2 rupees for this week.

 

  • So the final answer is (49 + 7 * ‘FULL_WEEKS’) *‘FULL_WEEKS’ / 2 + (2 * ‘FULL_WEEKS’ + ‘REMAINING_DAYS’ + 1) * ‘REMAINING_DAYS’ / 2.