Found 33676 Articles for Programming

Search in a Sorted Array of Unknown Size in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:36:59

372 Views

Suppose we have an array and that is sorted in ascending order, we have to define a function to search target in nums. If the target is present, then return its index, otherwise, return -1.The array size is not known. We can only access the array using an ArrayReader interface. There is a get function like ArrayReader.get(k) this will return the element of the array at index k.So, if the input is like array = [-1, 0, 3, 5, 9, 12], target = 9, then the output will be 4 as 9 exists in nums and its index is 4To ... Read More

Number of Distinct Islands in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:35:04

278 Views

Suppose we have a binary 2D array grid, here an island is a group of 1's (land) connected 4- directionally (horizontal or vertical.) We can assume all four edges of the grid are surrounded by water. We have to count the number of distinct islands.An island is considered to be the same as another when one island can be translated (and not rotated or reflected) to equal the other.So, if the input is like11011100000000111011then the output will be 3To solve this, we will follow these steps −Define a function dfs(), this will take x, y, grid, temp, c, if x ... Read More

Next Closest Time in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:44:10

359 Views

Suppose we have a time represented in the format "HH: MM", we have to generate the next closest time by reusing the current digits. We can use the digit an unlimited number of times.So, if the input is like "19:34", then the output will be "19:39" as the next closest time choosing from digits 1, 9, 3, 4, is 19:39. It is not 19:33, because this occurs 23 hours and 59 minutes later.To solve this, we will follow these steps −Define a function eval(), this will take x, a := convert x[0] to stringa := a + x[1]b := convert ... Read More

Path Sum IV in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:42:17

291 Views

Suppose we have a list of integers that is representing a binary tree with a depth smaller than 5. If the depth of a tree is less than 5, then this tree can be represented by a list of three-digit integers. For each integer in this list −The hundreds digit is representing the depth D of this node, 1

Equal Tree Partition in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:38:49

465 Views

Suppose we have a binary tree with n nodes, our task is to check whether it's possible to partition the tree to two trees which have the equal sum of values after deleting exactly one edge on the original tree.So, if the input is likethen the output will be true.To solve this, we will follow these steps −Define one stack stDefine a function solve(), this will take node, if node is null, then −return 0leftSum := solve(left of node)rightSum := solve(right of node)curr := val + leftSum + rightSum of nodeinsert curr into streturn currFrom the main method do the ... Read More

4 Keys Keyboard in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:36:11

1K+ Views

Suppose we will try to write the letter ‘A’, using the keyboard. Our goal is to use only four keys and try to write maximum ‘A’s on the text field. The keys are ‘A’, ‘C’, ‘V’, and ‘Ctrl’.To write a maximum number of A, we will use Ctrl + A to select All, Ctrl + C to copy, and Ctrl + V to paste.So, if the input is like the number of keystrokes is 7 then the output will be 9 as of press A three times.Then Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+VTo solve this, we will follow these steps −if keyStrokes ... Read More

Design Log Storage System in Python

Arnab Chakraborty
Updated on 16-Nov-2020 14:32:07

389 Views

Suppose we have some logs, that each log contains a unique id and timestamp. The Timestamp is a string that has the format: Year:Month:Day:Hour:Minute: Second, for example, 2019:01:01:23:59:59. All domains are zero-padded decimal numbers.We have to design a log storage system to implement the following functions −void Put(int id, string timestamp): This will take the log's unique id and timestamp, and it stores the log in the storage system.int[] Retrieve(String start, String end, String granularity): This will return the id of logs whose timestamps are within the range from start to end parameters. The granularity parameter indicates the time level ... Read More

Find the Derangement of An Array in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:29:44

360 Views

Suppose we have an array consisting of n numbers from 1 to n in increasing order, we have to find the number of derangements it can generate.We know that in combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no element will appear in its original position. The answer may be very large, so return the output mod 10^9 + 7.So, if the input is like 3, then the output will be 2, as the original array is [1, 2, 3]. The two derangements are [2, 3, 1] and [3, 1, 2].To solve this, ... Read More

Minimum Factorization in C++

Arnab Chakraborty
Updated on 16-Nov-2020 14:26:26

138 Views

Suppose we have a positive integer x, we have to find the smallest positive integer b whose multiplication of each digit equals to x. If we have no such answer then return 0.So, if the input is like 48, then the output will be 68To solve this, we will follow these steps −ret := 0, mul := 1if a < 2, then:return afor initialize i := 9, when i >= 2, update (decrease i by 1), do −while a mod i is same as 0, do −ret := i * mul + retmul := mul * 10a := a / ... Read More

Add Bold Tag in String in C++

Nishu Kumari
Updated on 25-Jul-2025 13:00:47

3K+ Views

 A string is a sequence of characters, like words, numbers, or sentences, enclosed in double quotes ("). The given task is to add a closed pair of HTML bold tags around a string using C++.Let us understand this with an example: Input: abc Output: abc Add Bold Tag in String in C++ To add bold tags to a string in C++, we use string concatenation. This is the process of joining two or more strings together to form a new string. In C++, this can be done using the '+' operator. To wrap a string with bold ... Read More

Advertisements