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 162 of 377
Best Time to Buy and Sell Stock in Python
The Best Time to Buy and Sell Stock problem asks us to find the maximum profit from buying and selling a stock once. Given an array where each element represents the stock price on a specific day, we need to determine the optimal buy and sell days to maximize profit. For example, if prices = [7, 1, 5, 3, 6, 4], the maximum profit is 5 (buy at price 1 on day 2, sell at price 6 on day 5). Algorithm Approach We can solve this using two auxiliary arrays: leftMin: ...
Read MorePath Sum in Python
The Path Sum problem asks us to determine if there exists a root-to-leaf path in a binary tree where the sum of node values equals a given target sum. This is a classic tree traversal problem that can be solved using recursion. 0 -3 9 -10 5 ...
Read MoreConvert Sorted Array to Binary Search Tree in Python
Converting a sorted array to a height-balanced binary search tree (BST) is a common algorithmic problem. A height-balanced BST is a binary tree where the depth of the two subtrees of every node never differs by more than 1. Algorithm The key insight is to use the middle element as the root to ensure balance. The algorithm works as follows − If the array is empty, return None Find the middle element and make it the root Recursively build the left subtree from elements before the middle Recursively build the right subtree from elements after the ...
Read MoreMaximum Depth of Binary Tree in Python
A binary tree's maximum depth is the number of nodes along the longest path from the root to any leaf node. This is a fundamental tree traversal problem that can be solved efficiently using recursion. 1 2 2 3 4 3 ...
Read MoreMerge Sorted Array in Python
When working with two sorted arrays, we often need to merge them into a single sorted array. Python provides several approaches to accomplish this task efficiently. For example, if we have A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], the merged result should be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]. Using Two Pointers Approach The most efficient approach uses two pointers to compare elements from both arrays ? def merge_sorted_arrays(arr1, arr2): merged = [] i = ...
Read MoreSqrt(x) in Python
Finding the square root of a number without using library functions is a common programming challenge. We need to implement our own function that returns the integer part of the square root, truncating any decimal digits. For example, if x = 4, the result is 2. If x = 8, the result is also 2 (since √8 = 2.828... but we take only the integer part). Algorithm Overview We use binary search to find the square root efficiently ? Initialize low = 1, high = x + 1, and answer = 0 While high > ...
Read MorePlus One in Python
The Plus One problem involves incrementing a large number represented as an array of digits. Given an array like [5, 3, 2, 4] representing 5324, we need to add 1 and return [5, 3, 2, 5] representing 5325. Method 1: String Conversion Approach Convert the array to a string, then to integer, add 1, and convert back to array ? def plus_one_string(digits): # Convert digits to string num_str = "" for digit in digits: num_str += ...
Read MoreMaximum Subarray in Python
The Maximum Subarray Problem involves finding a contiguous subarray within an array that has the largest sum. This is a classic problem that can be efficiently solved using Kadane's Algorithm, which uses dynamic programming principles. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the maximum subarray is [4, -1, 2, 1] with sum 6. Algorithm Overview The dynamic programming approach works as follows ? Create a DP array of the same size as input array Initialize dp[0] = nums[0] ...
Read MoreCount and Say in Python
The Count and Say sequence is a fascinating pattern where each term describes the previous term by counting consecutive identical digits. Let's understand how this sequence works and implement it in Python. Understanding the Sequence The Count and Say sequence starts with "1" and each subsequent term describes the previous term ? Term 1: "1" Term 2: "11" (one 1) Term 3: "21" (two 1s) Term 4: "1211" (one 2, one 1) Term 5: "111221" (one ...
Read MoreImplement strStr() in Python
The strStr() function finds the first occurrence of a substring within a string and returns its index. This is similar to the strstr() function in C programming. If the substring is not found, it returns -1. Algorithm To implement strStr(), we use a two-pointer approach with the following steps ? Initialize i = 0 (for main string), j = 0 (for substring) If substring is empty, return 0 While i < length of string and remaining characters >= substring length: If characters match, compare the entire substring If complete match found, return starting index Otherwise, ...
Read More