āš”ļø
DSA Important Questions
  • šŸ˜‡README
  • šŸ”«Question List
  • 🧪Formula List
  • šŸ—’ļø01 Array
    • āœ…Rotate Matrix
    • āœ…Max Sum Contiguous Subarray
    • Find Duplicate in Array
    • āœ…Merge Intervals
    • āœ…Spiral Order Matrix I
    • Repeat and Missing Number Array
    • āœ…Merge Overlapping Intervals
    • āœ…Set Matrix Zeros
    • āœ…Spiral Order Matrix II
    • Largest Number
    • First Missing Integer
    • āœ…Pascal Triangle
    • ⭐Max Distance
    • āœ…Wavy Array
    • Next Permutation
    • āœ…Min Steps in Infinite Grid
    • Flip
    • ⭐Find Permutation
    • ⭐Maximum Absolute Difference
    • ⭐Maximum Unsorted Subarray
    • Reorder Data in Log File
    • āœ…Make equal elements Array
  • āž•02 Math
    • āœ…Excel Column Number
    • ⭐Excel Column Title
    • ⭐Grid Unique Paths
    • ⭐Power Of Two Integers
    • āœ…Next Similar Number
    • ⭐k-th Permutation
  • šŸ”03 Binary Search
    • ⭐Median of Array
    • ⭐Square Root of Integer
    • ⭐Rotated Sorted Array Search
    • ⭐Matrix Median
    • Capacity To Ship Packages Within B Days
  • 🧵04 String
    • Implement StrStr
    • ⭐Integer to Roman
    • ⭐Roman to Integer
    • Length of Last Word
    • ⭐atoi
    • ⭐Valid IP Addresses
    • ⭐Compare Version Numbers
    • ⭐Longest Palindromic Substring
    • Count And Say
    • Reverse the String
    • Power of 2
    • 🚧KMP: Minimum Characters Required to Make a String Palindromic
    • ⭐Convert to Palindrome
    • Bulls and Cows
  • 1ļøāƒ£05 Bit Manipulation
    • Reverse Bits
    • Single Number
    • ⭐Divide Integers
    • ⭐Single Number II
    • ⭐Count Total Set Bits
    • ⭐Palindromic Binary Representation
  • āœŒļø06 Two Pointers
    • Merge Two Sorted Lists II
    • ⭐3 Sum
    • Remove Duplicates from sorted Array
    • ⭐Container With Most Water
    • Remove Element from Array
    • ⭐Max Continuous Series of 1s
    • Pair With Given Difference
    • ⭐Maximum Ones After Modification
  • šŸ”—07 Linked List
    • Swap List Nodes in Pairs
    • Rotate List
    • ⭐Reorder List
    • Merge Two Sorted Lists
    • Remove Duplicates from Sorted List
    • Add Two Numbers as Lists
    • Remove Nth Node from List End
    • ⭐List Cycle
    • Intersection of Linked Lists
    • ⭐Reverse Linked List II
    • Palindrome List
    • ⭐K reverse linked list
    • ⭐Reverse Alternate K Nodes
    • ⭐Kth Node From Middle
    • ⭐Sort Binary Linked List
    • ⭐Even Reverse
  • šŸ“š08 Stacks and Queues
    • ⭐Rain Water Trapping
    • Generate all Parentheses
    • ⭐Largest Rectangle in Histogram
    • ⭐Min Stack
    • Redundant Braces
    • Nearest Smaller Element
    • ⭐First non-repeating character in a stream of characters
    • āœ…Balanced Parantheses!
  • šŸ”™09 Backtracking
    • ⭐Kth Permutation Sequence
    • ⭐Combination Sum
    • Combination Sum II
    • ⭐NQueens
    • Combinations
    • ⭐Subsets II
    • Subset
    • Palindrome Partitioning
  • šŸ’±10 Hashing
    • ⭐Longest Consecutive Sequence
    • ⭐4 Sum
    • ⭐Anagrams
    • ⭐Points on the Straight Line
    • 2 Sum
    • ⭐Valid Sudoku
    • Copy List
    • Longest Substring Without Repeat
  • šŸ—»11 Heaps & Maps
    • Merge K Sorted Lists
    • ⭐LRU Cache
    • ⭐Inversions
    • Distinct Numbers in Window
  • 🌳12 Tree Data Structure
    • āœ…Inorder Traversal
    • ⭐Recover Binary Search Tree
    • ⭐Inorder Traversal of Cartesian Tree
    • ⭐Least Common Ancestor
    • ⭐Construct Binary Tree From Inorder And Preorder
    • Flatten Binary Tree to Linked List
    • Valid Binary Search Tree
    • āœ…Preorder Traversal
    • ⭐Binary Tree From Inorder And Postorder
    • Balanced Binary Tree
    • āœ…Sorted Array To Balanced BST
    • Symmetric Binary Tree
    • āœ…Postorder Traversal
    • ⭐Populate Next Right Pointers Tree
    • Identical Binary Trees
    • BST Iterator
    • ZigZag Level Order Traversal BT
    • Path Sum
    • Next Pointer Binary Tree
    • Min Depth of Binary Tree
    • Root to Leaf Paths With Sum
    • ⭐Kth Smallest Element in Tree
    • ⭐2-Sum Binary Tree
    • Vertical Order traversal of Binary Tree
    • ⭐Diagonal Traversal
    • Cousins in Binary Tree
    • Path to Given Node
    • Remove Half Nodes
    • Merge two Binary Tree
    • ⭐Burn a Tree
    • Nodes at Distance K
    • Vertical Sum of a Binary Tree
    • Covered / Uncovered Nodes
  • 13 Dynamic Programming
    • Ugly Number II
    • Coin Change
    • 0 - 1 Knapsack Problem
    • Permutation Coefficient
  • 14 Greedy Algorithm
    • ⭐Gas Station
    • ⭐Majority Element
    • ⭐Distribute Candy
    • ⭐Highest Product
    • Assign Mice to Holes
    • ⭐Meeting rooms
  • šŸ‘Øā€šŸ‘©ā€šŸ‘§ā€šŸ‘¦15 Graph
    • Clone Graph
    • Word Search Board
    • ⭐Stepping Numbers
    • Black Shapes
    • Knight On Chess Board
    • āŒSmallest Multiple With 0 and 1
    • ⭐Commutable Islands
    • Possibility of finishing all courses given pre-requisites
    • āŒValid Path
    • Cycle in Directed Graph
    • āŒCycle in Undirected Graph
