VOOZH about

URL: https://www.geeksforgeeks.org/dsa/multiplication-power-2/

⇱ Multiplication with a power of 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Multiplication with a power of 2

Last Updated : 26 May, 2022

Given two numbers x and n, we need to multiply x with 2n
Examples : 
 

Input : x = 25, n = 3
Output : 200
25 multiplied by 2 raised to power 3
is 200.

Input : x = 70, n = 2
Output : 280
 


 


A simple solution is to compute n-th power of 2 and then multiply with x. 
 

Output : 

280


Time complexity : O(logn)

Auxiliary Space : O(logn)
An efficient solution is to use bitwise leftshift operator. We know 1 << n means 2 raised to power n.
 

Output : 

280

Time Complexity : O(1)

Auxiliary Space: O(1)


 

Comment
Article Tags: