Found 7197 Articles for C++

Additive Number in C++

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

680 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

418 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

978 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

314 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

383 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

Contains Duplicate III in C++

Arnab Chakraborty
Updated on 02-May-2020 07:11:25

329 Views

Suppose we have an array of integers, we have to check whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t. And the absolute difference between i and j is at most k. So if input is like [1, 2, 3, 1], then if k = 3 and t = 0, then return true.To solve this, we will follow these steps −Make a set s, n := size of nums arrayfor i in range 0 to n – 1x is index of set element starting ... Read More

Bitwise AND of Numbers Range in C++

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

891 Views

Suppose we have a range [m, n] where 0 >= 1;          i++;       }       return m

Advertisements