Find Positive Integer Solution for a Given Equation in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:11:04

287 Views

Suppose we have a function f that takes two parameters (x, y). We have to return all pairs of x and y, for which f(x, y) = z. The z is given as input, and x, y are positive integers. The function is constantly increasing function. So f(x, y) < f(x + 1, y) and f(x, y) < f(x, y + 1).To solve this we will perform straight-forward approach. Take i in range 1 to 1000, and j in range 1 to 1000, for all combinations of i, j, if f(i, j) = 0, then return true, otherwise false.Consider the ... Read More

Insert into a Binary Search Tree in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:08:57

8K+ Views

Suppose we have a binary search tree. we have to write only one method, that performs the insertion operation with a node given as a parameter. We have to keep in mind that after the operation, the tree will remain BST also. So if the tree is like −if we insert 5, then the tree will be −To solve this, we will follow these steps −This method is recursive. this is called insert(), this takes a value v.if root is null, then create a node with given value v and make that as rootif value of root > v, thenleft ... Read More

Check If It Is a Straight Line in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:07:21

596 Views

Suppose we have a list of data-points consisting of (x, y) coordinates, we have to check whether the data-points are forming straight line or not. So if the points are like [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)], then they are forming straight line.To solve this, we will take the differences between each consecutive datapoints, and find the slope. For the first one find the slope. For all other points check whether the slope is same or not. if they are same, then simply return true, otherwise falseExampleLet us see the following implementation to get ... Read More

Split a String in Balanced Strings in C++

Arnab Chakraborty
Updated on 29-Apr-2020 08:05:26

502 Views

As we know that the balanced strings are those which have equal quantity of left and right characters. Suppose we have a balanced string s split it in the maximum amount of balanced strings. We have to return the maximum amount of splitted balanced strings. So if the string is “RLRRLLRLRL”, then output will be 4. as there are four balanced strings. “RL”, “RRLL”, “RL” and “RL” each substring has equal amount of L and R.To solve this, we will follow these steps −initialize cnt := 0, and ans := 0for i := 0 to size of the stringcnt := ... Read More

Partition to K Equal Sum Subsets in C++

Arnab Chakraborty
Updated on 29-Apr-2020 07:59:22

453 Views

Suppose we have an array of integers called nums and a positive integer k, check whether it's possible to divide this array into k non-empty subsets whose sums are all same. So if the array is like [4, 3, 2, 3, 5, 2, 1] and k = 4, then the result will be True, as the given array can be divided into four subarray like [[5], [1, 4], [2, 3], [2, 3]] with equal sums.To solve this, we will follow these steps −define two table called dp and total of size 2^n, sort the given array nums, set sum := ... Read More

Prime Arrangements in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:57:12

338 Views

We have to find the number of permutations of 1 to n, so the prime numbers are placed at prime indices. The answers may be large, return the answer modulo 10^9 + 7. So if n = 5, then output will be 12. So there will be 12 permutations. one possible permutation will be [1, 2, 5, 4, 3], one invalid permutation is [5, 2, 3, 4, 1] because 5 is placed at index 1, that is not prime.To solve this, we will follow these steps −Define one method called getNum, as follows −prime := list of all primes from ... Read More

Calculate Day of the Year in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:54:48

4K+ Views

Suppose, we have a date in the format “YYYY-MM-DD”. We have to return the day number of the year. So if the date is “2019-02-10”, then this is 41st day of the year.To solve this, we will follow these steps −Suppose D is an array of day count like [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]Convert the date into list of year, month and dayif the year is leap year then set date D[2] = 29Add up the day count up to the month mm – 1. and day count after that.ExampleLet us see ... Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:45

328 Views

Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0

Top K Frequent Words in C++

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:40

600 Views

Suppose we have a non-empty list of words; we have to find the k most frequent elements. our answer should be sorted by frequency from highest to lowest. When two words have the same frequency, then the word with the lower alphabetical order will be placed at first. So if the array is like [‘the’, ‘sky’, ‘is’, ‘blue’, ‘the’, ‘weather’, ‘is’, ‘comfortable’], so most frequent words are ["is", "the", "blue"]To solve this, we will follow these steps −Define one map called mcreate one priority queue vfor i := 0 to n, where n is the size of the word array, ... Read More

Defanging an IP Address in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:47:43

974 Views

Suppose we have a valid IPv4 IP address. We have to return the Defanged version of the IP address. A Defanged IP address is basically replace every period “.” by “[.]” So if the IP address is “192.168.4.1”, the output will be “192[.]168[.]4[.]1”To solve this, we will follow these steps −We will split the string using dot, then put each element separated by “[.]”ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object):    def defangIPaddr(self, address):       address = address.split(".")       return "[.]".join(address) ob1 = Solution() print(ob1.defangIPaddr("192.168.4.1"))Input"192.168.4.1"Output"192[.]168[.]4[.]1"

Advertisements