Found 7197 Articles for C++

Length of Last Word in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:06:26

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
Updated on 10-Jun-2020 12:06:01

554 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

Difference between Set and UnOrderSet in C++

Nitin Sharma
Updated on 09-Jun-2020 09:00:59

210 Views

In C++ both Set and UnOrderSet are the type of data structures which are used to store the data for easy accessing and insertion. On the basis of characteristics of both these data structures we can distinguish between Set and UnOrderSetFollowing are the important differences between Set and UnOrderSet −Sr. No.KeySetUnOrderSet1DefinitionSet in C++ can be defined as a type of associative container which stores the data in key value pair and in which each value element has to be unique, because the value of the element identifies it.On other hand UnOrderSet are part of the C++ STL (Standard Template Library) ... Read More

Difference between Set and MultiSet in C++

Nitin Sharma
Updated on 09-Jun-2020 08:59:29

5K+ Views

In C++, both Set and MultiSet are the type of data structures which are used to store the data for easy accessing and insertion. On the basis of characteristics of both these data structures we can distinguish between Set and MultiSet.Following are the important differences between Set and MultiSet −Sr. No.KeySetMultiSet1DefinitionSet in C++ can be defined as a type of associative container which stores the data in key value pair and in which each value element has to be unique, because the value of the element identifies it.On other hand MultiSet are part of the C++ STL (Standard Template Library) ... Read More

Binary Tree Postorder Traversal in Python Programming

Arnab Chakraborty
Updated on 09-Jun-2020 07:35:29

239 Views

Suppose we have a binary tree. We have to find the post order traversal of this tree using 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 is ... Read More

Distinct Subsequences in C++ Programming

Arnab Chakraborty
Updated on 09-Jun-2020 07:30:43

317 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
Updated on 09-Jun-2020 07:27:51

320 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
Updated on 09-Jun-2020 07:24:09

195 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
Updated on 09-Jun-2020 07:22:55

172 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

Restore The Array in C++

Arnab Chakraborty
Updated on 09-Jun-2020 07:21:32

362 Views

Suppose there is a program that is used to print the array elements of an array A, but there was a little mistake in the program. In that program there was no white space after each element, so if we have one printed string, can we regenerate the array again? We know the array elements are in range 1 to k.Given the string s and the integer k. We have to find how many number of ways we can restore the array. The answer may be very large so return it modulo 10^9 + 7.So, if the input is like ... Read More

Advertisements