Suppose we have an array; we have to check whether given number x is the majority element of that array or not. The array is sorted. One element is said to be the majority element when it appears n/2 times in the array. Suppose an array is like {1, 2, 3, 3, 3, 3, 6}, x = 3, here the answer is true as 3 is the majority element of the array. There are four 3s. The size of the array is 7, so we can see 4 > 7/2.We can count the occurrences of x in the array, and ... Read More
Suppose we have an array A. In this array there are many numbers that occur twice. Only one element can be found a single time. We have to find that element from that array. Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there is each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res = res XOR ereturn resExampleLet us see ... Read More
Suppose we have a string with alphanumeric values and symbols. There are lower case and uppercase letters as well. We have to check whether the string is forming a palindrome or not by considering only the lowercase letters (uppercases will be converted into lower case), other symbols like a comma, space will be ignored.Suppose the string is like “A Man, a Plan, a Canal: Panama”, then by considering these rules, it will be “amanaplanacanalpanama”. This is a palindrome.To solve this, follow these steps −define x = “”read each character c in str −if c is lowercase letter or number, then ... Read More
Suppose we have an array A, here A[i] is indicating the price of a given stock on day i. We have to find the maximum profit. We can complete as many transactions as we like. (Transaction means to buy and sell stocks). But we have to keep in mind that we may not engage in multiple transactions at the same time. So we have to sell the stock before buying the new one.Suppose the array is like A = [7, 1, 5, 3, 6, 4], then the result will be 7. As we can see, if we buy on day ... Read More
Suppose we have an array A, here A[i] is indicating the price of a given stock on day i. We have to find the maximum profit. We can complete at most one transaction. (Transaction means to buy and sell stocks). But we have to keep in mind that we may not engage in multiple transactions at the same time. So we have to sell the stock before buying the new one.Suppose the array is like A = [7, 1, 5, 3, 6, 4], then the result will be 5. As we can see, if we buy on day 2 (index ... Read More
Suppose a decimal number can be converted to its Hexspeak representation by converting it to an uppercase hexadecimal string at first, after that replacing all occurrences of the digit 0 with the letter O, and the digit 1 with the letter I.This kind of representation is valid if and only if it consists only of the letters in the set {"A", "B", "C", "D", "E", "F", "I", "O"}.So we have a string num representing a decimal integer N, we have to find the Hexspeak representation of N if it is correct, otherwise return "ERROR". So if num = “257”, then ... Read More
Suppose we have one tree and a sum. We have to find one path such that if we follow that path, we will get the sum that will be matched with the given sum. Suppose the tree is like [0, -3, 9, -10, null, 5] and the sum is 14, then there is a path 0 → 9 → 5To solve this, we will follow these steps.If the root is null, then return Falseif left and right subtree are empty, then return true when sum – root.val = 0, otherwise falsereturn solve(root.left, sum – root.val) or solve(root.right, sum – root.val)Let ... Read More
Suppose we have a m x n matrix filled with non-negative integers, find a path from top left corner to bottom right corner which minimizes the sum of all numbers along its path. Movements can only be either down or right at any point in time. So for example, if the matrix is like below131151421The output will be 7, the path will be 1, 3, 1, 1, 1, this will minimize the sumLet us see the steps −a := number of rows, b := number of columnsi := a – 1, j := b - 1while j >= 0matrix[a, j] ... Read More
Suppose we have one sorted array A. We have to generate one height-balanced binary search. In this problem, a height-balanced binary tree is actually a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Suppose the array is like [-10, -3, 0, 5, 9]. So one possible output will be like: [0, -3, 9, -10, null, 5]To solve this, we will follow these steps.If A is empty, then return Nullfind the mid element, and make it rootDivide the array into two sub-arrays, left part of the mid element, and right ... Read More
Suppose there are three integer arrays arr1, arr2 and arr3 and they are sorted in strictly increasing order, we have to return a sorted array of only the integers that appeared in all of these three arrays. So if arrays are [1, 2, 3, 4, 5], [1, 2, 5, 7, 9], and [1, 3, 4, 5, 8], so the output will be [1, 5]To solve this, we will follow these steps −define an array called rescreate three maps f1, f2 and f3for i in range 0 to length of arr1f1[arr1[i]] increase by 1for i in range 0 to length of ... Read More