Q:
Let N be a positive odd number. There are N coins, numbered 1, 2 , ... , N. For each i (1≤i≤N), when Coin i is tossed, it comes up heads with probability pi and tails with probability 1-pi
belongs to collection: interview C++ coding problems/challenges | dynamic programming
interview C++ coding problems/challenges | dynamic programming
- We have given items i1, i2 , ..., in (the item we want to put in our bag) with associated weights w1, w2, ... wn and profit values V1, V2, ... Vn. Now the problem is how we can maximize the total benefit given a capacity of the bag is W and each item is allowed to be used for 0 or 1 time?
- A professional robber is planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping him from robbing each of them is that adjacent houses have security system connected
- Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins doesn\'t matter
- Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
- Given a dictionary, you have to split a given string into meaningful words
- There are N friends, each one of them can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways to do so. Since inputs are big return answer MOD 10^9+7
- n stock market, a person buys a stock and sells it on some future date. Given the stock prices of N days in form of an array Amount and a positive integer K, find out the maximum profit a person can make in at most K transactions
- Given a weighted directed graph, the problem is to find the shortest distances between every pair of vertices. The Graph is represented by an adjacency matrix, and any cell arr[i][j] denotes the weight of the edge (path cost) from node i to node j (if it exists) else INF
- You are given an array A of size N. The array contains integers and is of even length. The elements of the array represent N coin of values x1, x2, ... ,xn
- Consider a highway of M miles. The task is to place billboards on the highway such that revenue is maximized
- Given a gold mine of n*m dimensions, each cell in this mine contains a positive integer which is the amount of gold in tons. Initially, the miner is at the first column but can be at any row
- Given a sequence A of size N, find the length of the longest increasing subsequence from the given sequence
- Given a non-negative integer n, count all numbers with unique digits, x, 0<=x<10^n
- Given a number N, write a program to print the minimum number of squares that sums to N
- Given a value P, you have to make change for P cents, given that you have infinite supply of each of C { C1, C2, ... ,Cn} valued coins
- Given an array, you have to find out the length of the longest bitonic subsequence from the array
- Given string str, find the length of the longest repeating subsequence
- Given two strings str1 and str2, find length of the longest common sub-sequence between them
- Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown
- Given a rod of length N inches and an array of prices that contains prices for all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces
- Given an array of integers where each element represents the max number of steps that can be made forward from that element
- Given a M X N matrix with initial position at top-left cell, find the number of possible unique paths to reach the bottom right cell of the matrix from the initial position
- Given two strings S and T, find the number of times the second string occurs in the first string, whether continuous or discontinuous as subsequence
- Given a number n, we can divide it into only three parts n/2, n/3 and n/4 (we will consider only integer part). The task is to find the maximum sum we can make by dividing the number into three parts recursively and summing up them together
- Given an array you have to print the longest bitonic subsequence from the array. A bitonic sequence is a sequence where numbers of that sequence are sorted and they are increasing first then decreasing
- Given a string you have to find out the longest palindromic subsequence from the given string
- Given a string you have to find out the length of the longest palindromic subsequence from the given string
- Given a string you have to count the total number of palindromic subsequences in the giving string and print the value
- Given an array of size n and a sum K, determine whether any subset is possible with that sum or not. Print \"yes\" if there is any subset present else print \"no\"
- Given a set of numbers, check whether it can be partitioned into two subsets or not such that the sum of elements in both subsets is same
- Given an integer N denoting the Length of a rod, one needs to cut the rod in such a way that the cut length of the rod segment each time is integer either x, y or z and after performing all cutting operation the total number of cut segments must be maximum
- There is a shop which sells pizza of three different sizes- Small, Medium, and Large Pizza. IncludeHelp is crazy for Pizzas
- Shivang is a blog writer and he is working on two websites simultaneously. He has to write two types of blogs
- Given a square matrix of size n x n, find the sum of the Zigzag sequence with the largest sum. A zigzag sequence starts from the top and ends at the bottom. Two consecutive elements of sequence cannot belong to the same column
- There is a grid with size NX4 and you have only one type of tiles to construct the grid. You can place either vertically or horizontally. You need to find, how many ways you can construct a grid of size N x 4 using the 1X4 tiles
- Given string str find the minimum number of deletions such that the resultant string is a palindrome
- Given two strings string1 and string2 find the minimum cost required to make the given two strings identical. We can delete characters from both the strings. The cost of deleting a character from string1 is costX and string2 is costY
- Given two strings, you have to find the shortest common super sequence between them and print the length of the super sequence
- Given two strings, you have to find and print the longest common subsequence between them
- Given n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in the row
- You are given N eggs, and building with K floors from 1 to K. Each egg is identical, you drop an egg and if an egg breaks, you cannot drop it again
- Let N be a positive odd number. There are N coins, numbered 1, 2 , ... , N. For each i (1≤i≤N), when Coin i is tossed, it comes up heads with probability pi and tails with probability 1-pi
- Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path
- Given a binary Tree find the maximum path sum. The path may start and end at any node in the tree
- Given an array of n integers. Find the minimum number of elements from the array to remove or delete so that when the remaining elements are placed in the same sequence order form a sorted sequence
- You are provided an input string S and the string \"includehelp\". You need to figure out all possible subsequences \"includehelp\" in the string S? Find out the number of ways in which the subsequence \"includehelp\" can be formed from the string S
- Given two positive integer n and m, find how many arrays of size n that can be formed
- Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day
- Imagine you have a special keyboard with four types of keys
- Given a string S, write a program to check if it is possible to construct the given string S by performing any of the below operations any number of times. In each step
- Given the number of digits n, find the count of total non-decreasing numbers with n digits
- You need to display N similar characters on a screen. You are allowed to do three types of operation each time
- Given an integer, S represented as a string, get the sum of all possible substrings of this string
- Given an array of N positive integers a1, a2, ..., an. The value of each contiguous subarray of a given array is the maximum element present in that subarray
- Weights and values are given for n items along with the maximum capacity allowed W. What is the maximum value we can achieve if we can pick any weights, any number of times for the total allowed capacity of W
- Given 3 strings X, Y and Z, the task is to find the longest common sub-sequence in all three given sequences
- Given a number n, count minimum steps to minimize it to 1
- Given a string str, find total number of possible palindromic sub-sequences. A sub-sequence does not need to be consecutive, but for any xixj i<j must be valid in the parent string too
- You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically cost of i kg packet of oranges
- Given a rectangular grid of dimension 2 x N find out the maximum sum such that no two chosen numbers are adjacent, vertically, diagonally or horizontally
- Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1\'s. Output your answer mod 10^9 + 7
- Given a string str, find the longest palindromic substring. A substring need to be consecutive such that for any xixj i<j must be valid in the parent string too. Like \"incl\" is a substring of \"includehelp\" while \"iel\" is not
- Given a string str, find total number of possible palindromic sub-sequences. A substring need to be consecutive such that for any xixj i<j must be valid in the parent string too. Like \"incl\" is a substring of \"includehelp\" while \"iel\" is not
- Given a sequence of numbers, you have to find the maximum sum alternating subsequence and print the value
- A list of n days is given to you and there are three columns. One is the day, the second one is high-effort value, and the another one is low-effort value
- Given a tree, you have to find out the largest independent set. Independent elements are those that don’t have any common edges. Print the length of the largest independent set
- Given a sequence of numbers you have to find out the longest alternating subsequence and print the length of the subsequence
- Given a sequence of numbers you have to print the longest alternating subsequence. A sequence is an alternating sequence when it will be maintaining like, (increasing) -> ( decreasing ) -> (increasing ) -> (decreasing) or (decreasing) -> (increasing) -> (decreasing) -> (increasing)
- You are trying to climb on a staircase. There are N number of steps. You can only climb one or two steps at one time. Now you have to find out the number of possible combinations to reach the final step
- An N pair array is given to you. In every pair, the first number is always smaller than the second number. A pair (a,b) can follow another pair (c,d) if a is greater than d
- Given a sequence of numbers you have to find out the length of the longest increasing odd even subsequence and print the length of the subsequence
- You are standing in some location (x, y) on an island, given in form of a matrix of size N*M, you are allowed to move in all four directions left, right, up and down
- Given N activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time
- You are given a list of jobs where each job has a start time, finish time, and the profit associated with that job, find the maximum profit subset of non-overlapping jobs
- Given a string, find out if the string is K-palindrome or not. A k-palindrome string transforms into a palindrome on removing at most k characters from it
- You are given a staircase, you need to find the total number of ways to reach the nth stair from the bottom of the staircase when you are only allowed to climb 1, 2 or 3 stairs at a time
- Given the mobile numeric keypad. You can only press buttons that are up, left, right or down to the current button. You are not allowed to press bottom row corner buttons (i.e. * and #)
- You are given a set of n types of rectangular 3-D boxes, where the i^th box has length l(i), width w(i), and height h(i) (all real numbers)
- You are given an array Arr, you have to find the maximum value of the expression (Arr[i1]-Arr[i2]+Arr[i3]-Arr[i4]) where i1>i2>i3>i4 and (0<=i1,i2,i3,i4<n)
- Given a N X N Matrix, Matrix[N][N] of positive integers. There are only three possible moves from a cell Matrix[r][c]
- Given a rope of length n meters, cut the rope in different parts of integer lengths in a way that maximizes the product of lengths of all parts. You must make at least one cut. Assume that the length of the rope is more than 2 meters
- Given an array A of N positive integers. Find the sum of maximum sum increasing subsequence of the given array
- Given a N*M binary matrix find the size of largest square sub-matrix of 1\'s present in it
- You are given a matrix of size N*M with each cell having some values, you need to find the number of paths from first cell to last bottom right cell such that the path has exactly given sum
- Given an array of integers and a sum, the task is to count all subsets of a given array with the sum equal to the given sum
- Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with a sum equal to the given sum
- Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum
This question belongs to these collections
Similar questions
- Given a value P, you have to make change for P cents, given that you have infinite supply of each of C { C1, C2, ... ,Cn} valued coins
- Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
- Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
- Given a value N, find the number of ways to make change for N cents, if we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins. The order of coins doesn\'t matter
Dynamic Programming
Since the problem can be solved with recursion with time complexity of O(2^n) which is not accepted as there are other better approach to solve the problem with time complexity of O(n*n);
Let's assume prob[i][j] to be the probability of getting j heads with first i coins. To get j heads at the ith position, there are two possibilities:
If the number of heads till (i - 1) coins is equal to j then a tail comes at ith. If the number of heads till (i - 1) coins is equal to (j - 1) then a head comes at ith position.
The probability of occurring jth head at ith coin toss depends on the number of heads in (i-1)th coins, if (i-1) coin have (j-1) head then jth coin occurs at ith coin(p[i]), and if (i-1) coins have already j coins then at jth toss tails come(1-p[i]).
Pseudo Code:
Time complexity: In worst case O(n*n)
C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer