Articles on Trending Technologies

Technical articles with clear explanations and examples

Closest Divisors in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 468 Views

Suppose we have an integer num, we have to find the closest two integers in absolute difference whose product equals num + 1 or num + 2. We have to find the two integers in any order. So if the input is 8, then the output will be [3, 3], for num + 1, it will be 9, the closest divisors are 3 and 3, for num + 2 = 10, the closest divisors are 2 and 5, hence 3 and 3 are chosen.To solve this, we will follow these steps −Define a method called getDiv(), this will take x as inputdiff := infinity, create an array called ret of size 2for i := 1, if i^2

Read More

Validate Binary Tree Nodes in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 435 Views

Suppose we have n binary tree nodes numbered from 0 to n - 1 where node I has two children leftChild[i] and rightChild[i], we have to say true if and only if all the given nodes form exactly one valid binary tree. When node i has no left child then leftChild[i] will equal -1, similarly for the right child. We have to keep in mind that the nodes have no values and that we only use the node numbers in this problem. So if the input is like −Then the output will be true.To solve this, we will follow these ...

Read More

Number of Substrings Containing All Three Characters in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 733 Views

Suppose we have given a string s consisting only of characters a, b and c. We have to return the number of substrings containing at least one occurrence of all these characters a, b and c. So for example, if the string is “abcabc”, then the output will be 10, as the substring containing at least one occurrence of the characters a, b and c, these are “abc”, “abca”, “abcab”, “abcabc”, “bca”, “bcab”, “cab”, “cabc” and “abc” (again for the last part).To solve this, we will follow these steps −ret := 0, make a map called m, set j := ...

Read More

Apply Discount Every n Orders in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 381 Views

Suppose there is a sale in a supermarket, there will be a discount every n customer. Consider there are some products in the supermarket where the id of the i-th product is products[i] and the price per unit of this product is prices[i]. Here the system will count the number of customers and when the n-th customer arrives he/she will have a discount on the bill. Then the system will start counting customers again. The customer orders a certain amount of each product where product[i] is the id of the i-th product the customer ordered and amount[i] is the number ...

Read More

Maximum Number of Events That Can Be Attended in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 903 Views

Suppose we have an array of events where events[i] = [startDayi, endDayi]. Here every event I start at startDayi and ends at endDayi. We can attend an event I at any day d where d in range startTimei and endTimei (both inclusive). We have to keep in mind that we can only attend one event at any time. So find the maximum number of events we can attend. So for example, if the input is like [[1, 4], [4, 4], [2, 2], [3, 4], [1, 1]], then the output will be 1, as we can attend maximum of four events, ...

Read More

Product of the Last K Numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 392 Views

Suppose we have the implement the class called ProductOfNumbers that supports two methods −add(int num): This adds the number num to the back of the current list of numbers.getProduct(int k): This returns the product of the last k numbers in the current list.We can assume that always the current list has at least k numbers. So for example, if the input is like − add(3), add(0), add(2), add(5), add(4), getProduct(2), getProduct(3), getProduct(4), add(8), getProduct(2), then the output will be (after each function call) −[3], [3, 0], [3, 0, 2], [3, 0, 2, 5], [3, 0, 2, 5, 4], then (5 ...

Read More

Insert Delete GetRandom O(1) in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 605 Views

Suppose we have a data structure that supports all following operations in average O(1) time.insert(val) − This will Insert an item val to the set if that is not already present.remove(val) − This will remove an item val from the set if it presents.getRandom − This will return a random element from current set of elements. Each element must have the same probability of being returned.To solve this, we will follow these steps −For the initialization, define a parent map, and elements arrayFor the insert() function, it will take val as inputif val is not present in parent map, or ...

Read More

Flatten Nested List Iterator in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 477 Views

Suppose we have a nested list of integers; we have to implement an iterator to flatten it. Each element is either an integer, or a list. The elements of that list may also be integers or other lists. So if the input is like [[1, 1], 2, [1, 1]], then the output will be [1, 1, 2, 1, 1]To solve this, we will follow these steps −In the initializing section, it will take the nested list, this will work as follows −set res as empty list, index := 0, call getVal(nestedList)The getVal() will take nestedIntegers, this will work as −for ...

Read More

Super Ugly Number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 389 Views

We have to create one function to find the nth super ugly number. The super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. So if the n is 12 and primes are [2, 7, 13, 19], then the output will be 32, this is because [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of 12 super ugly numbers.To solve this, we will follow these steps −Create a data structure triplet, with num, prime and idxif n is 1, then return 1, create ...

Read More

Reduce Array Size to The Half in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 1K+ Views

Suppose we have an array arr. We can choose a set of integers and remove all the occurrences of these integers in the array. We have to find the minimum size of the set so that at least half of the integers of the array are removed. So for example, if arr = [3, 3, 3, 3, 5, 5, 5, 2, 2, 7], then the output will be 2. This is because if we choose {3, 7} this will make the new array [5, 5, 5, 2, 2] which has size 5 (this is equal to half of the size ...

Read More
Showing 47671–47680 of 61,248 articles
Advertisements