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
Server Side Programming Articles
Page 580 of 2109
Reverse Linked List in Python
A linked list is a linear data structure where elements are stored in nodes, and each node contains data and a pointer to the next node. Reversing a linked list means changing the direction of pointers so that the last node becomes the first node. For example, if we have: 1 → 3 → 5 → 7, the reversed list will be: 7 → 5 → 3 → 1 Algorithm We can reverse a linked list using a recursive approach ? Define a recursive function solve(head, back) where back tracks the previous node If head ...
Read MoreHouse Robber in Python
The House Robber problem is a classic dynamic programming challenge. A robber wants to rob houses along a street, but cannot rob two adjacent houses as this would trigger an alarm. We need to find the maximum amount that can be robbed. Given an array where each element represents the amount of money in each house, we must determine the maximum sum possible without selecting adjacent elements. Problem Understanding For the array [2, 7, 10, 3, 1], the optimal strategy is to rob houses at indices 0, 2, and 4 (values 2, 10, 1) for a total ...
Read MoreBest 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 More