Found 7347 Articles for C++

Reverse Words in a String II in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:15:16

743 Views

Suppose we have one input string, we have to reverse the strings word by word.So, if the input is like ["t", "h", "e", " ", "m", "a", "n", " ", "i", "s", " ", "n", "l", "c", "e"], then the output will be ["n", "l", "c", "e", " ", "i", "s", " ", "m", "a", "n", " ", "t", "h", "e"]To solve this, we will follow these steps −reverse the array sj := 0n := size of sfor initialize i := 0, when i < n, update (increase i by 1), do −if s[i] is same as ' ', then ... Read More

Missing Ranges in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:13:29

169 Views

Suppose we have a sorted integer array nums, the range of elements are in the inclusive range [lower, upper], we have to find the missing ranges.So, if the input is like nums = [0, 1, 3, 50, 75], and lower value is 0 and upper value is 99, then the output will be ["2", "4->49", "51->74", "76->99"]To solve this, we will follow these steps −Define an array numsDefine one set vfor initialize i := 0, when i < size of t, update (increase i by 1), do −if t[i] is not in v, then −insert t[i] into vinsert t[i] at ... Read More

One Edit Distance in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:10:40

113 Views

Suppose we have two strings s and t; we have to check whether they are both one edit distance apart. The one edit distance has three types −Insert a character into s to get tDelete a character from s to get tReplace a character of s to get tSo, if the input is like s = "ab", t = "acb", then the output will be TrueTo solve this, we will follow these steps −n := size of s, m := size of tif n < m, then −return isOneEditDistance(t, s)for initialize i := 0, when i < m, update (increase ... Read More

Longest Substring with At Most Two Distinct Characters in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:09:04

1K+ Views

Suppose we have a string s; we have to find the length of the longest substring t that has at most 2 distinct characters.So, if the input is like "eceba", then the output will be 3 as t is "ece" which its length is 3.To solve this, we will follow these steps −Define a function lengthOfLongestSubstringKDistinct(), this will take s, k, ans := 0Define one map mn := size of s, x := 0for initialize j := 0, i := 0, when j < n, update (increase j by 1), do −(increase m[s[j]] by 1)if m[s[j]] is same as 1, ... Read More

Binary Tree Upside Down in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:07:24

258 Views

Suppose we have a binary tree where all the right nodes are either leaf nodes with a sibling otherwise empty, we have to flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. We have to return the new node.So, if the input is like [1, 2, 3, 4, 5]then the output will return the root of the binary tree [4, 5, 2, #, #, 3, 1]To solve this, we will follow these steps −Define a function solve(), this will take node, par, sibling, if node is not present, then ... Read More

Synonymous Sentences in C++

Arnab Chakraborty
Updated on 18-Nov-2020 11:03:31

342 Views

Suppose we have a list of pairs of equivalent words synonyms and a sentence text, we have to find all possible synonymous sentences they are sorted lexicographically.So, if the input is like synonyms = [["happy", "joy"], ["sad", "sorrow"], ["joy", "cheerful"]], and text = "I am happy today but was sad yesterday", then the output will be ["I am cheerful today but was sad yesterday", "I am cheerful today but was sorrow yesterday", "I am happy today but was sad yesterday", "I am happy today but was sorrow yesterday", "I am joy today but was sad yesterday", "I am joy today ... Read More

The k Strongest Values in an Array in C++

Arnab Chakraborty
Updated on 18-Nov-2020 10:56:36

153 Views

Suppose we have an array of numbers called arr and an integer k. There is a value arr[i] that is said to be stronger than a value arr[j] when |arr[i] - m| > |arr[j] - m| where m is the median of the array. If |arr[i] - m| is same as the |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j]. So we have to find a list of the strongest k values in the array.So, if the input is like arr = [1, 2, 3, 4, 5], k = 2, then the ... Read More

Reorder Routes to Make All Paths Lead to the City Zero in C++

Arnab Chakraborty
Updated on 18-Nov-2020 10:54:10

368 Views

Suppose there are n different cities those are numbered from 0 to n-1 and there are also n-1 roads such that there is only one way to travel between two different cities. Suppose the ministry of transport decided to orient the roads in one direction because they are too narrow.Here the roads are represented by connections where connections[i] = [a, b] this represents a road from city a to b.If there is a big event in the capital (city numbered as 0), and many people want to travel to this city. We have to perform some reorienting task on some ... Read More

Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts in C++

Arnab Chakraborty
Updated on 18-Nov-2020 10:51:57

139 Views

Suppose we have a rectangular cake with height h and width w, we also have two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] represents the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] represents distance from the left of the rectangular cake to the jth vertical cut.We have to find the maximum area of a piece of cake after we cut it at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. The answer may be large, so return this modulo 10^9 + 7.So, if the input is ... Read More

Course Schedule IV in C++

Arnab Chakraborty
Updated on 18-Nov-2020 10:49:16

238 Views

Suppose there are a total of n courses we can to take, the courses are labeled from 0 to n-1.Some courses may have direct prerequisites, as example, to take course 0 we have first to take course 1, which is expressed as a pair: [1, 0].So, if we have a number of courses n, a list of direct prerequisite pairs and a list of queries pairs.You should find the answer for each queries[i] whether the course queries[i][0] is a prerequisite of the course queries[i][1] or not. Finally, we have to return a list of boolean, the answers to the given ... Read More

Advertisements