long long sumFirstN(long long n) {
if(n==0)
return 0;
return sumFirstN(n-1)+n;
}
Problem of the day
You are given an integer ‘n’.
Your task is determining the sum of the first ‘n’ natural numbers and returning it.
Input: ‘n’ = 3
Output: 6
Explanation: The sum of the first 3 natural numbers is 1 + 2 + 3, equal to 6.
The first line is an integer ‘n’, denoting the integer.
Output Format:
Return the sum of the first n natural numbers.
Note:-
You don't need to print anything. Just implement the given function.
3
6
Input: ‘n’ = 3
Output: 6
Explanation: The sum of the first 3 natural numbers is 1 + 2 + 3, equal to 6.
5
15
Input: ‘n’ = 5
Output: 15
Explanation: The sum of the first 5 natural numbers is 1 + 2 + 3 + 4 + 5, equal to 15.
The expected time complexity is O(1).
The expected space complexity is O(1).
1 <= n <= 10^9
Time Limit: 1 sec
Can we use a formula to calculate the answer?
We know that the formula to calculate the sum of the first ‘n’ natural numbers is ‘(n) * (n + 1) / 2’. So, what we will do is we will put the given ‘n’ in this formula and store it in a variable. Finally, we will return the answer.
The steps are as follows:
function sumFirstN(int n):
O(1).
We are calculating the answer in constant operation.
Hence, the time complexity is O(1).
O(1).
We are using a variable to keep the answer.
Hence, the space complexity is O(1).
Interview problems
Most simple CPP solution
long long sumFirstN(long long n) {
if(n==0)
return 0;
return sumFirstN(n-1)+n;
}
Interview problems
The most effective C++ solution
long long sumFirstN(long long n,long sum = 0) {
// Write your code here.
if(n==0){
return sum;
}
sum = n+sumFirstN(n-1,sum);
}
Easy sol. in JAVA (one test case wasn't passed)
public class Solution {
public static long ans =0;
public static long sumFirstN(long n) {
// Write your code here.
if(n==1) {
return 1;
}
long sum_nm1= sumFirstN(n-1);
long sum_n = n + sum_nm1;
return sum_n;
}
}
Interview problems
Java soln
public class Solution {
public static long sumFirstN(long n) {
int sum=0;
return printNSum(n,sum);
}
public static long printNSum(long x, int sum1){
int p=1;
while(p<=x){
sum1=sum1+p;
p++;
}
return sum1;
}
}
Interview problems
java code with easy using (am)
import java.util.*;
public class Solution {
public static long sumFirstN(long n) {
long sum=n*(n+1)/2;
return sum;
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(sumFirstN(sc.nextLong()));
}
}
Interview problems
With the help of Arithmetic formula
"As we know, the formula for the sum of the first 'n' numbers is sum = n * (n + 1) / 2;
So, if we implement this formula in our code, it would be much more efficient unless it's provided that we do it by some other method. With this approach, the time and space complexity would be O(1)." long long sumFirstN(long long n) {
// Write your code here.
long long ans=0;
ans=n*(n+1)/2;
return ans;
}
Interview problems
java answer
public static long sumFirstN(long n) {
// Write your code here.
if(n>0){
n=n+sumFirstN(n-1);
}
return n;
}
Interview problems
C++ (One line)
long long sumFirstN(long long n) {
// Write your code here.
return (n*(n +1))/2;
}
Interview problems
Easy Java Code
public class Solution {
public static long sumFirstN(long n) {
if(n<=0){
return n;
}
return n + sumFirstN(n-1);
}
}
Interview problems
Python Code - But getting TLE in last testcase
from typing import List
def sumFirstN(n: int) -> int:
if n == 0: # Base case: when n is 0, return 0
return 0
else:
return n + sumFirstN(n - 1)