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
Articles by Arnab Chakraborty
Page 157 of 377
Largest Number in Python
Finding the largest number from a list of non-negative integers requires arranging them in a specific order. For example, given [10, 2], the largest number would be 210, not 102. The key insight is to sort numbers based on which arrangement produces a larger concatenated result. We compare two numbers by checking if x + y is greater than y + x as strings. Algorithm Steps Convert all numbers to strings Sort using a custom comparator that checks concatenated results Join the sorted strings and handle edge cases Implementation Let us see the ...
Read MoreBinary Tree Inorder Traversal in Python
Binary tree inorder traversal visits nodes in the order: left subtree, root, right subtree. This article demonstrates how to perform inorder traversal iteratively using a stack instead of recursion. 10 5 15 2 7 20 ...
Read MoreDecode Ways in Python
Suppose we have a message containing letters from A to Z that is being encoded to numbers using the following mapping − 'A' → 1, 'B' → 2 ... 'Z' → 26. Given a non-empty string containing only digits, we need to find in how many ways that string can be decoded. For example, if the string is "12", it can be decoded as "AB" (1, 2) or "L" (12), so there are two possible ways. Algorithm Approach We will solve this using dynamic programming with the following steps ? Create a DP array where ...
Read MoreCombinations in C++
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 MoreSort Colors in Python
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 MorePow(x, n) in Python
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 MoreValid Sudoku in Python
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 MoreFind First and Last Position of Element in Sorted Array in Python
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 MoreSearch in Rotated Sorted Array in Python
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 MoreGenerate Parentheses in Python
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