Programming Articles - Page 1929 of 3363

Number of Digit One in C++

Arnab Chakraborty
Updated on 27-May-2020 05:32:06

451 Views

Suppose we have a number n, We have to count the total number of digit 1 appearing in all non-negative numbers less than or equal to n. So if the input is 15, then the output will be 8, because the numbers containing 1 is [1,10,11,12,13,14,15], there are 8 1s.To solve this, we will follow these steps −ret := 0for initializing i := 1, when i

Shortest Palindrome in C++

Arnab Chakraborty
Updated on 26-May-2020 14:06:52

793 Views

Suppose we have a string s. We can convert it to palindrome by adding characters in front of it. We have to find the shortest palindrome, that we can find performing this information. So if the string is like “abcc”, then the result will be − "ccbabcc".To solve this, we will follow these steps −n := size of s, s1 := s, s2 := sReverse the string s2s2 := s concatenate "#" concatenate s2Define an array lps of size same as s2j := 0, i := 1while i < size of s2, do −if s2[i] is same as s2[j], then, ... Read More

Word Search II in Python

Arnab Chakraborty
Updated on 26-May-2020 14:03:03

761 Views

Suppose we have a 2D board and a list of words. So from the dictionary, we have to find all words in the board. Here each word must be constructed from letters of sequentially adjacent cell, where the adjacent cells are those horizontally or vertically neighboring. We have to keep in mind that the same letter cell may not be used more than once in a word.So if the input is like −To solve this, we will follow these steps −make an array resultDefine a method called solve(), this will take board, d, i, j swhen either i or j ... Read More

Best Time to Buy and Sell Stock IV in C++

Arnab Chakraborty
Updated on 26-May-2020 13:57:46

222 Views

Suppose we have an array for which the i-th element is the price of a given stock for the day i. We have to devise an algorithm to find the maximum profit. We can complete at most k transactions. So if the input is like [3, 2, 6, 4, 0, 3] and k = 2, then the output will be 7, as buy on day 2 (when price = 2) and sell on day 3 (when price = 6), profit will be 6-2 = 4. Then buy on day 5 (price is 0) and sell on day 6 (price is ... Read More

Dungeon Game in C++

Arnab Chakraborty
Updated on 26-May-2020 13:52:55

774 Views

Suppose there is a story like the demons had captured the princess whose name is P and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M row, N column grid-like rooms. Our valiant knight named K was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.Now the knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies at that moment.Some of the rooms have demons to guard that room, so the ... Read More

Maximum Gap in C++

Arnab Chakraborty
Updated on 26-May-2020 13:50:25

515 Views

Suppose we have an array, that is not sorted. We have to find the maximum difference between successive elements in its sorted form. We will return 0 if the array contains less than 2 elements. So if the array is like [12, 3, 9, 1, 17], then the output will be 6, as the sorted array will be [1, 3, 9, 12, 17] so 5 will be the maximum difference as difference between 3 and 9 is 6.To solve this, we will follow these steps −minVal := inf, maxCal := -infn := size of numsif n < 2, then return ... Read More

Find Minimum in Rotated Sorted Array II in C++

Arnab Chakraborty
Updated on 26-May-2020 13:47:17

392 Views

Suppose we have an array that is sorted, now that is rotated at some pivot. The pivot is not known before. We have to find the minimum element from that array. So if the array is like [4, 5, 5, 5, 6, 8, 2, 3, 4], then the minimum element is 2.To solve this, we will follow these steps −Define one method called search(), this takes arr, low and highif low = high, then return arr[low]mid := low + (high – low) / 2ans := infif arr[low] < arr[mid], then ans := min of arr[low] and search(arr, mid, high)otherwise when ... Read More

Max Points on a Line in C++

Arnab Chakraborty
Updated on 26-May-2020 13:43:46

628 Views

Suppose we have a 2D plane. We have to find the maximum number of points that reside on the same straight line. So if the points are like −Then there are 4 pointsTo solve this, we will follow these steps −n := number of points, if n < 3, then return nans := 2for i in range 1 to n – 1count := 0take two points from index i and i – 1, these are p1, p2if p1 and p2 points are same, thenfor j in range 0 to n – 1if points[j].x = p1.x and points[j].y = p1.y, then ... Read More

Binary Tree Postorder Traversal in Python

Arnab Chakraborty
Updated on 26-May-2020 13:40:45

1K+ Views

Suppose we have a binary tree. We have to find the post order traversal of this tree using the iterative approach. So if the tree is like −Then the output will be: [9, 15, 7, 10, -10]To solve this, we will follow these steps −if root is null, then return empty arraycreate an array retstack := define a stack with pair [root, 0]while stack is not empty −node := top of stack, then delete element from stack.if second value of node pair is 0, thencurrent := first value of node pairinsert a pair (current, 1) into stackif right of current ... Read More

Word Break II in Python

Arnab Chakraborty
Updated on 26-May-2020 13:37:37

520 Views

Suppose we have a non-empty string s and a dictionary called wordDict, this dictionary is containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. We have to find all such possible sentences. “appleraincoat” and dictionary is [“app”, “apple”, “rain”, “coat”, “raincoat”]To solve this, we will follow these steps −Create one map memoDefine a method called solve, this will take string and wordDictif s is null, then return empty listif s in memo, then −return memo[s]create an array retfor i in range 1 to size of sif substring ... Read More

Advertisements