Calculate money placed in boxes after N days based on given conditions
Last Updated :
30 Jan, 2023
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:
- 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.
- In box b1, put 1 more than the money already present in box b1.
- 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
- The money spent on ith day is ((i – 1)/ 7) + ((i – 1) % 7 + 1), where i lies in the range [1, N]
- Simulate the same for days [1, N]
- Print the total cost.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int totalMoney( int N)
{
int ans = 0;
for ( int i = 0; i < N; i++)
{
ans += i / 7;
ans += (i % 7 + 1);
}
return ans;
}
int main()
{
int N = 15;
cout << totalMoney(N);
}
|
Java
import java.io.*;
class GFG {
public static int totalMoney( int N)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++) {
ans += i / 7 ;
ans += (i % 7 + 1 );
}
return ans;
}
public static void main(String[] args)
{
int N = 15 ;
System.out.println(
totalMoney(N));
}
}
|
Python
def totalMoney(N):
ans = 0
for i in range ( 0 , N):
ans + = i / 7
ans + = (i % 7 + 1 )
return ans
N = 15
print (totalMoney(N))
|
C#
using System;
class GFG{
public static int totalMoney( int N)
{
int ans = 0;
for ( int i = 0; i < N; i++)
{
ans += i / 7;
ans += (i % 7 + 1);
}
return ans;
}
static public void Main()
{
int N = 15;
Console.WriteLine(totalMoney(N));
}
}
|
Javascript
<script>
function totalMoney(N)
{
let ans = 0;
for (let i = 0; i < N; i++) {
ans += Math.floor(i / 7);
ans += (i % 7 + 1);
}
return ans;
}
let N = 15;
document.write(
totalMoney(N));
</script>
|
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:
- Initialize variables X and Y, to store the amount of money that can be placed in the complete weeks and partial weeks respectively.
- 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.
- 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)
- Therefore, total amount is equal to X + Y. Print the total amount placed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int totalMoney( int N)
{
int CompWeeks = N / 7;
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' ;
}
int main()
{
int N = 15;
totalMoney(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void totalMoney( int N)
{
int CompWeeks = N / 7 ;
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);
}
public static void main(String[] args)
{
int N = 15 ;
totalMoney(N);
}
}
|
Python3
def total_money(N):
CompWeeks = N / / 7
RemDays = N % 7
X = 28 * CompWeeks + 7 * (CompWeeks * (CompWeeks - 1 ) / / 2 )
Y = RemDays * (RemDays + 1 ) / / 2 + CompWeeks * RemDays
cost = X + Y
print (cost)
N = 15
total_money(N)
|
C#
using System;
class GFG{
static void totalMoney( int N)
{
int CompWeeks = N / 7;
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);
}
public static void Main()
{
int N = 15;
totalMoney(N);
}
}
|
Javascript
<script>
function totalMoney( N)
{
let CompWeeks = Math.floor(N / 7);
let RemDays = N % 7;
let X = 28 * CompWeeks
+ 7 * Math.floor((CompWeeks
* (CompWeeks - 1) / 2));
let Y = RemDays
* Math.floor((RemDays + 1) / 2)
+ CompWeeks * RemDays;
let cost = X + Y;
document.write(cost , '<br>' );
}
let N = 15;
totalMoney(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...