Programming Articles

Page 511 of 2547

Program to check whether given list is in valid state or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 468 Views

Sometimes we need to check if a list can be completely partitioned into valid groups. This problem involves grouping numbers using specific rules to determine if the entire list is in a "valid state". Problem Definition Given a list of numbers, check if every number can be grouped using one of these rules: Contiguous pairs: (a, a) − two identical numbers Identical triplets: (a, a, a) − three identical numbers Consecutive triplets: (a, a+1, a+2) − three consecutive numbers Example For nums = [7, 7, 3, 4, 5], we can group [7, 7] ...

Read More

Program to find all upside down numbers of length n in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 373 Views

An upside down number (also called a strobogrammatic number) is a number that appears the same when rotated 180 degrees. The digits that remain valid when rotated are: 0, 1, 6, 8, and 9, where 6 becomes 9 and 9 becomes 6 when rotated. So, if the input is like n = 2, then the output will be ['11', '69', '88', '96']. Understanding Valid Digits When rotated 180 degrees ? 0 → 0 1 → 1 6 → 9 ...

Read More

Program to check all values in the tree are same or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 355 Views

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 More

Program to check occurrences of every value is unique or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 358 Views

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 More

Program to check whether we can pick up and drop every passenger in given list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 417 Views

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 More

Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 209 Views

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 More

Program to check two trees are exactly same based on their structure and values in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 233 Views

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 More

Program to find possible number of palindromes we can make by trimming string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 256 Views

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 More

Program to traverse binary tree using list of directions in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 339 Views

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 More

Program to find sum of all elements of a tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

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 More
Showing 5101–5110 of 25,466 articles
« Prev 1 509 510 511 512 513 2547 Next »
Advertisements