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 508 of 2109
Program 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 MoreProgram to schedule tasks to take smallest amount of time in Python
Suppose we have a list of values called tasks where each different value represents a different task type, and we also have a non-negative integer k. Each task takes one minute to complete, but we must wait k minutes between doing two tasks of the same type. At any time, we can be doing a task or waiting. We have to find the smallest amount of time it takes to complete all the tasks. So, if the input is like tasks = [2, 2, 2, 3, 3, 2], k = 1, then the output will be 7, as the ...
Read MoreProgram to check whether given tree is symmetric tree or not in Python
A symmetric binary tree is one that looks identical to its mirror image. To check if a tree is symmetric, we need to verify that the left subtree is a mirror reflection of the right subtree. Symmetric Tree 1 2 2 3 4 4 ...
Read MoreProgram to check whether two trees can be formed by swapping nodes or not in Python
Binary trees can sometimes be transformed into each other by swapping left and right subtrees at any node. This problem checks whether two given trees can be made identical through such swapping operations. Algorithm Overview The solution uses level-order traversal (BFS) to compare both trees level by level. At each level, we check if the node values can match either in the same order or in reverse order (indicating a possible swap). Step-by-Step Approach Use two queues to store nodes from both trees level by level For ...
Read MoreProgram to check whether each node value except leaves is sum of its children value or not in Python
A binary tree has a special property when each internal node's value equals the sum of its children's values. We need to check if this property holds for all non-leaf nodes in the tree. In this problem, we traverse the tree recursively and verify that each internal node satisfies the sum condition. Algorithm Steps The solution uses a depth-first search (DFS) approach ? If the current node is null, return True (empty subtree is valid) If the current node is a leaf (no children), return True ...
Read MoreProgram to find three unique elements from list whose sum is closest to k Python
Given a list of numbers and a target value k, we need to find three unique elements whose sum is closest to k and return the absolute difference between their sum and k. For example, if nums = [2, 5, 25, 6] and k = 14, the triplet [2, 5, 6] gives us the sum 13, which is closest to 14 with an absolute difference of 1. Approach We'll use a two-pointer technique after sorting the array: Sort the input list to enable two-pointer traversal For each element as the first element of the triplet, ...
Read More