Given two numbers L and R. The task is to find the sum of numbers in the range L to R.
Examples:
Input: L = 3, R = 6
Output: 40
Explanation: 3 + 3+4 + 3+4+5 + 3+4+5+6 = 40Input: L = 5, R = 6
Output: 16
Approach: This problem is formula-based. For the illustration given below, observe the number of times each number is repeating in the sum, and depending upon that the final sum is calculated.
Illustration: L = 3, R = 6
Sum = 3 + 3+4 + 3+4+5 + 3+4+5+6 = 3+3+3+3 + 4+4+4 + 5+5 + 6 (Upon Grouping)
That is equals to 3*4 + 4*3 + 5*2 + 6*1
Therefore for any range L to R, the sum can be calculated as:
L*D + (L+1)*(D-1) + (L+2)*(D-2) + … + (R-1)*(2) + R*1
Below is the implementation of above approach.
C++
// C++ program for above approach #include <iostream> using namespace std; // Function to return sum int findSum( int L, int R) { // Initializing the variables int sum = 0, d = R - L + 1; for ( int i = L; i <= R; i++) { sum += (i * d); d--; } // Return Sum as the final result. return sum; } // Driver Code int main() { int L = 3, R = 6; // Function call cout << findSum(L, R); return 0; } |
Java
// Java code to implement above approach import java.util.*; public class GFG { // Function to return sum static int findSum( int L, int R) { // Initializing the variables int sum = 0 , d = R - L + 1 ; for ( int i = L; i <= R; i++) { sum += (i * d); d--; } // Return Sum as the final result. return sum; } // Driver code public static void main(String args[]) { int L = 3 , R = 6 ; // Function call System.out.println(findSum(L, R)); } } // This code is contributed by Samim Hossain Mondal. |
Python
# Python program for above approach # Function to return sum def findSum(L, R): # Initializing the variables sum = 0 d = R - L + 1 for i in range (L, R + 1 ): sum + = (i * d) d = d - 1 # Return Sum as the final result. return sum # Driver Code L = 3 R = 6 # Function call print (findSum(L, R)) # This code is contributed by Samim Hossain Mondal. |
C#
// C# code to implement above approach using System; public class GFG { // Function to return sum static int findSum( int L, int R) { // Initializing the variables int sum = 0, d = R - L + 1; for ( int i = L; i <= R; i++) { sum += (i * d); d--; } // Return Sum as the final result. return sum; } // Driver code public static void Main() { int L = 3, R = 6; // Function call Console.WriteLine(findSum(L, R)); } } // This code is contributed by ukasp. |
Javascript
<script> // JavaScript code for the above approach // Function to return sum function findSum(L, R) { // Initializing the variables let sum = 0, d = R - L + 1; for (let i = L; i <= R; i++) { sum += (i * d); d--; } // Return Sum as the final result. return sum; } // Driver Code let L = 3, R = 6; // Function call document.write(findSum(L, R)); // This code is contributed by Potta Lokesh </script> |
40
Time Complexity: O(R-L+1)
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!