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 111 of 377
Program to convert level order binary tree traversal to linked list in Python
Converting a binary tree to a singly linked list using level-order traversal means visiting nodes level by level from left to right. We use a queue to perform breadth-first traversal and create linked list nodes in the same order. Problem Understanding Given a binary tree like this: 5 4 10 2 7 ...
Read MoreProgram to traverse binary tree level wise in alternating way in Python
Suppose we have a binary tree, we need to traverse it level by level in an alternating manner − first level left−to−right, second level right−to−left, third level left−to−right, and so on. So, if the input is like: 5 4 -10 ...
Read MoreProgram to find length of longest balanced subsequence in Python
When working with bracket sequences, finding the longest balanced subsequence is a common problem. A balanced bracket subsequence contains equal numbers of opening "(" and closing ")" brackets, where each closing bracket has a matching opening bracket before it. So, if the input is like s = "())(()(" , then the output will be 4, as we can take the subsequence like "()()". Algorithm Approach The key insight is to traverse the string from right to left and use a greedy approach ? Initialize result counter and closing bracket counter Traverse string from right to ...
Read MoreProgram to find leftmost deepest node of a tree in Python
In a binary tree, we often need to find the leftmost deepest node. This means finding the node at the maximum depth, and if multiple nodes exist at that depth, we return the leftmost one. So, if the input is like 13 12 14 16 22 ...
Read MoreProgram to check whether we can fill square where each row and column will hold distinct elements in Python
Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once. This is essentially a Latin square completion problem where we need to verify if a partially filled grid can be completed following sudoku-like rules. Example Input and Output So, if the input is like ? 002 201 123 Then the output will be True, as we ...
Read MoreProgram to find the largest sum of the path between two nodes in a binary tree in Python
In a binary tree, we often need to find the maximum sum path between any two nodes. This path doesn't have to go through the root and can be between any two nodes in the tree. Given a binary tree like this: .node-circle { fill: #e6f3ff; stroke: #0066cc; stroke-width: 2; } .node-text { font-family: Arial, sans-serif; font-size: 12px; text-anchor: middle; dominant-baseline: central; } .edge-line { stroke: #666; stroke-width: 2; } ...
Read MoreProgram to find largest sum of non-adjacent elements of a list in Python
The problem of finding the largest sum of non-adjacent elements in a list is a classic dynamic programming problem. Given a list of numbers, we need to select elements such that no two selected elements are adjacent, and their sum is maximized. So, if the input is like [3, 5, 7, 3, 6], then the output will be 16, as we can take 3, 7, and 6 to get the maximum sum without selecting adjacent elements. Algorithm Approach We use dynamic programming with two variables to track the maximum sum: take: Maximum sum including the ...
Read MoreProgram to find sum of contiguous sublist with maximum sum in Python
The Maximum Subarray Problem asks us to find the contiguous sublist within an array that has the largest sum. This is a classic problem that can be efficiently solved using Kadane's Algorithm with dynamic programming. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the maximum sum subarray is [4, -1, 2, 1] with sum 6. Algorithm Approach We use dynamic programming to solve this problem efficiently ? Create an array dp of the same size as input array Set dp[0] = ...
Read MoreProgram to find k-sized list where difference between largest and smallest item is minimum in Python
Given a list of numbers and an integer k, we need to select k elements to create a sublist where the difference between the largest and smallest elements is minimized. This problem requires finding the optimal contiguous subsequence after sorting. For example, if nums = [3, 11, 6, 2, 9] and k = 3, the output will be 4 because the best sublist we can make is [2, 3, 6] with difference 6 - 2 = 4. Algorithm To solve this problem, we follow these steps: Sort the input list to group similar numbers together ...
Read MoreProgram to find the largest grouping of anagrams from a word list in Python
Suppose we have a list of strings words, we have to group all anagrams together and return the size of the largest grouping. Anagrams are words formed by rearranging the letters of another word, like "xyz" and "zyx". So, if the input is like words = ["xy", "yx", "xyz", "zyx", "yzx", "wwwww"], then the output will be 3, as ["xyz", "zyx", "yzx"] is the largest grouping. Algorithm To solve this, we will follow these steps − Create a dictionary to store anagram groups Initialize result as 0 For each ...
Read More