New update is available. Click here to update.
sidenav-btnClose
Topic list
Sum or Product
EASY
15 mins
1245 upvotes
Other topics
Problem solved
Skill meter
Strings
-
Matrices (2D Arrays)
-
Sorting
-
Binary Search
-
Linked List
-
Stacks & Queues
-
Trees
-
Graph
-
Dynamic Programming
-
Greedy
-
Tries
-
Arrays
-
Binary Search Trees
-
Heap
-
Bit Manipulation
-

Sum or Product

Contributed by
Ankush Gupta
Easy
Avg time to solve 15 mins
Success Rate 80 %
Share
1245 upvotes

Problem Statement

You are given a number ‘N’ and a query ‘Q.’ If ‘Q’ is 1, then you have to return the sum of all integers from 1 to ‘N,’ else if ‘Q’ is equal to 2 then you have to return the product of all integers from 1 to ‘N.’ Since the product can be very large, return it modulo 10 ^ 9 + 7.

For example

Given ‘N’ = 4, ‘Q’ = 1. 
Then the answer is 10 because the sum of all integers between 1 and 4 are 1, 2, 3, and 4. Hence 1 + 2 + 3 + 4 is equal to 10.
Detailed explanation ( Input/output format, Notes, Constraints, Images )

Sample Input 1 :

2
4 1 
4 2

Sample Output 1 :

10
24 

Explanation of the Sample Input 1:

In the first test case, the answer is 10 because all integers between 1 and 4 are 1, 2, 3, and 4. Hence 1 + 2 + 3 + 4 is equal to 10.


In the second test case, the answer is 25 because all integers between 1 and 4 are 1, 2, 3, and 4. Hence 1 * 2 * 3 * 4 is equal to 24.

Sample Input 2 :

2
5 1
5 2 

Sample Output 2 :

15
120
Reset Code
Full screen
copy-code
Console