Binary Tree Zigzag Level Order Traversal in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:12:20

835 Views

Binary tree zigzag level order traversal visits nodes level by level, alternating direction each level. The first level goes left to right, second level right to left, third level left to right, and so on. 3 9 20 15 7 ... Read More

Keys and Rooms in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:11:53

609 Views

The Keys and Rooms problem is a graph traversal challenge where we need to determine if all rooms can be visited starting from room 0. Each room contains keys to other rooms, and we need to check if we can reach every room using available keys. Problem Understanding Given N rooms numbered 0 to N-1, we start in room 0. Each room i contains a list of keys rooms[i], where each key opens a specific room. We need to return True if we can visit all rooms, False otherwise. Key constraints ? Initially, all rooms ... Read More

Maximum Swap in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:11:31

1K+ Views

The Maximum Swap problem asks us to find the maximum possible number by swapping at most two digits in a given non-negative integer. For example, given 2736, we can swap digits 2 and 7 to get 7236, which is the maximum possible value. Algorithm Overview The approach works by comparing the original number with its digits sorted in descending order. When we find the first position where they differ, we perform the optimal swap ? Convert the number into a list of digits Create a sorted version in descending order to find the target arrangement Find ... Read More

Palindromic Substrings in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:11:09

6K+ Views

A palindromic substring is a substring that reads the same forwards and backwards. Given a string, we need to count all palindromic substrings where substrings with different start or end indices are counted separately, even if they contain the same characters. For example, in the string "aaa", there are 6 palindromic substrings: "a", "a", "a", "aa", "aa", "aaa". Brute Force Approach The simplest approach is to check every possible substring and verify if it's a palindrome ? class Solution: def countSubstrings(self, s): counter ... Read More

Minimum Moves to Equal Array Elements II in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:10:49

550 Views

Finding the minimum number of moves to make all array elements equal is a classic problem that can be solved efficiently using the median approach. A move consists of incrementing or decrementing any element by 1. Algorithm Overview The key insight is that the optimal target value is the median of the array. This minimizes the total distance (moves) needed to make all elements equal ? Steps to Solve Sort the array to find the median easily Find the median element (middle element of sorted array) ... Read More

4Sum II in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:10:29

865 Views

The 4Sum II problem asks us to find how many tuples (i, j, k, l) exist such that A[i] + B[j] + C[k] + D[l] equals zero, given four lists A, B, C, D of integers. This is an optimization problem that can be solved efficiently using a hash map approach. Problem Understanding Given four lists of integers with the same length N (0 ≤ N ≤ 500), we need to count tuples where the sum of elements at indices (i, j, k, l) from lists A, B, C, D respectively equals zero. For example, with A = ... Read More

Hexspeak in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:10:03

991 Views

Hexspeak is a novelty form of spelling that uses hexadecimal digits to represent words. In Python, we can convert decimal numbers to Hexspeak by first converting to hexadecimal, then replacing specific digits with letters. Understanding Hexspeak Conversion The conversion process involves these steps: Convert decimal number to hexadecimal Replace digit 0 with letter 'O' and digit 1 with letter 'I' Keep hexadecimal letters A-F unchanged If any other digits (2-9) exist, return "ERROR" Valid Hexspeak characters are: {"A", "B", "C", "D", "E", "F", "I", "O"} Implementation Using Built-in hex() Function Here's a ... Read More

Diet Plan Performance in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:09:38

2K+ Views

A diet plan performance problem tracks a dieter's points based on their calorie consumption over consecutive k days. The dieter gains or loses points depending on whether their total calories fall below a lower bound, above an upper bound, or within the normal range. Problem Description Given an array calories[i] representing daily calorie consumption, we need to evaluate every consecutive sequence of k days and calculate points based on these rules: If total calories < lower bound: lose 1 point If total calories > upper bound: gain 1 point Otherwise: no change in points ... Read More

Single-Row Keyboard in python

Arnab Chakraborty
Updated on 25-Mar-2026 08:09:16

483 Views

A single-row keyboard requires finger movement between keys to type characters. Given a keyboard layout string and a word, we need to calculate the total time to type the word, where time equals the absolute distance between character positions. Problem Understanding Consider a keyboard layout "abcdefghijklmnopqrstuvwxyz" and word "hello": Start at index 0 (position 'a') Move to 'h' (index 7): distance = |0 - 7| = 7 Move to 'e' (index 4): distance = |7 - 4| = 3 Move to 'l' (index 11): distance = |4 - 11| = 7 Stay at 'l' (index 11): ... Read More

Check If a Number Is Majority Element in a Sorted Array in Python

Arnab Chakraborty
Updated on 25-Mar-2026 08:08:55

402 Views

A majority element in an array is an element that appears more than N/2 times in an array of length N. When dealing with a sorted array, we can use binary search to efficiently find the first and last occurrence of the target element and check if its frequency exceeds the majority threshold. Algorithm Overview To solve this problem, we need to: Find the leftmost (first) occurrence of the target using binary search Find the rightmost (last) occurrence of the target using binary search ... Read More

Advertisements