Suppose we have a string S; we have to find the lexicographically largest palindromic subsequence of that string.So, if the input is like "tutorialspointtutorial", then the output will be "uu"To solve this, we will follow these steps −ans := blank stringmax_val := s[0]for i in range 1 to size of s, domax_val := maximum of max_val, s[i]for i in range 0 to size of s, doif s[i] is same as max_val, thenans := ans + s[i]return ansExample Let us see the following implementation to get better understanding − Live Demodef largest_palindromic_substr(s): ans = "" max_val = s[0] for i ... Read More
Suppose we have a list in Scala, this list is defined under scala.collection.immutable package. As we know, a list is a collection of same type elements which contains immutable (cannot be changed) data. We generally apply last function to show last element of a list.Using last keywordThe following Scala code is showing how to print the last element stored in a list of Scala.Example import scala.collection.immutable._ object HelloWorld { def main(args: Array[String]) { val temp_list: List[String] = List("Hello", "World", "SCALA", "is", "awesome") println("Elements of temp_list: " + temp_list.last) } }Output$scala HelloWorld Elements of ... Read More
Suppose we have a binary matrix, we have to find the largest rectangle of all 1's in that given matrix. The rectangle can be built by swapping or exchanging any pair of columns of that matrix.So, if the input is like100101001111010then the output will be the 6 in this case. The rectangle can be generating by exchanging column 1 with 3. The matrix after exchanging will be −001100011110110To solve this, we will follow these steps −row := size of matcol := size of mat[0]temp := a matrix of order (row + 1) x (col + 1), and fill with 0for ... Read More
Suppose we have a given Binary Tree; we have to find the size of largest Perfect sub-tree in that given Binary Tree. As we know the perfect binary tree is a binary tree in which all internal nodes have two children and all leaves are at the identical level.So, if the input is likethen the output will be 3, and the subtree isTo solve this, we will follow these steps −Define one block called RetType, this will hold isPerfect, height and rootTree, they are all initially 0Define a function called get_prefect_subtree(), this takes rootr_type := a new RetTypeif root is ... Read More
Suppose we have an array of different digits; we have to find the largest multiple of 3 that can be generated by concatenating some of the given digits in that array in any order. The answer may be very large so make it as string. If there is no answer return an empty string.So, if the input is like [7, 2, 8], then the output will be 87.To solve this, we will follow these steps −Define one 2D array d, there will be three rowssort the array digitssum := 0for initialize i := 0, when i < size of digits, ... Read More
Suppose we have a 2D matrix mat and a value K, we have to find the longest rectangular submatrix whose sum is same as K.So, if the input is like28-56-778-311-1443-43110And K = 9then the output will be Top-Left point is (1, 0) and Bottom-Right point is (3, 2).-77811-144-431To solve this, we will follow these steps −MAX := 100Define a function sum_k(), this will take one array arr, start, end, n, k, Define one mapsum := 0, maximum_length := 0for initialize i := 0, when i < n, update (increase i by 1), do −sum := sum + arr[i]if sum is ... Read More
We have an array like this −const arr = [{ year: 2020, month: 'January' }, { year: 2017, month: 'March' }, { year: 2010, month: 'January' }, { year: 2010, month: 'December' }, { year: 2020, month: 'April' }, { year: 2017, month: 'August' }, { year: 2010, month: 'February' }, { year: 2020, month: 'October' }, { year: 2017, month: 'June' }]We have to sort this array according to years in ascending order (increasing order). Moreover, if there exist two objects ... Read More
We are required to write a function that accepts an array of string literals and returns the index of the longest string in the array. While calculating the length of strings we don’t have to consider the length occupied by whitespacesIf two or more strings have the same longest length, we have to return the index of the first string that does so.We will iterate over the array, split each element by whitespace, join again and calculate the length, after this we will store this in an object. And when we encounter length greater than currently stored in the object, ... Read More
We have an array that contains some number literals and some string literals mixed, and we are required to write a sorting function that separates the two and the two types should also be sorted within.The code for this sorting function will be −Exampleconst arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { const first = typeof a === 'number'; const second = typeof b === 'number'; if(first && second){ return a - b; }else if(first && !second){ return ... Read More
Let’s say, we have an object that describes various qualities of a football player like this −const qualities = { defence: 82, attack: 92, heading: 91, pace: 96, dribbling: 88, tenacity: 97, vision: 91, passing: 95, shooting: 90 };We wish to write a function that takes in such object and a number n (n { const requiredObj = {}; if(num > Object.keys(obj).length){ return false; }; Object.keys(obj).sort((a, b) => obj[b] - obj[a]).forEach((key, ind) => { if(ind < num){ requiredObj[key] = obj[key]; } }); return requiredObj; }; console.log(pickHighest(qualities, 3));OutputThe output in the console will be −{ tenacity: 97, pace: 96, passing: 95 } { tenacity: 97 } { tenacity: 97, pace: 96, passing: 95, attack: 92, heading: 91 }