Power of Two in C

Arnab Chakraborty
Updated on 28-Apr-2020 10:28:29

882 Views

Suppose we have a number n. We have to check whether the number is the power of 2 or not. So is n = 16, then the output will be true, if n = 12, it will be false.To solve this we will use logical operations. If we see the numbers that are the power of two then in the binary representation of that number will be the MSb is 1, and all other bits are 0. So if we perform [n AND (n – 1)], this will return 0 if n is the power of 2. If we see ... Read More

Invert Binary Tree in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:26:40

976 Views

Suppose we have a binary tree. our task is to create an inverted binary tree. So if the tree is like below −The inverted tree will be likeTo solve this, we will use a recursive approachif the root is null, then returnswap the left and right pointersrecursively solve left subtree and right subtreeExample (Python)Let us see the following implementation to get a better understanding − Live Democlass TreeNode:    def __init__(self, data, left = None, right = None):       self.data = data       self.left = left       self.right = right def make_tree(elements):    Tree = ... Read More

Contains Duplicate in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:59:35

991 Views

Suppose we have a list of numbers. We have to check whether the list is holding some duplicate elements or not. So if the list is like [1, 5, 6, 2, 1, 3], then it will return 1 as there are two 1s, but if the list is [1, 2, 3, 4], then it will be false, as there is no duplicate present.To solve this, we will follow this approach −We know that the set data structure only holds unique data. But the list can fold duplicate contents. So if we convert the list into the set, its size will ... Read More

Reverse Linked List in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:57:13

518 Views

Suppose we have a linked list, we have to reverse it. So if the list is like 1 → 3 → 5 → 7, then the new reversed list will be 7 → 5 → 3 → 1To solve this, we will follow this approach −Define one procedure to perform list reversal in a recursive way as to solve(head, back)if the head is not present, then return headtemp := head.nexthead.next := backback = headif temp is empty, then return headhead = tempreturn solve(head, back)ExampleLet us see the following implementation to get a better understanding − Live Democlass ListNode:    def __init__(self, ... Read More

Count Primes in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:53:26

4K+ Views

Suppose we have a limit n. We have to count the number of primes present in the range 2 to n. So if n = 10, the result will be 4. As there are four primes before 10, they are 2, 3, 5, 7.To solve this, we will follow this approach −count = 0take one array prime = of size n + 1, and fill it with Falsefor i = 0 to n, doif prime[i] = false, thenincrease count by 1set j = 2while j * i

House Robber Problem in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:50:44

1K+ Views

Suppose there is a city, and each house in the city has a certain amount. One robber wants to rob the money in one single night. The city has one security system, that is as if two consecutive houses are broken on the same night, then it will automatically call the police. So we have to find how the maximum amount the robber can rob?One array is provided, at index i, the A[i] is the amount that is present in i-th house. Suppose the array is like: A = [2, 7, 10, 3, 1], then the result will be 13. ... Read More

Number of 1 Bits in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:48:29

8K+ Views

Suppose we have an unsigned number n. We have to find the number of 1s in a binary representation of this number. This is also known as Hamming Weight. So if the number is like 000000101101, then the result will be 4.To solve this, we will use these steps −Take the number and convert it into a binary stringset count = 0for each character e in a binary stringif the character is ‘1’, then increase count by 1return countExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def hammingWeight(self, n):       """ ... Read More

Rotate Array in Python

Arnab Chakraborty
Updated on 28-Apr-2020 09:45:51

711 Views

Suppose we have an array A. We have to rotate right it k steps. So if the array is A = [5, 7, 3, 6, 8, 1, 5, 4], and k = 3, then the output will be [1, 5, 4, 5, 7, 3, 6, 8]. The steps are like[4, 5, 7, 3, 6, 8, 1, 5][5, 4, 5, 7, 3, 6, 8, 1][1, 5, 4, 5, 7, 3, 6, 8]To solve this, we will follow these steps.let n is the size of the arrayk = k mod nA = subarray of A from n – k to end + ... Read More

Factorial Trailing Zeroes in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:41:55

265 Views

Here we will see how to calculate the number of trailing 0s for the result of factorial of any number. So if the n = 5, then 5! = 120. There is only one trailing 0. For 20! it will be 4 zeros as 20! = 2432902008176640000.The easiest approach is just calculating the factorial and count the 0s. But this approach fails for a large value of n. So we will follow another approach. The trailing zeros will be there if the prime factors are 2 and 5. If we count the 2s and 5s we can get the result. ... Read More

Excel Sheet Column Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 09:40:10

821 Views

We know that the excel column numbers are alphabetic. It starts from A, and after Z, it will AA, AB, to ZZ, then again AAA, AAB, to ZZZ and so on. So column 1 is A, column 27 is Z. Here we will see how to get the column letter if a number of columns is given. So if the column number is 80, then it will be CB.Suppose we have a number n, and its value is 28, then we need to take a reminder with 26. If the remainder is 0, then the number is 26, 52 and ... Read More

Advertisements