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 on Trending Technologies
Technical articles with clear explanations and examples
Program to check all values in the tree are same or not in Python
Suppose we have a binary tree, we have to check whether all nodes in the tree have the same values or not. This is a common tree traversal problem that can be solved using recursive depth-first search. So, if the input is like ? 5 5 5 5 5 ...
Read MoreProgram to check occurrences of every value is unique or not in Python
Suppose we have a list of numbers nums (positive or negative), we have to check whether the number of occurrences of every value in the array is unique or not. So, if the input is like nums = [6, 4, 2, 9, 4, 2, 2, 9, 9, 9], then the output will be True, as there is 1 occurrence of 6, 2 occurrences of 4, 3 occurrences of 2, and 4 occurrences of 9. So all number of occurrences are unique. Approach To solve this, we will follow these steps − ...
Read MoreProgram to check whether we can pick up and drop every passenger in given list in Python
Suppose we have a matrix called requested_trips where each row contains [start_x, end_x, num_passengers], and we also have a capacity value. Each requested trip asks to pick up num_passengers passenger at start_x and drop them off at end_x. We have a car with the given capacity that starts at position x = 0. The car can only move to the right side, and we need to check whether we can pick up and drop off everyone without exceeding the capacity. So, if the input is like trips = [[1, 25, 2], [3, 4, 3], [5, 12, 3]] and capacity ...
Read MoreProgram to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
Given a list of numbers and a target value k, we need to find two nonoverlapping sublists whose sum equals k and return the sum of their lengths. When multiple solutions exist, we choose the two shortest sublists. Problem Understanding For example, with nums = [7, 10, -2, -1, 4, 3] and k = 7, we can find sublists [7] (length 1) and [4, 3] (length 2), giving us a total length of 3. We don't choose [10, -2, -1] because it's longer than [7]. Algorithm Approach The solution uses a two-pass approach with prefix and ...
Read MoreProgram to check two trees are exactly same based on their structure and values in Python
Suppose we have two binary trees, we have to check whether they are exactly same in terms of their structures and values or not. We can say them as twin trees. So, if the input is like ? Tree 1 10 5 15 Tree 2 10 ...
Read MoreProgram to find possible number of palindromes we can make by trimming string in Python
Suppose we have a string s, we have to find the number of ways we can obtain a palindrome by trimming the left and right sides of s. This involves finding all possible palindromic substrings within the given string. So, if the input is like s = "momo", then the output will be 6. The palindromic substrings are: ["m", "o", "m", "o", "mom", "omo"]. Algorithm Approach To solve this, we will follow these steps − Define a function expand() that takes parameters i, j, and s Initialize counter c := 0 While i >= 0 ...
Read MoreProgram to traverse binary tree using list of directions in Python
Suppose we have a binary tree and a list of strings moves consisting of "R" (Right), "L" (Left) and "U" (Up). Starting from root, we have to traverse the tree by performing each move in moves where: "R" indicates traverse to the right child, "L" indicates traverse to the left child, and "U" indicates traverse to its parent. 2 4 3 5 ...
Read MoreProgram to find sum of all elements of a tree in Python
A binary tree is a hierarchical data structure where each node has at most two children. To find the sum of all elements in a binary tree, we can use recursive traversal to visit each node and accumulate their values. Given a binary tree like this: 2 4 3 5 ...
Read MoreProgram to find minimum distance that needs to be covered to meet all person in Python
This problem asks us to find the optimal meeting point in a 2D grid where people can gather with minimum total walking distance. We have a grid with walls (1), empty spaces (0), and people (2), and need to find the location that minimizes the sum of distances all people must travel. Problem Setup Given a 2D matrix with: 0 represents an empty cell 1 represents a wall 2 represents a person A person can move in four directions (up, down, left, right). We need to find a non-wall cell that minimizes the total ...
Read MoreProgram to find minimum number of cells it will take to reach bottom right corner in Python
Suppose we have a 2D grid representing a maze where 0 represents an empty space and 1 represents a wall. We start at grid[0][0] and need to find the minimum number of cells it takes to reach the bottom-right corner. If we cannot reach the destination, we return -1. Problem Example Consider this grid ? 0 0 0 1 0 0 1 0 0 The output will be 5 because the shortest path requires 5 steps. Algorithm Steps We use Breadth-First Search (BFS) to find the ...
Read More