Python Articles

Page 677 of 855

Combinations in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Generating all possible combinations of k numbers from a range 1 to n is a classic problem that can be solved efficiently using backtracking. For example, if n = 4 and k = 2, the combinations would be [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]. In Python, we can implement this using recursion with backtracking to explore all possible combinations systematically. Using Recursive Backtracking The backtracking approach builds combinations incrementally and backtracks when a complete combination is found ? def combine(n, k): result = [] ...

Read More

Sort Colors in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

The Sort Colors problem involves sorting an array containing only three values: 0 (red), 1 (white), and 2 (blue). We need to sort them in-place so objects of the same color are adjacent, in the order red, white, blue. For example, if the input array is [2,0,2,1,1,0], the sorted output should be [0,0,1,1,2,2]. Algorithm: Dutch National Flag This problem is efficiently solved using the Dutch National Flag algorithm with three pointers ? Set low = 0, mid = 0, and high = length of array − 1 While mid

Read More

Pow(x, n) in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Calculating x to the power n efficiently without using built-in functions like pow() or ** is a common programming problem. We can implement this using binary exponentiation, which reduces time complexity from O(n) to O(log n). Problem Statement Given two inputs x and n, where x is a number in range -100.0 to 100.0 and n is a 32-bit signed integer, we need to calculate xn efficiently. Example If x = 12.1 and n = -2, then xn = 12.1-2 = 1/(12.1)2 = 0.00683 Algorithm We use binary exponentiation which works by breaking down ...

Read More

Multiply Strings in C++

Aishwarya Naglot
Aishwarya Naglot
Updated on 25-Mar-2026 9K+ Views

We are given two strings consisting of integers that can have lengths up to 200. Both strings do not contain any leading zeros, but 0 as a number itself can be present. We need to multiply these integer strings and return the result as a string. For example, if we have two numbers as strings "26" and "12", the result will be "312". Following are various ways to multiply strings in C++: Using Built−in stoll() method Digit−by−Digit String Multiplication Algorithm Using Karatsuba Multiplication Algorithm ...

Read More

Valid Sudoku in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 5K+ Views

A valid Sudoku puzzle must follow three key rules for all filled cells: no duplicate digits in any row, column, or 3x3 sub-box. We need to validate only the filled cells, not solve the entire puzzle. Validation Rules A 9x9 Sudoku board is valid when ? Each row contains digits 1-9 without repetition Each column contains digits 1-9 without repetition Each of the 9 (3x3) sub-boxes contains digits 1-9 without repetition Algorithm Approach We'll use three dictionaries to track seen digits in rows, columns, and 3x3 blocks. For each cell, we calculate which ...

Read More

Find First and Last Position of Element in Sorted Array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Finding the first and last position of an element in a sorted array is a classic problem that can be efficiently solved using binary search. Given a sorted array and a target value, we need to return the starting and ending indices of the target. If the target is not found, we return [-1, -1]. For example, if the array is [2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6] and target is 4, the output should be [4, 7] since 4 appears from index 4 to index 7. Algorithm We use two binary searches ...

Read More

Search in Rotated Sorted Array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Consider we have an array sorted in ascending order that is rotated at some unknown pivot. For example, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. Given a target value to search, we return its index if found, otherwise return -1. We assume no duplicates exist in the array. Algorithm We use a modified binary search approach ? Initialize low = 0 and high = length of array While low < high: ...

Read More

Generate Parentheses in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

Generating well-formed parentheses is a classic programming problem that involves creating all valid combinations of opening and closing parentheses for a given number n. For n=3, we need 3 opening and 3 closing parentheses arranged in valid patterns. Problem Understanding A valid parentheses combination follows these rules ? Equal number of opening '(' and closing ')' parentheses At any point, the number of closing parentheses should not exceed opening ones All opening parentheses must be properly closed Solution Approach We use a recursive backtracking approach with these steps ? Track the ...

Read More

Remove Nth Node From End of List in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 778 Views

Removing the Nth node from the end of a linked list is a common programming problem. Given a linked list and a number n, we need to remove the node that is n positions from the end and return the modified list head. For example, if we have a list [1, 2, 3, 4, 5, 6] and n = 3, we remove the 3rd node from the end (which is 4), resulting in [1, 2, 3, 5, 6]. Algorithm Steps We use a two-pointer approach to solve this efficiently ? If there is only one ...

Read More

Letter Combinations of a Phone Number in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

The Letter Combinations of a Phone Number problem involves generating all possible letter combinations that a string of digits (2-9) could represent on a phone keypad. Each digit maps to a set of letters, similar to old telephone keypads. Phone Keypad Mapping 1 2abc 3def 4ghi 5jkl 6mno 7pqrs 8tuv 9wxyz * 0 # For example, if the input is "23", the possible combinations are ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Algorithm Approach We'll use backtracking to solve this problem: ...

Read More
Showing 6761–6770 of 8,549 articles
« Prev 1 675 676 677 678 679 855 Next »
Advertisements