Programming Articles - Page 1035 of 3366

Program to check whether we can eat favorite candy on our favorite day in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:06:51

250 Views

Suppose we have an array of positive calues candiesCount where candiesCount[i] denotes the number of candies of the ith type we have. We also have another another array called queries where queries[i] has three parameters [favoriteType_i, favoriteDay_i, dailyCap_i]. We have some rules:We start eating candies on day 0.We cannot eat any candy of type i unless we have eaten all candies of previous i-1 types.We must eat at least one candy per day until we have eaten all of them.Maintaining these rules, we have to make an array of Boolean values for each query results and i-th entry is true ... Read More

Program to find out the critical and pseudo-critical edges in a graph in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:23:47

760 Views

Suppose, we are given a graph that contains n vertices numbered 0 to n -1. The graph is undirected and each edge has a weight. So given the graph, we have to find out the critical and the pseudo-critical edges in the graphs MST. An edge is called a critical edge if deletion of that edge causes the MST weight to increase. A pseudo-critical edge is an edge that can appear in all the graphs MSTs, but not all. We find out the index of the edges given the graph as input.So, if the input is likeand number of vertices ... Read More

Program to find Kth ancestor of a tree node in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:06:53

389 Views

Suppose we have a tree with n nodes that are numbered from 0 to n-1. The tree is given by a parent array, where parent[i] is the parent of node i. The root of the tree is node 0. We have to find the kth ancestor of a given node, if the ancestor is not present, then return -1So, if the input is likethen the output will be 2 because the first ancestor of node 6 is 5 and the second is 2.To solve this, we will follow these steps −Define a function solve() . This will take parent, node, ... Read More

Program to find minimum total distance between house and nearest mailbox in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:50:51

1K+ Views

Suppose we have an array called houses and have another value k. Here houses[i] represents the location of the ith house along a street, we have to allocate k mailboxes in the street, and find the minimum total distance between each house and its nearest mailbox.So, if the input is like houses = [6, 7, 9, 16, 22] k = 2, then the output will be 9 because if we place mailbox at 7 and 18, then minimum total distance from each house is |6-7|+|7-7|+|9-7|+|16- 18|+|22-18| = 1+0+2+2+4 = 9.To solve this, we will follow these steps −sort the list ... Read More

Program to find minimum cost for painting houses in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:46:26

516 Views

Suppose there is an array of size m, represents m houses in a small city, each house must be painted with one of the n colors (the colors are labeled from 1 to n), And some houses are already painted, so no need to paint again. Those houses which are colored with same color are called neighborhood. We have the array houses, where houses[i] represents the color of the house, if the color value is 0, then it represents the house is not colored yet. We have another array called costs, this is a 2D array where costs[i, j] represents ... Read More

Program to find where the ball lands in a grid box in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:43:34

211 Views

Suppose, we are given a m x n grid box, where each cell has a board that is positioned either from the top-right to bottom-left, or from the top-left to the bottom-right. Now from the top cells, a ball is put into the box and we have to check if that ball reaches the bottom of the box. The grid is given as a matrix. If a cell is marked 1 the diagonal board spans from the top-left to the bottom-right; if it is marked -1 it spans from the top-right to the bottom-left corner. If n balls are put ... Read More

Program to implement a queue that can push or pop from the front, middle, and back in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:39:42

556 Views

Suppose, we are asked to implement a queue that can push and pop values at the front, middle, and back.We have to implement a pair of functions to push and pop for all three cases. We have to implement another function that shows the full queue at a given time.So, if the input is likepush_from_back(10)push_from_back(20)push_from_front(30)push_from_middle(40)push_from_front(50)show_queue()pop_from_back()show_queue()pop_from_front()show_queue()pop_from_middle()show_queue(), then the output will be [50, 30, 40, 10, 20[50, 30, 40, 10][30, 40, 10][30, 10]To solve this, we will follow these steps −array := array representation of the queueDefine a function push_from_front() . This will take valueinsert value into array at position 0Define a ... Read More

Program to determine if two strings are close in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:40:53

486 Views

Suppose we have two strings, s and t, we have to check whether s and t are close or not. We can say two strings are close if we can attain one from the other using the following operations −Exchange any two existing characters. (like abcde to aecdb)Change every occurrence of one existing character into another existing character, and do the same with the other characters also. (like aacabb -> bbcbaa (here all a's are converted to b's, and vice versa))We can use the operations on either string as many times as we want.So, if the input is like s ... Read More

Program to find out the vertical area between two points where no point lies and is the widest in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:37:22

180 Views

Suppose, we are given n number of points as (x, y). A vertical area is an area that is extended infinitely along the y-axis. We have to find out the vertical area between two points such that no other point is inside the area and is the widest.So, if the input is like pts = [[10, 9], [11, 11], [9, 6], [11, 9]], then the output will be 1.The areas in red and blue are optimal and there are no points inside them.To solve this, we will follow these steps −sort the list ptsfor i in range 1 to size ... Read More

Program to find out the inheritance order in a family in Python

Arnab Chakraborty
Updated on 06-Oct-2021 07:34:03

2K+ Views

Suppose, there is a family that consists of members from different generations. Such as the family has a father, his children, and their grandmother. But births and deaths happen in each family.The eldest member of the family is considered the head. So, when the 'head' member dies, their direct successor or their children becomes the head. We implement three functions, the first one is used when a child is born into the family. The function takes the parent's name and child's name as input and adds them to the record.The second function is used when there is a death. It ... Read More

Advertisements