Skip to main content

Posts

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 test
Recent posts

Round Robin Ranks CodeChef SnackDown 2021 Round 1A

 Round Robin Ranks CodeChef SnackDown 2021 Round 1A Question A round-robin tournament is being held in Chefland among N teams numbered 1,2,...,N. Every team play with all other teams exactly once. All games have only two possible results - win or loss. A win yields 2 points to the winning team while a loss yields no points. What is the maximum number of points a team finishing at the Kth position can score? Note: If two teams have the same points then the team with the higher team number achieves the better rank. Input Format First line will contain T, number of testcases. Then the testcases follow. Each testcase contains a single line of input, two space-separated integers N,K. Output Format For each testcase, output in a single line an integer - the maximum points the team ranked K in the round-robin tournament can score. Constraints 1≤T≤10^5 1≤K≤N≤10^9 Sample Input 1  3 3 3 4 1 7 4 Sample Output 1  2 6 8 Explanation Test Case 1: There are 3 teams in the tournament. The maximum score

Min Max LCM CodeChef SnackDown 2021 Round 1A Solution

 Min Max LCM CodeChef SnackDown 2021 Round 1A Solution Question You are given two positive integers X and K. You have to output the minimum and maximum value of LCM(i,j) where X≤i<j≤X⋅K. We define LCM(i,j) for two positive integers i and j as the minimum positive integer y such that both i and j divide y without remainder. Input Format First line will contain T, number of testcases. Then the testcases follow. Each testcase contains of a single line of input, two space separated integers X and K. Output Format For each testcase, output two space separated integers - the minimum and maximum possible value respectively of LCM(i,j) where X≤i<j≤X⋅K. Constraints 1≤T≤10^5 1≤X≤10^8 2≤K≤10^8 It is guaranteed that, for each test case, X⋅K≤10^9 Sample Input 1  2 4 3 2 3 Sample Output 1  8 132 4 30 Explanation Test Case 1: We want to find the minimum and maximum value of LCM(i,j) for 4≤i<j≤12. It is easy to verify that the LCM(4,8)=8 is the minimum possible value whereas LCM(11,12)=132 is

Dance Moves CodeChef SnackDown 2021 Round 1A Solution

 Dance Moves CodeChef SnackDown 2021 Round 1A Solution Question This year Chef is participating in a Dancing competition. The dance performance will be done on a linear stage marked with integral positions. Initially, Chef is present at position X and Chef's dance partner is at position Y. Chef can perform two kinds of dance moves. If Chef is currently at position k, Chef can: Moonwalk to position k+2, or Slide to position k−1 Chef wants to find the minimum number of moves required to reach his partner. Can you help him find this number? Input Format First line will contain a single integer T, the number of testcases. Then the description of T testcases follows. Each testcase contains a single line with two space-separated integers X,Y, representing the initial positions of Chef and his dance partner, respectively. Output Format For each testcase, print in a separate line, a single integer, the minimum number of moves required by Chef to reach his dance partner. Constraints 1≤T≤10^

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 = " Bob

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 follows: - 4 is underlined in nums2 = [1,3, 4 ,2]. There is no next greater element, so the answer is -1. - 1 is underlined in nums2 = [ 1 ,3,4,2]. The next greater element is 3. - 2 is underlined in

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 consuming so to reduce the time complexity we are sto