A bitonic sequence is a sequence of numbers that first increases strictly, reaches a peak (the bitonic point), then decreases strictly. We need to find this peak element efficiently using binary search. The bitonic point is the maximum element where the left neighbor is smaller and the right neighbor is also smaller. Algorithm We use binary search to find the bitonic point in O(log n) time ? Compare the middle element with its neighbors If array[mid-1] < array[mid] > array[mid+1], we found the bitonic point If array[mid] < array[mid+1], the peak is in the right ... Read More
Sometimes we need to construct an array that requires exactly k recursive calls when sorted using merge sort. This problem involves understanding how merge sort's recursive structure works and manipulating an array to achieve the desired call count. Given two numbers a and b, we need to find an array containing values in range [1, a] that requires exactly b number of recursive merge sort calls. Understanding the Problem Merge sort makes recursive calls by dividing the array into halves. The total number of calls follows a pattern based on the array structure and element positions. ... Read More
Given an array of integers, we need to find an element that divides the array into two subarrays with equal product. If no such element exists, return -1. For example, in the array [2, 5, 3, 2, 5], the element 3 at index 2 divides it into subarrays [2, 5] and [2, 5], both having product 10. Algorithm We use prefix and suffix product arrays to efficiently calculate products of left and right subarrays ? Create a prefix product array storing cumulative products from left Create a suffix product array storing cumulative products from right ... Read More
In Python, we can find an element in an array where elements form a strictly decreasing sequence followed by a strictly increasing sequence. The element we seek is the transition point between these two sequences. Problem Requirements The solution must satisfy these conditions: Both decreasing and increasing sequences must have minimum length 2 The last value of the decreasing sequence is the first value of the increasing sequence No duplicate elements are allowed (strictly decreasing/increasing) For example, in array [5, 4, 3, 4], the sequence [5, 4, 3] is strictly decreasing and [3, 4] ... Read More
When working with digit-to-character mappings, we often need to generate all possible string combinations from a given number. This is similar to how old mobile phone keypads worked, where each digit mapped to multiple letters. Problem Understanding Given a mapping where each digit (1-9) corresponds to a list of characters, we need to find all possible strings that can be formed from a number. The key constraint is that we must use the same character for every occurrence of a digit in the number. 1 → ['A', 'B', 'C'] 2 → ['D', 'E', 'F'] ... Read More
Finding all rectangles filled with 0s in a binary 2D matrix is a common problem in computer vision and image processing. We need to identify the starting and ending coordinates of each rectangular region containing only zeros. The problem requires us to find rectangles that are separated (don't touch each other) but can touch array boundaries. Each rectangle is represented by four coordinates: [start_row, start_col, end_row, end_col]. Input Example Consider this binary matrix where 1 represents filled cells and 0 represents empty cells ? 1011101 1101111 1011001 1011001 1011011 1010000 1110001 1011101 Algorithm ... Read More
A palindrome is a string that reads the same forwards and backwards. Finding all palindromic sub-strings involves checking every possible substring. This implementation uses the expand around centers approach with fractional positions to handle both odd and even-length palindromes efficiently. How the Algorithm Works The algorithm uses fractional positions (0.0, 0.5, 1.0, 1.5...) to represent centers: Integer positions (0, 1, 2...) represent centers of odd-length palindromes Half positions (0.5, 1.5, 2.5...) represent centers of even-length palindromes For each center, expand outward while characters match Implementation Here's the complete solution that finds all palindromic ... Read More
Given an array of numbers, we need to find all indices where removing that element results in a good array. A good array is one where at least one element equals the sum of all other elements in the array. The problem uses 1-based indexing, meaning the first element is at index 1, not 0. Understanding the Problem For the array [10, 4, 6, 2]: Removing index 1 (element 10): [4, 6, 2] → 6 = 4 + 2 ✓ Removing index 2 (element 4): [10, 6, 2] → 10 ≠ 6 + 2 ✗ ... Read More
Finding all distinct palindromic substrings is a common string processing problem. A palindrome is a string that reads the same forwards and backwards. This solution uses Manacher's algorithm to efficiently find all palindromic substrings. So, if the input is like "bddaaa", then the output will be [a, aa, aaa, b, d, dd] Understanding the Algorithm The algorithm follows these key steps: Create a matrix to store palindrome lengths at each position Process the string with padding characters to handle edge cases Use Manacher's algorithm to find all palindromes efficiently Extract distinct palindromic substrings using a ... Read More
Sometimes we need to create a string where each character is lexicographically greater than its immediate next character. This creates a strictly decreasing sequence of characters from left to right. Given a number n, we need to generate a lowercase string of length n+1 where each character is lexicographically larger than the character that follows it. Example If the input is n = 15, the output will be "ponmlkjihgfedcba" (16 characters total). Algorithm To solve this problem, we follow these steps ? Calculate extra = n % 26 to find remaining characters after ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance