Found 26504 Articles for Server Side Programming

Reconstruct Itinerary in C++

Arnab Chakraborty
Updated on 02-May-2020 07:53:32

260 Views

Suppose we have a list of airline tickets represented by pairs of departure and arrival airports like [from, to], we have to reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. So, the itinerary must begin with JFK.So if the input is like [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]], then the output will be ["JFK", "MUC", "LHR", "SFO", "SJC"].To solve this, we will follow these steps −Define array ret and a map called graph.Define a method called visit. This will take airport name as inputwhile size of the graph[airport] is ... Read More

Maximum Product of Word Lengths in C++

Arnab Chakraborty
Updated on 02-May-2020 07:50:19

487 Views

Suppose we have a string array called words, find the maximum value of length(word[i]) * length(word[j]) where the two words will not share the common letters. We can assume that each word will contain only lower case letters. If no such two words exist, then return 0. So if the input is like [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”], then the output will be 16, as two words can be “abcw”, “xtfn”.To solve this, we will follow these steps −Define a method called getRev(), this will take x as inputret := 0for i in range 0 to 25if x / ... Read More

Additive Number in C++

Nishu Kumari
Updated on 28-Jul-2025 19:30:40

685 Views

We are given a string containing only digits from 0 to 9, and we need to check whether it is an additive number. An additive number is a string that can form an additive sequence, where each number (starting from the third) is the sum of the previous two. For the sequence to be valid, it must have at least three numbers. Let's look at the example scenarios to understand the problem clearly: Scenario 1 Input: "112358" Output: true Explanation: The digits can form the sequence: 1, 1, 2, 3, 5, 8 Here, 1 + 1 = 2 1 ... Read More

H-Index II in C++

Arnab Chakraborty
Updated on 02-May-2020 07:35:49

277 Views

Suppose we have an array of citations (The citations are non-negative integers) of a researcher. These numbers are sorted in non-decreasing order. We have to define a function to compute the researcher's h-index. According to the definition of h-index: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."So if the input is like citations = [0, 1, 4, 5, 6], then the output will be 3, as it indicates that the researcher has five papers, they have got ... Read More

H-Index in C++

Arnab Chakraborty
Updated on 02-May-2020 07:29:02

752 Views

Suppose we have an array of citations (The citations are non-negative integers) of a researcher. We have to define a function to compute the researcher's h-index. According to the definition of h-index: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."So if the input is like citations = [3, 0, 6, 1, 7], then the output will be 3, as it indicates that the researcher has five papers, they have got 3, 0, 6, 1, 7 citations respectively. ... Read More

Ugly Number II in C++

Arnab Chakraborty
Updated on 02-May-2020 07:26:14

290 Views

Suppose we have to find the nth ugly number, so we have to define a method that can find it. As we know that the ugly numbers are those numbers, whose prime factors are only 2, 3 and 5. So if we want to find 10th ugly number, that will be 12, as the first few ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12To solve this, we will follow these steps −Make an array v, of size n + 1if n = 1, then return 1two := 2, three = 3 and five = 5, ... Read More

Single Number III in C++

Arnab Chakraborty
Updated on 02-May-2020 07:23:28

420 Views

Suppose we have an array, there exactly two elements appear once, but others are appearing twice. So we have to define a function, that will find these two numbers. So if the given array is like [1, 2, 3, 1, 5, 2], then the output will be [3, 5].To solve this, we will follow these steps −xor_res := 0for i in range 0 to size of numsxor_res := xor_res XOR nums[i]pos := 0while xor_res AND 2^pos = 0, do, increase pos by 1num1 := 0for i in range 0 to size of nums – 1if nums[i] and 2 ^ pos ... Read More

Different Ways to Add Parentheses in C++

Arnab Chakraborty
Updated on 02-May-2020 07:21:21

979 Views

Suppose we have a string of numbers and operators, we have to find all possible results from computing all the different possible ways to group the numbers and operators. Here the valid operators are +, - and *. So if the input is like “2*3-4*5”, then the output will be [-34, -14, -10, -10, 10]. This is because −(2*(3-(4*5))) = -34((2*3)-(4*5)) = -14((2*(3-4))*5) = -10(2*((3-4)*5)) = -10(((2*3)-4)*5) = 10To solve this, we will follow these steps −Define a map called a memo.Define a method called solve(). This will take the input string as input.create an array called retif the memo ... Read More

Majority Element II in C++

Arnab Chakraborty
Updated on 02-May-2020 07:17:52

316 Views

Suppose we have one integer array; we have to find those elements that appear more than floor of n/3. Here n is the size of array.So if the input is like [1, 1, 1, 3, 3, 2, 2, 2], then the results will be [1, 2]To solve this, we will follow these steps −first := 0, second := 1, cnt1 := 0, cnt2 := 0, n := size of array numsfor i in range 0 to size of n – 1x := nums[i]if x is first, then increase cnt by 1, otherwise when x is second, then increase cnt2 by ... Read More

Rectangle Area in C++

Arnab Chakraborty
Updated on 02-May-2020 07:14:23

385 Views

Suppose we want to find the total area covered by two rectilinear rectangles in a 2D plane. Here each rectangle is defined by its bottom left corner and top right corner as shown in the figure.To solve this, we will follow these steps −if C = E or A >= G or B >= H or D = H || D

Advertisements