Skip to main content

Posts

Showing posts with the label LeetCode

Equal Beauty CodeChef SnackDown 2021 Round 1A

 Equal Beauty CodeChef SnackDown 2021 Round 1A Question The beauty of an (non-empty) array of integers is defined as the difference between its largest and smallest element. For example, the beauty of the array [2,3,4,4,6] is 6−2=4. An array A is said to be good if it is possible to partition the elements of A into two non-empty arrays B1 and B2 such that B1 and B2 have the same beauty. Each element of array A should be in exactly one array: either in B1 or in B2. For example, the array [6,2,4,4,4] is good because its elements can be partitioned into two arrays B1=[6,4,4] and B2=[2,4], where both B1 and B2 have the same beauty (6−4=4−2=2). You are given an array A of length N. In one move you can: Select an index i (1≤i≤N) and either increase Ai by 1 or decrease Ai by 1. Find the minimum number of moves required to make the array A good. Input Format The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follow. Each ...

Reverse Words in a String LeetCode Solution

 Reverse Words in a String LeetCode Solution Question Given an input string  s , reverse the order of the  words . A  word  is defined as a sequence of non-space characters. The  words  in  s  will be separated by at least one space. Return  a string of the words in reverse order concatenated by a single space. Note  that  s  may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.   Example 1: Input: s = "the sky is blue" Output: "blue is sky the" Example 2: Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: s = "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string. Example 4: Input: s = " ...

Next Greater Element I LeetCode Solution

 Next Greater Element I LeetCode Solution Question The  next greater element  of some element  x  in an array is the  first greater  element that is  to the right  of  x  in the same array. You are given two  distinct 0-indexed  integer arrays  nums1  and  nums2 , where  nums1  is a subset of  nums2 . For each  0 <= i < nums1.length , find the index  j  such that  nums1[i] == nums2[j]  and determine the  next greater element  of  nums2[j]  in  nums2 . If there is no next greater element, then the answer for this query is  -1 . Return  an array  ans  of length  nums1.length  such that  ans[i]  is the  next greater element  as described above.   Example 1: Input: nums1 = [4,1,2], nums2 = [1,3,4,2] Output: [-1,3,-1] Explanation: The next greater element for each value of nums1 is as f...

Path Sum III LeetCode Solution

 Path Sum III LeetCode Solution Question Given the  root  of a binary tree and an integer  targetSum , return  the number of paths where the sum of the values along the path equals   targetSum . The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).   Example 1: Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8 Output: 3 Explanation: The paths that sum to 8 are shown. Example 2: Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 Output: 3   Constraints: The number of nodes in the tree is in the range  [0, 1000] . -10 9  <= Node.val <= 10 9 -1000 <= targetSum <= 1000 Explanation This is a good question to test your logical skills. This question is categorized as medium on LeetCode. Lets know how to solve it by the detailed explanation given here. We can do it by recursion only but it is to much time...

Best Time to Buy and Sell Stock III LeetCode Solution

 Best Time to Buy and Sell Stock III LeetCode Solution Question You are given an array  prices  where  prices[i]  is the price of a given stock on the  i th  day. Find the maximum profit you can achieve. You may complete  at most two transactions . Note:  You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).   Example 1: Input: prices = [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: prices = [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: prices = [7,6,4,3,1] Output: 0 Ex...