Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 553 of 2547
Find a string such that every character is lexicographically greater than its immediate next character in Python
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 MoreFind a sorted subsequence of size 3 in linear time in Pytho
Given an array of N numbers, we need to find three elements such that A[i] < A[j] < A[k] where i < j < k (an increasing subsequence of size 3) in linear O(n) time. If multiple triplets exist, we return any one of them. For example, if the input is [13, 12, 11, 6, 7, 3, 31], the output will be (6, 7, 31). Algorithm Approach We use two auxiliary arrays to track potential elements ? smaller[] − For each position i, stores the index of the smallest element to the left that is ...
Read MoreFind a positive number M such that gcd(N^M,N&M) is maximum in Python
Given a number N, we need to find a positive number M such that gcd(N^M, N&M) is maximized, where ^ is XOR and & is AND operation. The problem asks us to find both M and the maximum GCD value. The key insight is that we need to analyze the binary representation of N to find the optimal M that maximizes the GCD between N XOR M and N AND M. Algorithm The solution follows these steps − If N has no set bits at even positions (all bits at positions 0, 2, 4... are ...
Read MoreFinal state of the string after modification in Python
This problem simulates boxes being pushed in different directions until they reach a stable final state. Given a string where 'R' represents a box pushed right, 'L' represents a box pushed left, and '.' represents empty space, we need to find the final configuration after all movements stop. Problem Understanding Each box marked 'R' pushes subsequent boxes to the right, while boxes marked 'L' push preceding boxes to the left. The simulation continues until no more movement is possible ? Algorithm Approach We use a two-pass algorithm to calculate the net force on each position ? ...
Read MoreLFU Cache in Python
A Least Frequently Used (LFU) cache is a data structure that removes the least frequently accessed item when the cache reaches its maximum capacity. Python's collections.OrderedDict helps track both frequency and insertion order for efficient implementation. LFU Cache Operations The LFU cache supports two main operations: get(key) – Returns the value if the key exists, otherwise returns -1 put(key, value) – Inserts or updates a key-value pair When capacity is reached, the cache removes the least frequently used element before inserting a new one. Implementation Strategy The implementation uses two data structures: ...
Read MoreLongest Increasing Path in a Matrix in Python
Given a matrix, we need to find the length of the longest increasing path. From each cell, we can move in four directions − left, right, up, or down. We cannot move diagonally or outside the matrix boundaries. This problem can be solved efficiently using dynamic programming with memoization and depth-first search (DFS). Problem Example Consider this matrix ? 9 9 4 6 6 8 2 1 1 The longest increasing path is 1 → 6 → 8 → 9 with length 4. Algorithm Approach ...
Read MoreOptimize Water Distribution in a Village in Python
The water distribution optimization problem is a classic application of graph theory and the Minimum Spanning Tree (MST) algorithm. Given n houses, we can either build wells directly in houses or connect them via pipes. The goal is to minimize the total cost to supply water to all houses. The problem can be modeled as finding the MST of a graph where: Each house is a node Building a well is represented as connecting to a virtual node (node 0) Pipes between houses are edges with their respective costs Algorithm Approach We use Kruskal's algorithm ...
Read MoreString Transforms Into Another String in Python
Sometimes we need to check if we can transform one string into another by converting all occurrences of specific characters. This problem involves character mapping and transformation rules to determine if str1 can be converted to str2. In one conversion, we can convert all occurrences of one character in str1 to any other lowercase English character. The key constraint is that we need at least one "free" character (not used in str2) to perform intermediate conversions. Problem Example Given str1 = "aabcc" and str2 = "ccdee", we can transform str1 to str2 ? Convert 'c' ...
Read MoreParallel Courses in Python
The parallel courses problem involves finding the minimum number of semesters needed to complete N courses with prerequisite dependencies. This is essentially a topological sorting problem that can be solved using BFS (Breadth-First Search). Problem Understanding Given N courses labeled 1 to N and a relations array where relations[i] = [X, Y] means course X must be completed before course Y. We need to find the minimum semesters required to complete all courses, or return -1 if impossible due to circular dependencies. Algorithm Approach We use topological sorting with BFS to process courses level by level ...
Read MoreDivide Array Into Increasing Sequences in Python
Suppose we have a non-decreasing array of positive integers called nums and an integer K. We need to determine if this array can be divided into one or more disjoint increasing subsequences, each of length at least K. For example, if the input is nums = [1, 2, 2, 3, 3, 4, 4] and K = 3, the output will be True because this array can be divided into two subsequences: [1, 2, 3, 4] and [2, 3, 4], both with lengths at least 3. Algorithm Approach The key insight is that we need to find the ...
Read More