# Calculate money placed in boxes after N days based on given conditions

Given 7 empty boxes b1, b2, b3, b4, b5, b6, b7, and an integer N, the task is to find the total amount of money that can be placed in the boxes after N days based on the following conditions:

1. Each day, the money can be put only in one box in circular fashion b1, b2, b3, b4, b5, b6, b7, b1, b2, ….. and so on.
2. In box b1, put 1 more than the money already present in box b1.
3. In each box except b1, put 1 more than the money present in the previous box.

Examples:

Input: N = 4
Output: 15
Explanation:
Putting money in the box b1 on day 1 = 1
Putting money in the box b2 on day 2 = 2
Putting money in the box b3 on day 3 = 3
Putting money in the box b4 on day 4 = 4
Putting money in the box b5 on day 5 = 5
After the 5th day, total amount = 1 + 2 + 3 + 4 + 5 = 15

Input: N = 15
Output: 66
Explanation: After the 15th day, the total amount = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 3 = 66

Approach: Follow the steps below to solve the problem

1. The money spent on ith day is ((i – 1)/ 7) + ((i – 1) % 7 + 1), where i lies in the range [1, N]
2. Simulate the same for days [1, N]
3. Print the total cost.

Below is the implementation of the above approach:

## C++

 `// C++ program for``// the above approach``#include ``using` `namespace` `std;` `// Function to find the total money``// placed in boxes after N days``int` `totalMoney(``int` `N)``{``    ` `    ``// Stores the total money``    ``int` `ans = 0;` `    ``// Iterate for N days``    ``for``(``int` `i = 0; i < N; i++) ``    ``{``        ` `        ``// Adding the Week number``        ``ans += i / 7;` `        ``// Adding previous amount + 1``        ``ans += (i % 7 + 1);``    ``}` `    ``// Return the total amount``    ``return` `ans;``}` `// Driver code``int` `main()``{   ``    ` `    ``// Input``    ``int` `N = 15;` `    ``// Function call to find``    ``// total money placed``    ``cout << totalMoney(N);``}` `// This code is contributed khushboogoyal499`

## Java

 `// Java program for``// the above approach` `import` `java.io.*;` `class` `GFG {` `    ``// Function to find the total money``    ``// placed in boxes after N days``    ``public` `static` `int` `totalMoney(``int` `N)``    ``{` `        ``// Stores the total money``        ``int` `ans = ``0``;` `        ``// Iterate for N days``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Adding the Week number``            ``ans += i / ``7``;` `            ``// Adding previous amount + 1``            ``ans += (i % ``7` `+ ``1``);``        ``}` `        ``// Return the total amount``        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Input``        ``int` `N = ``15``;` `        ``// Function call to find``        ``// total money placed``        ``System.out.println(``            ``totalMoney(N));``    ``}``}`

## Python

 `# Python program for``# the above approach` `# Function to find the total money``# placed in boxes after N days``def` `totalMoney(N):``    ` `    ``# Stores the total money``    ``ans ``=` `0` `    ``# Iterate for N days``    ``for` `i ``in` `range``(``0``, N):``     ` `        ``# Adding the Week number``        ``ans ``+``=` `i ``/` `7` `        ``# Adding previous amount + 1``        ``ans ``+``=` `(i ``%` `7` `+` `1``)``     ` `    ``# Return the total amount``    ``return` `ans`` ` `# Driver code` `# Input``N ``=` `15` `# Function call to find``# total money placed``print``(totalMoney(N))` `# This code is contributed by shivanisinghss2110`

## C#

 `// C# program for``// the above approach``using` `System;` `class` `GFG{``    ` `// Function to find the total money``// placed in boxes after N days``public` `static` `int` `totalMoney(``int` `N)``{``    ` `    ``// Stores the total money``    ``int` `ans = 0;` `    ``// Iterate for N days``    ``for``(``int` `i = 0; i < N; i++) ``    ``{``        ` `        ``// Adding the Week number``        ``ans += i / 7;` `        ``// Adding previous amount + 1``        ``ans += (i % 7 + 1);``    ``}` `    ``// Return the total amount``    ``return` `ans;``}` `// Driver code``static` `public` `void` `Main()``{``    ` `    ``// Input``    ``int` `N = 15;` `    ``// Function call to find``    ``// total money placed``    ``Console.WriteLine(totalMoney(N));``}``}` `// This code is contributed by offbeat`

## Javascript

 ``

Output:
`66`

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by finding out the number of completed weeks and the number of days remaining in the last week.

Follow the steps to solve the problem:

1. Initialize variables X and Y, to store the amount of money that can be placed in the complete weeks and partial weeks respectively.
2. The money in each week can be calculated as:
• 1st  Week: 1 2 3 4 5 6 7 = 28 + (7 x 0)
• 2nd  Week: 2 3 4 5 6 7 8 = 28 + (7 x 1)
• 3rd  Week: 3 4 5 6 7 8 9 = 28 + (7 x 2)
• 4th  Week: 4 5 6 7 8 9 10 = 28 + (7 x 3) and so on.
3. Therefore, update:
X = 28 + 7 x (Number of completed weeks – 1)
Y = Sum of remaining days + ( Number of complete weeks * Number of days remaining in the last week)
4. Therefore, total amount is equal to X + Y. Print the total amount placed.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to find total``// money placed in the box``int` `totalMoney(``int` `N)``{``    ``// Number of complete weeks``    ``int` `CompWeeks = N / 7;` `    ``// Remaining days in``    ``// the last week``    ``int` `RemDays = N % 7;` `    ``int` `X = 28 * CompWeeks``            ``+ 7 * (CompWeeks``                   ``* (CompWeeks - 1) / 2);` `    ``int` `Y = RemDays``                ``* (RemDays + 1) / 2``            ``+ CompWeeks * RemDays;` `    ``int` `cost = X + Y;` `    ``cout << cost << ``'\n'``;``}` `// Driver Code``int` `main()``{``    ``// Input``    ``int` `N = 15;` `    ``// Function call to find``    ``// the total money placed``    ``totalMoney(N);` `    ``return` `0;``}`

## Java

 `// Java program for above approach` `import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG {` `// Function to find total``// money placed in the box``static` `void` `totalMoney(``int` `N)``{``    ``// Number of complete weeks``    ``int` `CompWeeks = N / ``7``;``  ` `    ``// Remaining days in``    ``// the last week``    ``int` `RemDays = N % ``7``;``  ` `    ``int` `X = ``28` `* CompWeeks``            ``+ ``7` `* (CompWeeks``                   ``* (CompWeeks - ``1``) / ``2``);``  ` `    ``int` `Y = RemDays``                ``* (RemDays + ``1``) / ``2``            ``+ CompWeeks * RemDays;``  ` `    ``int` `cost = X + Y;``  ` `    ``System.out.print(cost);``}`  `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``    ``// Input``    ``int` `N = ``15``;``  ` `    ``// Function call to find``    ``// the total money placed``    ``totalMoney(N);``    ``}``}` `// This code is contributed by souravghosh0416.`

## Python3

 `# Python3 code to implement the approach` `# Function to find total money``# placed in the box``def` `total_money(N):``    ``# Number of complete weeks``    ``CompWeeks ``=` `N ``/``/` `7` `    ``# Remaining days in the last week``    ``RemDays ``=` `N ``%` `7` `    ``X ``=` `28` `*` `CompWeeks ``+` `7` `*` `(CompWeeks ``*` `(CompWeeks ``-` `1``) ``/``/` `2``)` `    ``Y ``=` `RemDays ``*` `(RemDays ``+` `1``) ``/``/` `2` `+` `CompWeeks ``*` `RemDays` `    ``cost ``=` `X ``+` `Y` `    ``print``(cost)` `# Driver Code``# Input``N ``=` `15` `# Function call to find the total money placed``total_money(N)` `# This code is contributed by phasing17`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find total``// money placed in the box``static` `void` `totalMoney(``int` `N)``{``    ` `    ``// Number of complete weeks``    ``int` `CompWeeks = N / 7;``   ` `    ``// Remaining days in``    ``// the last week``    ``int` `RemDays = N % 7;``   ` `    ``int` `X = 28 * CompWeeks + 7 * ``    ``(CompWeeks * (CompWeeks - 1) / 2);``   ` `    ``int` `Y = RemDays * (RemDays + 1) / 2 + ``          ``CompWeeks * RemDays;``   ` `    ``int` `cost = X + Y;``   ` `    ``Console.WriteLine(cost);``}    ` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Input``    ``int` `N = 15;``   ` `    ``// Function call to find``    ``// the total money placed``    ``totalMoney(N);``}``}` `// This code is contriobuted by sanjoy_62`

## Javascript

 ``

Output:
`66`

Time Complexity: O(1)
Auxiliary Space: O(1)

Previous
Next