Reverse String in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:55:53

2K+ Views

Suppose we have an array of characters. We have to reverse the string without using any additional space. So if the string is like [‘H’, ‘E’, ‘L’, ‘L’, ‘O’], the output will be [‘O’, ‘L’, ‘L’, ‘E’, ‘H’]To solve this, we will follow these steps −Take two pointers to start = 0 and end = length of the string – 1swap first and last charactersincrease start by 1 and decrease-end by 1ExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def reverseString(self, s):       """       :type s: List[str]   ... Read More

Power of Three in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:52:30

2K+ Views

Suppose we have a number n. We have to check whether the number is the power of 3 or not. So if the number is like n = 27, that is the power of 3, the result will be true, if n = 15, it will be false.To solve this, we will follow these steps −We will use the Logarithm to solve thisif [log10(n) / log10(3)] mod 1 == 0, then it will be power of three, otherwise notExampleLet us see the following implementation to get a better understanding − Live Democlass Solution(object):    def isPowerOfThree(self, n):       """       :type n: int       :rtype: bool       """       if not n or n

Range Sum Query with Immutables in C++

Arnab Chakraborty
Updated on 28-Apr-2020 10:50:14

207 Views

Suppose we have an array of integers. We have to find the sum of the elements present from index i to j. Two things we have to keep in mind that the array will be immutable, so elements will not be altered, and there will be multiple such queries. So we have to care about the execution time for a large number of queries. Suppose the array is like A = [5, 8, 3, 6, 1, 2, 5], then if query is (A, 0, 3), then it will be 5 + 8 + 3 + 6 = 22.To solve this, ... Read More

Move Zeroes in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:46:18

1K+ Views

Suppose we have an array to hold some numbers. There are non-zero values as well as zero values. So we have to send all zeros to the right without changing the relative order of other numbers. So if the array is like [0, 1, 5, 0, 3, 8, 0, 0, 9], then the final array will be [1, 5, 3, 8, 9, 0, 0, 0, 0]To solve this, we will follow these steps −Suppose index = 0for i = 0 to the length of Aif A[i] != 0, thenA[index] := A[i]index := index + 1for i = index to the ... Read More

Find Missing Number in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:43:48

487 Views

Suppose we have a list of numbers from 0 to n. There is one number that is missing. We have to find the missing number in an efficient approach. So if A = [0, 1, 2, 3, 4, 5, 7, 8, 9], missing number is 6.To solve this, we will use the binary search approach.sort the list in ascending orderhigh = length of A, and low = 0while low < high, domid = low + (high – low)/2if A[mid] > midhigh = midotherwiselow = mid + 1return lowExampleLet us see the following implementation to get a better understanding − Live Democlass ... Read More

Ugly Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 10:39:27

1K+ Views

Ugly numbers are those numbers whose prime factors are 2, 3 or 5. From 1 to 15, there are 11 ugly numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15. The numbers 7, 11, 13 are not ugly because they are prime. The number 14 is not ugly because in its prime factor the 7 will come. So suppose we want to check the 10th ugly number. The value will be 12.Let us see the following algorithm to get an idea −AlgorithmgetUglyNumbers(n)Input − The number of terms.Output − Find nth Ugly numbers.Begin    define array named uglyNum ... Read More

Valid Anagram in Python

Arnab Chakraborty
Updated on 28-Apr-2020 10:33:32

678 Views

Anagrams are basically all permutations of a given string or pattern. This pattern searching algorithm is slightly different. In this case, not only the exact pattern is searched, it searches all possible arrangements of the given pattern in the text. So if the inputs are “ANAGRAM” and “NAAGARM”, then they are anagram, but “cat” and “fat” are not an anagramTo solve this, we will convert the string into a list of characters, then sort them, if two sorted lists are same then they are anagram.Example (Python)Let us see the following implementation to get a better understanding − Live Democlass Solution(object):   ... Read More

Power of Two in C

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

943 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

1K+ 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

1K+ 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

Advertisements