Calculate sum of scores after N days based on given conditions
Last Updated :
21 Feb, 2022
Given an integer N, representing the number of days, the task is to find the sum of scores after N days based on the following conditions:
- On the first Monday, score is set to 1.
- On every Monday, the score becomes 1 greater than the score on the previous Monday.
- On every other day, the score becomes equal to 1 greater than the score on the previous day.
Examples:
Input: N=4
Output: 10
Explanation:
Scores on each day of the four days are as follows:
Monday: 1
Tuesday: 2
Wednesday: 3
Thursday: 4
Total sum of scores = 1 + 2 + 3 + 4 = 10
Input: N=8
Output: 30
Explanation:
Scores on each day of the 8 days are as follows:
Monday: 1
Tuesday: 2
Wednesday: 3
Thursday: 4
Friday: 5
Saturday: 6
Sunday: 7
Monday: 2
Total sum of scores = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 2 = 30
Naive Approach: The simplest approach to solve the problem is to iterate over the range [1, N] and keep on adding the score on each day. Finally, print the sum of all the scores obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findScoreSum( int n)
{
int total = 0;
int prev_monday = 0, curr_day = 0;
for ( int day = 1; day <= n; day++) {
if (day % 7 == 1) {
prev_monday++;
curr_day = prev_monday;
}
total += curr_day++;
}
cout << total;
}
int main()
{
int N = 8;
findScoreSum(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findScoreSum( int n)
{
int total = 0 ;
int prev_monday = 0 , curr_day = 0 ;
for ( int day = 1 ; day <= n; day++)
{
if (day % 7 == 1 )
{
prev_monday++;
curr_day = prev_monday;
}
total += curr_day++;
}
System.out.println(total);
}
public static void main (String[] args)
{
int N = 8 ;
findScoreSum(N);
}
}
|
Python3
def findScoreSum(n):
total = 0
prev_monday, curr_day = 0 , 0
for day in range ( 1 , n + 1 ):
if (day % 7 = = 1 ):
prev_monday + = 1
curr_day = prev_monday
total + = curr_day
curr_day + = 1
print (total)
if __name__ = = '__main__' :
N = 8
findScoreSum(N)
|
C#
using System;
class GFG
{
static void findScoreSum( int n)
{
int total = 0;
int prev_monday = 0, curr_day = 0;
for ( int day = 1; day <= n; day++) {
if (day % 7 == 1) {
prev_monday++;
curr_day = prev_monday;
}
total += curr_day++;
}
Console.Write(total);
}
public static void Main(String[] args)
{
int N = 8;
findScoreSum(N);
}
}
|
Javascript
<script>
function findScoreSum(n)
{
let total = 0;
let prev_monday = 0, curr_day = 0;
for (let day = 1; day <= n; day++)
{
if (day % 7 == 1)
{
prev_monday++;
curr_day = prev_monday;
}
total += curr_day++;
}
document.write(total);
}
let N = 8;
findScoreSum(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:
Let the number of full weeks be F and the remaining days be D.
Sum of scores after N days = Sum of scores in the first F weeks + Sum of scores in the remaining D days
Sum of scores in the first F weeks:
Sum of scores in the first week = 1 + 2 + 3 + … + 7
Sum of scores in the second week = 2 + 3 + 4 + … + 8 = 7*1 + (1 + 2 + 3 + … + 7)
Sum of scores in the third week = 3 + 4 + 5 + … + 9 = 7*2 + (1 + 2 + 3 + … + 7)
…
Sum of scores in Fth week = 7*(F – 1) + (1 + 2 + 3 + … + 7)
Total sum of scores in the first F weeks = F * (1 + 2 + 3 + … + 7) + 7(1 + 2 + … + (F – 1))
= F * ((7 * 8) / 2) + 7 * (F * (F – 1) / 2)
= F / 2 * (49 + 7 * F)
Sum of scores for the remaining D days:
Sum of scores in the remaining D days = (F + 1) + (F + 2) + … + (F + D)
= F * D + D * (D + 1) / 2
= D / 2 * (2 * F + D + 1)
So, the total sum of scores is [F/2 * (49 + 7*F)] + [D/2 * (2*F + D+1)].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findScoreSum( int n)
{
int F = n / 7;
int D = n % 7;
int fullWeekScore
= (49 + 7 * F) * F / 2;
int lastNonFullWeekScore
= (2 * F + D + 1) * D / 2;
cout << fullWeekScore + lastNonFullWeekScore;
}
int main()
{
int N = 8;
findScoreSum(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void findScoreSum( int n)
{
int F = n / 7 ;
int D = n % 7 ;
int fullWeekScore = ( 49 + 7 * F) * F / 2 ;
int lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / 2 ;
System.out.println(fullWeekScore
+ lastNonFullWeekScore);
}
public static void main(String[] args)
{
int N = 8 ;
findScoreSum(N);
}
}
|
Python3
def findScoreSum(n):
F = n / / 7
D = n % 7
fullWeekScore = ( 49 + 7 * F) * F / / 2
lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / / 2
print (fullWeekScore + lastNonFullWeekScore)
if __name__ = = '__main__' :
N = 8
findScoreSum(N)
|
C#
using System;
class GFG{
static void findScoreSum( int n)
{
int F = n / 7;
int D = n % 7;
int fullWeekScore = (49 + 7 * F) * F / 2;
int lastNonFullWeekScore = (2 * F + D + 1) * D / 2;
Console.WriteLine(fullWeekScore +
lastNonFullWeekScore);
}
static public void Main()
{
int N = 8;
findScoreSum(N);
}
}
|
Javascript
<script>
function findScoreSum(n)
{
let F = n / 7;
let D = n % 7;
let fullWeekScore = (49 + 7 * F) * F / 2;
let lastNonFullWeekScore = (2 * F + D + 1) * D / 2;
document.write(Math.floor(fullWeekScore
+ lastNonFullWeekScore));
}
let N = 8;
findScoreSum(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...