Powered by GitBook
On this page
  1. 04 String

Power of 2

PreviousReverse the StringNextKMP: Minimum Characters Required to Make a String Palindromic

Last updated 3 years ago

Thoughts
  • 0:00 This sounds like a super easy question that I should be able to solve in 2 minutes. But since this question is in the string section, so I don't think it's going to be that simple. I'll try it once.

  • 10:00 It's not simple at all. The question asks to check if the given number, which is very huge and in a string, is a power of two or not.

  • So it's very similar to the factorial problem where we need to do multiplication manually.

  • I'm getting some errors. .

  • 25:00 Turns out I messed up in the multiplication function. I forgot to reverse the result of the multiplication.

  • Also, I forgot some edge cases.

  • 28:00 Done from my side. Let me take a loot at the given solution.

  • Their solution is not even following the constraint. They are converting the string to int and then doing the basic bit manipulation technique.

  • This reminds me that I have forgotten the bit manipulation technique to check for the power of two. Time to see notes.

int comp(string B, string A){
    if(B.size() == A.size())
        return (B < A ? -1 : B > A ? 1 : 0);
    return (B.size() < A.size() ? -1 : 1);
}

string timesTwo(string B) {
    reverse(B.begin(), B.end());
    int carry = 0;
    string res = "";
    int i = 0; 
    while(i < B.size()) {
        int prod = (B[i] - '0') * 2 + carry;
        res.push_back((prod % 10) + '0');
        carry = prod / 10;
        i++;
    }
    
    while(carry) {
        res.push_back((carry % 10) + '0');
        carry /= 10;
    }
    
    reverse(res.begin(), res.end());
    return res;
}

int Solution::power(string A) {
    // edge case
    if(A == "1" || A[0] == '-')
        return 0;
    string B = "1";
    while(comp(B, A) == -1) 
        B = timesTwo(B);
        
    return (comp(B, A) == 0);
}

Time Complexity: (nlog⁔n)(n \log n)(nlogn)​

Space Complexity: O(n)O(n)O(n)​ for products of 222​.

Edge Cases:

  • You initialized res = 1 But is 111​ being equal to given number a valid case?

  • What if the input is negative?

🧵
I'll take it to OnlineGDB
Power of 2 - InterviewBitInterviewBit
Logo