Server Side Programming Articles - Page 1802 of 2646

Water and Jug Problem in C++

Arnab Chakraborty
Updated on 02-May-2020 08:04:37

2K+ Views

Suppose we have two jugs with capacities x and y liters. There is an infinite amount of water supply available to us. Now we need to determine whether it is possible to measure exactly z liters using these two jugs. If z liters of water are measurable, we must have z liters of water contained within one or both buckets by the end.We can do these few operations −Fill any of the jugs fully with water.Empty any of the jugs.Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.So ... Read More

Count Numbers with Unique Digits in C++

Arnab Chakraborty
Updated on 02-May-2020 08:01:38

1K+ Views

Suppose we have a non-negative integer n. We have to count all numbers with unique digits x, where x is in range 0 to 10^n. So if the number n is 2, then the result will be 91, as we want to find numbers from 0 to 100 without 11, 22, 33, 44, 55, 66, 77, 88, 99.To solve this, we will follow these steps −if n is 0, then return 1n := min of 10 and nif n is 1, then return 10ans := 9 and ret := 10for i in range 2 to nans := ans * (9 ... Read More

Integer Break in C++

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

320 Views

Suppose we have a positive integer n, we have to break it into the sum of at least two positive numbers and maximize the product of those integers. We have to find the maximum product we can get. So if the number is 10, then the answer will be 36, as 10 = 3 + 3 + 4, 3 * 3 * 4 = 36To solve this, we will follow these steps −Define a method solve(), this will take n, array dp and flagif n is 0, then return 1if dp[n] is not -1, then return dp[n]end := n – ... Read More

Reconstruct Itinerary in C++

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

280 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

512 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

715 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

303 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

804 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

311 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

451 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

Advertisements