Skip to main content

Posts

MSB bits

  #!/bin/python3 import math import os import random import re import sys # # Complete the 'getOneBits' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts INTEGER n as parameter. # def getOneBits ( n ):     # Write your code here     if n == 0 :         return [ 0 ]             binary = bin ( n )[ 2 :]     d = []     pos = 1     temp = n     for idx , bit in enumerate ( binary , 1 ) :         if bit == '1' :                         d . append ( idx )             return [ len ( d )] + d     if __name__ == '__main__' :     fptr = open ( os . environ [ 'OUTPUT_PATH' ], 'w' )     n = int ( input () . strip ())     result = getOneBits ( n )     fptr ...

int to Roman Number

  #!/bin/python3 import math import os import random import re import sys # # Complete the 'romanizer' function below. # # The function is expected to return a STRING_ARRAY. # The function accepts INTEGER_ARRAY numbers as parameter. # def romanizer ( numbers ):     # Write your code here     def intToRoman ( num ):         val = [             1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1         ]         syms = [             "M" , "CM" , "D" , "CD" ,             "C" , "XC" , "L" , "XL" ,             "X" , "IX" , "V" , "IV" ,             "I"         ]         roman = ""         i = 0         while num > 0 :           ...

binary search Algorithm

Guide: Binary Search is used to find target integer in a sorted array quickly. Binary search has left , right , and mid variables, but the target variable is what binary search is used for. Assume num2 is a sorted array in which we are searching for target. The Match: If nums2[mid] is exactly equal to your target, you've found a common number! You can immediately return it. Go Right: If your target is greater than nums2[mid], that means your target has to be in the right half of nums2. Move your left pointer to mid + 1. Go Left: If your target is smaller than nums2[mid], your target must be in the left half. Move your right pointer to mid - 1. //java int left = 0; int right = nums2.length - 1; int mid = 0; int target = 0; while(left <= right) { mid = left + (right - left) / 2; if(nums1[i] == nums2[mid]) { return target; } else { if(target < nums2[mid]) { right = mid - 1; } else { ...

union-find

  Union-Find (or Disjoint Set Union, DSU) is a data structure that tracks elements partitioned into disjoint subsets, supporting rapid merging () and representative finding () operations. Optimized with path compression and union by rank/size, it achieves near-constant amortized time complexity, $O(\alpha(n))$ , making it efficient for Kruskal’s algorithm and dynamic connectivity. [ 1 , 2 , 3 , 4 ] Key Concepts and Operations Find(x): Determines the representative (root) of the set containing element . Union(x, y): Merges the sets containing elements and . MakeSet(x): Initializes a new set containing only element . Structure: Typically implemented as a forest of trees, where each node points to its parent. The root of a tree is its own parent and acts as the representative. [ 4 , 5 , 6 , 7 , 8 ] Optimizations Path Compression: During a operation, makes every node on the path point directly to the root, flattening the tree. Union by Rank/Size: Always attaches the smaller tr...

Balancing brackets

  Balancing brackets  = checking whether every opening bracket has a matching closing bracket in the correct order. Types usually considered: () [] {} Core idea (stack method) Use a  stack : Read characters left → right. If opening bracket → push onto stack. If closing bracket → stack empty → invalid top doesn’t match → invalid else pop. End → stack must be empty. Time:  O(n) Space:  O(n) Example Input: {[()]} Process: { push [ push ( push ) pop ] pop } pop Stack empty → balanced. Python (clean version) def is_balanced(s): stack = [] pairs = {')': '(', ']': '[', '}': '{'} for ch in s: if ch in "([{": stack.append(ch) elif ch in ")]}": if not stack or stack[-1] != pairs[ch]: return False stack.pop() return len(stack) == 0 Edge cases people miss "(]"  → order mismatch "((("  → leftover opens "))...

3+ prompt for Every Developer

optimization check for optimizations.optimization can be reduction of code.It can be like converting code more organised by rearranging thing so code is mainatable and revent peaces of code is at one place itself. you can use oops style too. implement changes without introducing or inventing new bugs Development plan the pieces in the code.keep them together.test every piece with robust test before integrating them together .here pieces can be functions.