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
Programming Articles
Page 492 of 2547
Program to check whether we can form 24 by placing operators in python
Suppose we have a list of four numbers, each number is in range 1 to 9, in a fixed order. We need to place operators (+, -, *, /) between the numbers and group them with brackets to check whether it is possible to get the value 24 or not. So, if the input is like nums = [5, 3, 6, 8, 7], then the output will be True, as (5 * 3) - 6 + (8 + 7) = 24. Approach We will use a recursive approach to generate all possible results by splitting the array ...
Read MoreProgram to find number of days it will take to burn all trees in python
Suppose we have a 2D matrix that represents a forest where there are three types of cells: 0 (empty cell), 1 (tree cell), and 2 (tree on fire cell). Every day, a tree catches fire when there is an adjacent (top, down, left, right, not diagonal) tree on fire. We have to find the number of days it would take for every tree to be on fire. If that is not possible, return -1. Problem Example If the input matrix is: 1 2 1 1 0 1 1 1 1 ...
Read MoreProgram to count number of paths whose sum is k in python
In this problem, we need to find the number of paths in a binary tree that sum to a target value k. The paths can start and end at any nodes, not necessarily from root to leaf. So, if the input is like: 3 2 4 1 2 ...
Read MoreProgram to check whether we can color a tree where no adjacent nodes have the same color or not in python
Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color. So, if the input is like: .node { fill: lightblue; stroke: black; stroke-width: 2; } .text { font-family: Arial; font-size: 14px; text-anchor: middle; } .line { stroke: black; stroke-width: ...
Read MoreProgram to find the final ranking of teams in order from highest to lowest rank in python
When we have a list of voting strings where each string represents votes on candidates in order from highest to lowest preference, we need to rank candidates based on their voting positions. The ranking follows a specific priority: first by highest preference votes, then by second preference votes if tied, and finally alphabetically if still tied. So, if the input is like votes = ["zyx", "zxy", "xyz"], then the output will be "zxy". Here's why: 'z' received 2 votes for highest preference (position 0), 'x' received 1 vote for highest preference, and 'y' received 0 votes for highest preference. ...
Read MoreProgram to count number of surrounded islands in the matrix in python
In this problem, we have a binary matrix where 1 represents land and 0 represents water. An island is a group of connected 1s, and we need to find islands that are completely surrounded by water (not touching the matrix boundaries). So, if the input matrix is like: ...
Read MoreProgram to find sum of all numbers formed by path of a binary tree in python
Suppose we have a binary tree where each node contains a single digit from 0 to 9. Each path from the root to a leaf represents a number with its digits in order. We need to find the sum of all numbers represented by these root-to-leaf paths. So, if the input is like ? 4 6 3 2 ...
Read MoreProgram to find number of subsequence that are present inside word list in python
Given a list of words and a string s, we need to find how many words are subsequences of s. A subsequence maintains the relative order of characters but doesn't need to be contiguous. So, if the input is like words = ["xz", "xw", "y"] and s = "xyz", then the output will be 2, as "xz" and "y" are subsequences of "xyz". Algorithm To solve this, we will follow these steps − Initialize ans := 0 and create an empty dictionary d Group words by their first character in dictionary d For each character ...
Read MoreProgram to find minimum number of subsequence whose concatenation is same as target in python
Suppose we have two strings source and target, we have to find the minimum number of subsequences of source we can form such that if we concatenate them, it will be same as target. If there is no such result, return -1. So, if the input is like source = "xyz" target = "xyzyzz", then the output will be 3, as we can concatenate these ["xyz" + "yz" + "z"] Algorithm To solve this, we will follow these steps − s_size := size of s, t_size := size of t concat_count := 0, target_idx := ...
Read MoreProgram to list of candidates who have got majority vote in python
Suppose we have a list of numbers called nums where each number represents a vote to a candidate. We have to find the ids of the candidates that have greater than floor(n/3) votes, in non-decreasing order. So, if the input is like nums = [3, 2, 6, 6, 6, 6, 7, 7, 7, 7, 7], then the output will be [6, 7], as 6 and 7 have more than 33% of the votes (4 out of 11 and 5 out of 11 respectively). Algorithm To solve this, we will follow these steps − ans := ...
Read More