Calculate sum of scores after N days based on given conditions
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 = 10Input: 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to c sum of calculate // sum of scores after n days void findScoreSum( int n) { // Store the required sum int total = 0; // Store the score on previous // monday and current day respectively int prev_monday = 0, curr_day = 0; // Iterate over the range [1, n] for ( int day = 1; day <= n; day++) { // If the current day is monday if (day % 7 == 1) { // Increment score of // prev_monday by 1 prev_monday++; // Update score of current day curr_day = prev_monday; } // Add score of current day and // increment score for next day total += curr_day++; } // Print the result cout << total; } // Driver Code int main() { int N = 8; findScoreSum(N); return 0; } |
Java
// Java Program to implement // the above approach import java.io.*; class GFG { // Function to c sum of calculate // sum of scores after n days static void findScoreSum( int n) { // Store the required sum int total = 0 ; // Store the score on previous // monday and current day respectively int prev_monday = 0 , curr_day = 0 ; // Iterate over the range [1, n] for ( int day = 1 ; day <= n; day++) { // If the current day is monday if (day % 7 == 1 ) { // Increment score of // prev_monday by 1 prev_monday++; // Update score of current day curr_day = prev_monday; } // Add score of current day and // increment score for next day total += curr_day++; } // Print the result System.out.println(total); } // Driver Code public static void main (String[] args) { int N = 8 ; findScoreSum(N); } } // This code is contributed by avanitrachhadiya2155 |
Python3
# Python3 program for the above approach # Function to c sum of calculate # sum of scores after n days def findScoreSum(n): # Store the required sum total = 0 # Store the score on previous # monday and current day respectively prev_monday, curr_day = 0 , 0 # Iterate over the range [1, n] for day in range ( 1 , n + 1 ): # If the current day is monday if (day % 7 = = 1 ): # Increment score of # prev_monday by 1 prev_monday + = 1 # Update score of current day curr_day = prev_monday # Add score of current day and # increment score for next day total + = curr_day curr_day + = 1 # Print the result print (total) # Driver Code if __name__ = = '__main__' : N = 8 findScoreSum(N) # This code is contributed by mohit kumar 29 |
C#
// C# Program to implement // the above approach using System; class GFG { // Function to c sum of calculate // sum of scores after n days static void findScoreSum( int n) { // Store the required sum int total = 0; // Store the score on previous // monday and current day respectively int prev_monday = 0, curr_day = 0; // Iterate over the range [1, n] for ( int day = 1; day <= n; day++) { // If the current day is monday if (day % 7 == 1) { // Increment score of // prev_monday by 1 prev_monday++; // Update score of current day curr_day = prev_monday; } // Add score of current day and // increment score for next day total += curr_day++; } // Print the result Console.Write(total); } // Driver Code public static void Main(String[] args) { int N = 8; findScoreSum(N); } } // This code is contributed by code_hunt. |
Javascript
<script> // Javascript Program to implement // the above approach // Function to c sum of calculate // sum of scores after n days function findScoreSum(n) { // Store the required sum let total = 0; // Store the score on previous // monday and current day respectively let prev_monday = 0, curr_day = 0; // Iterate over the range [1, n] for (let day = 1; day <= n; day++) { // If the current day is monday if (day % 7 == 1) { // Increment score of // prev_monday by 1 prev_monday++; // Update score of current day curr_day = prev_monday; } // Add score of current day and // increment score for next day total += curr_day++; } // Print the result document.write(total); } // Driver code let N = 8; findScoreSum(N); // This code is contributed by target_2 </script> |
30
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 daysSum 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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate sum // of scores after n days void findScoreSum( int n) { // Store the number // of full weeks int F = n / 7; // Stores the remaining // days in the last week int D = n % 7; // Store the sum of scores // in the first F full weeks int fullWeekScore = (49 + 7 * F) * F / 2; // Store the sum of scores // in the last week int lastNonFullWeekScore = (2 * F + D + 1) * D / 2; // Print the result cout << fullWeekScore + lastNonFullWeekScore; } // Driver Code int main() { int N = 8; findScoreSum(N); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to calculate sum // of scores after n days static void findScoreSum( int n) { // Store the number // of full weeks int F = n / 7 ; // Stores the remaining // days in the last week int D = n % 7 ; // Store the sum of scores // in the first F full weeks int fullWeekScore = ( 49 + 7 * F) * F / 2 ; // Store the sum of scores // in the last week int lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / 2 ; // Print the result System.out.println(fullWeekScore + lastNonFullWeekScore); } // Driver Code public static void main(String[] args) { int N = 8 ; findScoreSum(N); } } // This code is contributed by Kingash. |
Python3
# Python3 program for the above approach # Function to calculate sum # of scores after n days def findScoreSum(n): # Store the number # of full weeks F = n / / 7 # Stores the remaining # days in the last week D = n % 7 # Store the sum of scores # in the first F full weeks fullWeekScore = ( 49 + 7 * F) * F / / 2 # Store the sum of scores # in the last week lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / / 2 # Print the result print (fullWeekScore + lastNonFullWeekScore) # Driver Code if __name__ = = '__main__' : N = 8 findScoreSum(N) # This code is contributed by SURENDRA_GANGWAR. |
C#
// C# program for the above approach using System; class GFG{ // Function to calculate sum // of scores after n days static void findScoreSum( int n) { // Store the number // of full weeks int F = n / 7; // Stores the remaining // days in the last week int D = n % 7; // Store the sum of scores // in the first F full weeks int fullWeekScore = (49 + 7 * F) * F / 2; // Store the sum of scores // in the last week int lastNonFullWeekScore = (2 * F + D + 1) * D / 2; // Print the result Console.WriteLine(fullWeekScore + lastNonFullWeekScore); } // Driver Code static public void Main() { int N = 8; findScoreSum(N); } } // This code is contributed by rag2127 |
Javascript
<script> // JavaScript program to implement // the above approach // Function to calculate sum // of scores after n days function findScoreSum(n) { // Store the number // of full weeks let F = n / 7; // Stores the remaining // days in the last week let D = n % 7; // Store the sum of scores // in the first F full weeks let fullWeekScore = (49 + 7 * F) * F / 2; // Store the sum of scores // in the last week let lastNonFullWeekScore = (2 * F + D + 1) * D / 2; // Print the result document.write(Math.floor(fullWeekScore + lastNonFullWeekScore)); } // Driver code let N = 8; findScoreSum(N); // This code is contributed by susmitakundugoaldanga. </script> |
30
Time Complexity: O(1)
Auxiliary Space: O(1)
Contact Us