Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Most Significant Bit Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Most Significant Bit

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

The most significant bit of a number n is the left most set bit in its binary representation. The significance of that bit is determined by its position. Given a number n, find the significance of the most significant bit, i.e., find the greatest number less than or equal to n which is the power of two.

Naive Approach

The naive approach is to keep dividing the number by 2, and keeping the track of the index of the last significant bit encountered. We keep doing this till the number is reduced to zero.

Analysis

  • Time Complexity: O(log2n)
  • Space Complexity: O(1)

Implementation

C++
int msbSignificance(int n) {
    int index = 0, count = 0;
	while(n != 0) {
		count++;
		if(n % 2 == 1) {
			index = count;
		}
		n /= 2;
	}
	return (1 << (index - 1));
}
Java
class Solution {
    int msbSignificance(int n) {
		int index = 0, count = 0;
		while(n != 0) {
			count++;
			if(n % 2 == 1) {
				index = count;
			}
			n /= 2;
		}
		return (1 << (index - 1));
    }
}

Optimal Approach

The optimal approach is to use the formula that MSB = floor(log2n). 

Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

Implementation

C++
int msbSignificance(int n) {
    int index = floor(log2(n));
	return (1 << index);
}
Java
class Solution {
    int msbSignificance(int n) {
		int index = (int)(Math.log(n) / Math.log(2));
		return (1 << index);
    }
}
Related Content
Count Set Bits
Divide without Division, Multiplication & Mod
Power Of Two
Repeat and Missing Number in Array
Square without Multiplication, Division & Pow
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet