Skip to content
Related Articles
Open in App
Not now

Related Articles

Sum of all N elements of the given series having X as its first integer

Improve Article
Save Article
  • Last Updated : 01 Feb, 2023
Improve Article
Save Article

Given two Integers N and X in which N is the total number of elements of the series that starts with X (say A). The ith element of the series Ai is defined as the sum of all the elements from A0 to Ai-1. The task is to find the sum of the N elements of the series.

Note: The answer can be very large. Therefore, print it in modulo 109 +7.

Examples:

 Input: N = 5, X = 3
Output: 48 
Explanation: The first 5 terms are: 3, 3, 6, 12, 24
The sum of these are (3 + 3 + 6 + 12 + 24) = 48.

Input: N = 4, X = 26 
Output: 208 

Approach: The problem can be solved based on the following mathematical idea:

Say, the ith term of the series is represented by Ai and sum till ith term is represented by Si
As per the definition of the series we can say that Ai = Si-1 .

So,  
A0. A0 = X and S0 = X = A0
A1 = A0 = S0 and S1 = A0 + A1 = A0 + A0 = 2*A0 [By substituting the value of S0]  
A2 = S1 = 2*A0 and S2 = (A0 + A1) + A2 = 2*A0 + 2* A0 = 4*A0 = 22 * A0 

So we can see that the sum of first N terms is X * 2N-1 .

Steps were taken to solve the problem:

  • Calculate 2N-1 with arithmetic modulus. 
  • Multiply 2N-1 with X and print the answer in modulo by using the multiplication property of arithmetic modulo.
  • (a*b)%mod = ((a % mod) * (b % mod)) % mod.

Below is the implementation of the above the approach.

C++




// C++ code to implement approach
 
#include <iostream>
using namespace std;
#define ll long long
 
// Mod value
ll M = 1000000007 ;
 
// Function to calculate 2^(n-1) with Modulo Arithmetic
ll power(ll a, ll n){
   
  ll result = 1 ;
   
  while(n > 0){
    result = (result * a) % M ;
    n-- ;
  }
   
  return result ;
}
 
// Function which is called in main()
ll Total_Sum(ll n, ll x){
   
  // Calculating 2^(n-1) using user defined power function above
  ll r_pow = power(2, n-1) ;
   
  // calculating final answer with Modulo Arithmetic
  ll ans = ((x % M) * r_pow ) % M ;
   
  return ans ;
   
}
 
// Main function
int main() {
 
      // Input values of N and X
    ll N = 5, X = 3;
   
      // Function Call and Printing the required value
      cout << Total_Sum(N, X);
   
    return 0;
}
 
// This code is contributed by rahulbhardwaj0711


Java




// Java code to implement approach
 
import java.util.*;
class GFG {
 
    // Driver function
    public static void main(String[] args)
    {
        // Input value of N and X
        long N = 5, X = 3;
 
        // Function call
        Total_Sum(N, X);
    }
 
    // Function which is called in main()
    static void Total_Sum(long n, long x)
    {
 
        // Mod value
        final long M = 1000000007;
 
        // Calculating 2^(N-1) using user-defined
        // power function written below
        long r_pow = power(2, n - 1);
 
        // Calculating 2^(N-1)*X with arithmetic modulo
        long ans = ((x % M) * (r_pow % M)) % M;
 
        // Printing answer
        System.out.println(ans);
    }
 
    // Function to calculate 2^(N-1) with Modulo arithmetic
    static long power(long a, long n)
    {
        final long M = 1000000007;
        long result = 1;
        while (n > 0) {
            if ((n & 1) == 1)
                result = (result * a) % M;
            n >>= 1;
            a = (a * a) % M;
        }
        return result;
    }
}


Python3




# Python3 code to implement approach
 
# Mod value
M = 1000000007
 
# Function to calculate 2^(n-1) with Modulo Arithmetic
def power(a, n):
    result = 1
    while(n > 0):
        result = (result * a) % M
        n -= 1
    return result
 
# Function which is called in main()
def Total_Sum(n, x):
   
    # Calculating 2^(n-1) using user defined power function above
    r_pow = power(2, n-1)
 
    # calculating final answer with Modulo Arithmetic
    ans = ((x % M) * r_pow) % M
 
    return ans
 
# Main function
if __name__ == "__main__":
   
    # Input values of N and X
    N = 5
    X = 3
    # Function Call and Printing the required value
    print(Total_Sum(N, X))
 
# This code is contributed by Rohit Pradhan


C#




// C# code to implement approach
using System;
 
public class GFG {
 
  // Function which is called in main()
  static void Total_Sum(long n, long x)
  {
 
    // Mod value
    long M = 1000000007;
 
    // Calculating 2^(N-1) using user-defined
    // power function written below
    long r_pow = power(2, n - 1);
 
    // Calculating 2^(N-1)*X with arithmetic modulo
    long ans = ((x % M) * (r_pow % M)) % M;
 
    // Printing answer
    Console.WriteLine(ans);
  }
 
  // Function to calculate 2^(N-1) with Modulo arithmetic
  static long power(long a, long n)
  {
    long M = 1000000007;
    long result = 1;
    while (n > 0) {
      if ((n & 1) == 1)
        result = (result * a) % M;
      n >>= 1;
      a = (a * a) % M;
    }
    return result;
  }
 
  static public void Main()
  {
 
    // Input value of N and X
    long N = 5, X = 3;
 
    // Function call
    Total_Sum(N, X);
  }
}
 
// This code is contributed by lokeshmvs21.


Javascript




<script>
       // JavaScript code for the above approach
 
       // Mod value
       let M = 1000000007;
 
       // Function to calculate 2^(n-1) with Modulo Arithmetic
       function power(a, n) {
 
           let result = 1;
 
           while (n > 0) {
               result = (result * a) % M;
               n--;
           }
 
           return result;
       }
 
       // Function which is collected in main()
       function Total_Sum(n, x) {
 
           // Calculating 2^(n-1) using user defined power function above
           let r_pow = Math.pow(2, n - 1);
 
           // calculating final answer with Modulo Arithmetic
           let ans = ((x % M) * r_pow) % M;
 
           return ans;
 
       }
 
       // Main function
 
       // Input values of N and X
       let N = 5, X = 3;
 
       // Function Call and Printing the required value
       document.write(Total_Sum(N, X));
 
// This code is contributed by Potta Lokesh
 
   </script>


Output

48

Time Complexity: O(N)
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!