Second Approach: O(n) It is observed that, given series is Geometric Progression, with common ratio = (K – 1)/K So, above expression can be simplified as:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
usingnamespacestd;
// Function to return sum
intsum(intk, intn)
{
intsum
= pow(k, n + 1)
- pow(k - 1, n + 1);
returnsum;
}
// Driver code
intmain()
{
intn = 3;
intK = 3;
cout << sum(K, n);
}
Java
// Java implementation of above approach
classGFG
{
// Function to return sum
staticintsum(intk, intn)
{
intsum = (int)(Math.pow(k, n + 1) -
Math.pow(k - 1, n + 1));
returnsum;
}
// Driver code
publicstaticvoidmain(String args[])
{
intn = 3;
intK = 3;
System.out.print(sum(K, n));
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Function to return sum
defsum(k, n):
sum=(pow(k, n +1) -
pow(k -1, n +1));
returnsum;
# Driver code
n =3;
K =3;
print(sum(K, n));
# This code is contributed by mits
C#
// C# implementation of above approach
usingSystem;
classGFG
{
// Function to return sum
staticintsum(intk, intn)
{
intsum = (int)(Math.Pow(k, n + 1) -
Math.Pow(k - 1, n + 1));
returnsum;
}
// Driver code
publicstaticvoidMain()
{
intn = 3;
intK = 3;
Console.Write(sum(K, n));
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// Function to return sum
functionsum($k, $n)
{
$sum= pow($k, $n+ 1) -
pow($k- 1, $n+ 1);
return$sum;
}
// Driver code
$n= 3;
$K= 3;
echosum($K, $n);
// This code is contributed
// by Akanksha Rai
Javascript
<script>
// Javascript implementation of the approach
// Function to return sum
functionsum(k,n)
{
let sum = 0;
for(let i = 0; i <= n; i++) {
let p = 1;
for(let j = 0; j < n - i; j++) {
p = p * k;
}
for(let j = 0; j < i; j++) {
p = p * (k - 1);
}
sum = sum + p;
}
returnsum;
}
// Driver code
let n = 3;
let K = 3;
document.write(sum(K, n));
// This code is contributed by unknown2108
</script>
Output:
65
Time Complexity: O( n )
Auxiliary Space: O(1)
Third Approach (Efficient): O(log n) Note: Time complexity can further be reduced to O(log(n)), by calculating power in log(n) complexity. Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
usingnamespacestd;
// Recursive C program to compute modular power
intexponent(intA, intB)
{
// Base cases
if(A == 0)
return0;
if(B == 0)
return1;
// If B is even
longy;
if(B % 2 == 0) {
y = exponent(A, B / 2);
y = (y * y);
}
// If B is odd
else{
y = A;
y = (y * exponent(A, B - 1));
}
returny;
}
// Function to return sum
intsum(intk, intn)
{
intsum = exponent(k, n + 1)
- exponent(k - 1, n + 1);
returnsum;
}
// Driver code
intmain()
{
intn = 3;
intK = 3;
cout << sum(K, n);
}
Java
importjava.lang.Math;
classGFG
{
// Recursive C program to compute modular power
staticintexponent(intA, intB)
{
// Base cases
if(A == 0)
return0;
if(B == 0)
return1;
// If B is even
inty;
if(B % 2== 0)
{
y = exponent(A, B / 2);
y = (y * y);
}
// If B is odd
else
{
y = A;
y = (y * exponent(A, B - 1));
}
returny;
}
// Function to return sum
staticintsum(intk, intn)
{
intsum = exponent(k, n + 1)
- exponent(k - 1, n + 1);
returnsum;
}
// Driver code
publicstaticvoidmain(String[] args)
{
intn = 3;
intK = 3;
System.out.println(sum(K, n));
}
}
// This code is contributed by Code_Mech.
Python3
# Recursive python3 program to compute modular power
defexponent(A, B):
# Base cases
if(A ==0):
return0;
if(B ==0):
return1;
# If B is even
if(B %2==0):
y =exponent(A, B /2);
y =(y *y);
# If B is odd
else:
y =A;
y =(y *exponent(A, B -1));
returny;
# Function to return sum
defsum(k, n):
sum=exponent(k, n +1) -exponent(k -1, n +1);
returnsum;
# Driver code
n =3;
K =3;
print(sum(K, n));
# This code has been contributed by 29AjayKumar
C#
// C# program of above approach
usingSystem;
classGFG
{
// Recursive C program to compute modular power
staticintexponent(intA, intB)
{
// Base cases
if(A == 0)
return0;
if(B == 0)
return1;
// If B is even
inty;
if(B % 2 == 0)
{
y = exponent(A, B / 2);
y = (y * y);
}
// If B is odd
else
{
y = A;
y = (y * exponent(A, B - 1));
}
returny;
}
// Function to return sum
staticintsum(intk, intn)
{
intsum = exponent(k, n + 1)
- exponent(k - 1, n + 1);
returnsum;
}
// Driver code
publicstaticvoidMain()
{
intn = 3;
intK = 3;
Console.WriteLine(sum(K, n));
}
}
// This code is contributed by Code_Mech.
PHP
<?php
// Recursive C program to compute modular power
functionexponent($A, $B)
{
// Base cases
if($A== 0)
return0;
if($B== 0)
return1;
// If B is even
if($B% 2 == 0)
{
$y= exponent($A, $B/ 2);
$y= ($y* $y);
}
// If B is odd
else
{
$y= $A;
$y= ($y* exponent($A, $B- 1));
}
return$y;
}
// Function to return sum
functionsum($k, $n)
{
$sum= exponent($k, $n+ 1) -
exponent($k- 1, $n+ 1);
return$sum;
}
// Driver code
$n= 3;
$K= 3;
echosum($K, $n);
// This code is contributed by Akanksha Rai
?>
Javascript
<script>
// Recursive Javascript program to
// compute modular power
functionexponent(A, B)
{
// Base cases
if(A == 0)
return0;
if(B == 0)
return1;
// If B is even
let y;
if(B % 2 == 0)
{
y = exponent(A, parseInt(B / 2, 10));
y = (y * y);
}
// If B is odd
else
{
y = A;
y = (y * exponent(A, B - 1));
}
returny;
}
// Function to return sum
functionsum(k, n)
{
let sum = exponent(k, n + 1) -
exponent(k - 1, n + 1);
returnsum;
}
// Driver code
let n = 3;
let K = 3;
document.write(sum(K, n));
// This code is contributed by divyeshrabadiya07
</script>
Output:
65
Time Complexity: O(logn)
Auxiliary Space: O(logn)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!