In this problem, we are given a string N that represents a number. Our task is to find the largest number smaller than N with the same set of digits. Problem Description: we need to find a number using all the digits of the given number which is the largest smaller number than N.Let’s take an example to understand the problem, Input: N = “54314”Output: 54341Solution ApproachA simple solution to the problem is by finding the digit of the number which can be moved to find the largest smaller number. Now, the number should be greater than its right success in order to solve ... Read More
In this problem, we are given two numbers x and y. Our task is to find larger of x^y and y^x. Problem Description: The problem is simple, we need to find weather x to the power y is greater than y to the power x.Let’s take an example to understand the problem, Input: x = 4, y = 5Output: 1024Explanation: x^y = 4^5 = 1024y^x = 5^4 = 625Solution ApproachThe solution to the problem is simple. We need to find the value of x^y and y^x and return the maximum of both.There can be a more mathematically easy way to solve the problem, which is ... Read More
In this problem, we are given n ranges and an integer k. Our task is to find k-th smallest element in given n ranges. We need to find the kth smallest elements from the array which is created after combining the ranges.Let’s take an example to understand the problem, Input: ranges = {{2, 5}, {7, 9}, {12, 15}}, k = 9Output: 13Explanation: The array created is {2, 3, 4, 5, 7, 8, 9, 12, 13, 14, 15}The smallest elements is 13Solution Approach:A simple solution to the problem is by creating the array from all ranges and as it is created from range it is ... Read More
In this problem, we are given a linked-list and a number k. Our task is to find kth node from Middle towards the Head of a Linked List. Let’s take an example to understand the problem, Input: linked-list : 4 -> 2 -> 7 -> 1 -> 9 -> 12 -> 8 -> 10 -> 5, k = 2Output: 7Explanation: Middle node value is 9.The 2nd node from middle towards head is 7.Solution ApproachWe need to find kth element from the middle of the linked-list towards the beginning. For this we need to find the size of linked-list by traversing the linked-list from beginning ... Read More
In this problem, we are given two integer values n and k. Our task is to find kth smallest number in range [1, n] when all the odd numbers are deleted. We need to find the kth smallest number in the range [1, n] which contains only even values.So, from range [1, 5] -> number will be 2, 4.Let’s take an example to understand the problem, Input: n = 12, k = 4Output: 8Explanation: Even elements in the range [1, n] : 2, 4, 6, 8, 10, 12The 4th smallest element is 8.Solution approach: The solution is simple as we need to find the kth ... Read More
In this problem, we are given an array arr[] of n elements. Our task is to find k maximum elements of the array in original order. We need to find k maximum elements of the array and then print then as they were indexed originally.Let’s take an example to understand the problem, Input: arr[] = {5, 1, 3, 6, 2}, k = 2Output: 5, 6Explanation: The largest two elements of the array are 6 and 5. But 5 comes before 6 in the original array hence we have printed in that way.Solution ApproachTo solve the problem, and print k elements in the original order.For ... Read More
In this problem, we are given an array arr[] of n integers.Our task is to find integers that divides maximum number of elements of the array. Problem Description: We need to find a number p which can divide the maximum number of elements of the array. In case, there are more than one such element we will return the smaller one.Let’s take an example to understand the problem, Input: arr[] = {4, 5, 6, 7, 8}Output: 2Explanation: The element 2 divides {4, 6, 8}.Solution ApproachA simple solution to the problem is by looping through the array and then for each element of the array, divide each ... Read More
In this problem, we are given a list that consists of items and their values and an integer k. Our task is to find K items with the lowest values. Problem description: We need to find k items from the list that have the lowest value.Let’s take an example to understand the problem, Input: item-value = { {item1, 200}, {item2, 100}, {item3, 500}, {item4, 400}} k = 2Output: item1 , item2Explanation: Two elements with least value are item1 with 200 and item2 with 100.Solution ApproachA solution to the problem is by finding k items with the least value greedily. We will first sort the item ... Read More
We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should find that longest string which is sandwiched between two identical characters and return its length.For example −If the input string is −const str = 'sadtrsewak';Then the output should be −const output = 6;because between two 'a' we have the longest desired substring of length 6.ExampleFollowing is the code −const str = 'sadtrsewak'; const longestSubstringBetween = (str = '') => { const map = {}; let res = -1; for(let i = 0; i < str.length; i++){ ... Read More
We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should return true if all the characters present in the string are unique. And if even one character appears for more than one, the function should return false.We will use a hash set to keep track of the characters we encounter in the string and if at any stage of iteration, we encounter duplicate characters, we will return false otherwise at the end of iteration, we will return true.ExampleFollowing is the code −const str = 'abschyie'; const checkUniqueness ... Read More