Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 238 of 377

Pascal's Triangle II in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 10-Jun-2020 385 Views

Suppose we have a non-negative index k where k ≤ 33, we have to find the kth index row of Pascal's triangle.So, if the input is like 3, then the output will be [1,3,3,1]To solve this, we will follow these steps −Define an array pascal of size rowIndex + 1 and fill this with 0for initialize r := 0, when r

Read More

Minimum Depth of Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 10-Jun-2020 196 Views

Suppose we have a binary tree; we have to find the minimum depth of that tree. As we know the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.So, if the input is likethen the output will be 2To solve this, we will follow these steps −Define an array aa of tree nodesinsert root at the end of aaDefine another array ak of tree nodeslevel := 0if root is null, then −return 0while size of aa is not equal to 0, do −clear the array ak(increase level by ...

Read More

Length of Last Word in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 10-Jun-2020 1K+ Views

Suppose we have a string s. s can hold any English letters and white-spaces. We have to find the length of last word in the string. If there is no last word, then return 0.So, if the input is like "I love Programming", then the output will be 11To solve this, we will follow these steps −n := 0for each word temp in a string −n := size of tempreturn nExampleLet us see the following implementation to get a better understanding − Live Demo#include using namespace std; class Solution { public:    int lengthOfLastWord(string s){       stringstream str(s); ...

Read More

Search Insert Position in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 10-Jun-2020 615 Views

Suppose we have a sorted array arr and a target value, we have to find the index when the target is found. If that is not present, then return the index where it would be if it were inserted in order.So, if the input is like [1, 3, 4, 6, 6], and target = 5, then the output will be 3, as we can insert 5 at index 3, so the array will be [1, 3, 4, 5, 6, 6]To solve this, we will follow these steps−n := size of Aif n < 1, then −return 0low := 0, high ...

Read More

Remove Element in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 10-Jun-2020 245 Views

Suppose we have an array num and another value val, we have to remove all instances of that value in-place and find the new length.So, if the input is like [0, 1, 5, 5, 3, 0, 4, 5] 5, then the output will be 5.To solve this, we will follow these steps −count := 0for each index i of numsif nums[i] is not equal to val, then −nums[count] := nums[i]count := count + 1return countExampleLet us see the following implementation to get a better understanding − Live Democlass Solution:    def removeElement(self, nums, val):       count = 0   ...

Read More

Integer to English Words in Python Programming

Arnab Chakraborty
Arnab Chakraborty
Updated on 09-Jun-2020 2K+ Views

Suppose we have a number. The number can be anything in between 0 to 231 – 1. We have to convert the number into words. So if the number is like 512, then the result will be Five hundred twelve.To solve this, we will follow these steps −Define some lists like less_than_20, this will hold all words from one to nineteenAnother array like tens to hold tens, twenty, thirty and so on up to ninetyAnother array for thousands, to hold thousand, million and billionDefine one function called helper(), this will take nif n is 0, then return blank stringotherwise when ...

Read More

Distinct Subsequences in C++ Programming

Arnab Chakraborty
Arnab Chakraborty
Updated on 09-Jun-2020 352 Views

Suppose we have strings S and T. We have to count number of distinct sequences of S which is equal to T.We know that a subsequence of a string is a new string which is formed from the original string by removing some (can be none) of the characters without disturbing the relative positions of the remaining characters. (Like, "ACE" is a subsequence of "ABCDE" while "AEC" is not).If the input strings are “baalllloonnn” and “balloon”, then there will be 36 different ways to select.To solve this, we will follow these steps −n := size of s, m := size ...

Read More

Find the Kth Smallest Sum of a Matrix With Sorted Rows in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 09-Jun-2020 360 Views

Suppose we have one m * n matrix called mat, and an integer k, mat has its rows sorted in nondecreasing order. We can choose exactly one element from each row to form an array. We have to find the Kth smallest array sum among all possible arrays.So, if the input is like mat = [[1, 3, 11], [2, 4, 6]]1311246and k = 5, then the output will be 7, as when we choose one element from each row the first k smallest sums are [1, 2], [1, 4], [3, 2], [3, 4], [1, 6]. here the 5th sum is ...

Read More

Number of Ways to Wear Different Hats to Each Other in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 09-Jun-2020 231 Views

Suppose there are n people and 40 different types of hats those are labeled from 1 to 40. Now a 2D list is given called hats, where hats[i] is a list of all hats preferred by the i-th person. We have to find the number of ways that the n people wear different hats to each other. The answer may come very large, so return the answer modulo 10^9 + 7.So, if the input is like [[4, 6, 2], [4, 6]], then the output will be 4, as there are 4 different ways to choose, these are [4, 6], [6, ...

Read More

Constrained Subsequence Sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 09-Jun-2020 215 Views

Suppose we have an array called nums and an integer k, we have to find the maximum sum of a non-empty subsequence of that array such that for every two consecutive numbers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i k and first element of dq is same as dp[i - k - 1], thendelete front element from dqdp[i] := maximum of dp[i] and (if dq is empty, then dp[i] + 0, otherwise first element of dp + dq[i])while (not dq is empty and last element of dq < dp[i]), do −delete ...

Read More
Showing 2371–2380 of 3,768 articles
« Prev 1 236 237 238 239 240 377 Next »
Advertisements