Suppose we have an array nums of integers, we need to find the maximum possible sum of elements of the given array such that it is divisible by three. So if the input is like [3, 6, 5, 1, 8], then the output will be 18, as the subarray is [3, 6, 1, 8], and the sum is 18, that is divisible by 3.To solve this, we will follow these steps −n := size of nums arraycreate one 2d array dp of size (n + 1) x 3set dp[0, 0] := 0, dp[0, 1] := -inf, dp[0, 2] := inffor ... Read More
Suppose we have some lists of regions where the first region of each list includes all other regions in that list. basically, if a region X contains another region Y then, X is larger than Y. Also by definition a region X contains itself. So if we have two regions r1 and r2, we have to find the smallest region that contains both of them. So if we have r1, r2 and r3 such that r1 includes r3, it is guaranteed there is no r2 such that r2 includes r3. So if the input is like [["Earth", "North America", "South ... Read More
Suppose we have a non-negative integer n, and we have to find the encoded form of it. The encoding strategy will be as follows −NumberEncoded number0“”1“0”2“1”3”00”4”01”5”10”6”11”7”000”So if the number is 23, then result will be 1000, if the number is 54, then it will be 10111To solve this, we will follow these steps −Create one method called bin, this will take n and k, this method will act like belowres := empty stringwhile n > 0res := res + the digit of n mod 2n := n /2reverse the number reswhile x > length of resres := prepend 0 with ... Read More
Suppose we have the following details of a matrix with n columns and 2 rows −Matrix elements will be either 0 or 1Sum of elements of the 0-th(upper) row is given as upper.Sum of elements of the 1-st(lower) row is given as lower.Sum of elements in the i-th column(0-indexed) is colsum[i], where colsum is given as an integer array with length n.The task is to reconstruct the matrix with upper, lower and colsum. We have to find it as a 2D integer array. If there are more than one valid solution, any of them will be accepted. If there is ... Read More
Suppose we have an array of strings arr. The string s is a concatenation of a sub-sequence of arr which have unique characters. Find the maximum possible length of s. If the input is like [“cha”, “r”, “act”, “ers”], then the output will be 6, possible solutions are “chaers” and “acters”.To solve this, we will follow these steps −make one method called ok() and this will take strings s and t. This will act like belowmake one map xfor i in range 0 to size of sincrease x[s[i]] by 1if x[s[i]] > 1, then return falsefor i in range 0 ... Read More
Suppose we have 2 integers n and start. Our task is return any permutation p of (0, 1, 2....., 2^n -1) as follows −p[0] = startp[i] and p[i+1] differ by only one bit in their binary representation.p[0] and p[2^n -1] must also differ by only one bit in their binary representation.So if the input is like n = 2 and start = 3, then the returned array will be [3, 2, 0, 1], these are [11, 10, 00, 01]To solve this, we will follow these steps −ans is an arrayfor i in range 0 to 2^ninsert start XOR i XOR ... Read More
Suppose we have a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string will be balanced if each of its characters appears n/4 times where n is the length of the string. Find the minimum length of the substring that can be replaced with any other string of the same length to make the original string is balanced. So if s = “QQWE”, then output will be 1. This is because we can replace Q to R, so that “RQWE”, that is balanced.Return 0 if the string is already balanced.To solve this, we will follow ... Read More
Suppose we have an array of strings products and a string called searchWord. We want to design a module that suggests at most three product names from products list after each character of searchWord is typed. The suggested products should have common prefix with the searchWord. When there are more than three products with a common prefix return the three lexicographically minimums products. So we have to find the lists of the suggested products after each character of searchWord is typed.If the input is like: [ "mobile", "mouse", "moneypot", "monitor", "mousepad"], and the searchWord is “mouse”, then the output will ... Read More
Suppose we have a binary tree. The rules of that tree is as follows −root.val == 0If treeNode.val is x and treeNode.left is not null, then treeNode.left.val = 2 * x + 1If treeNode.val is x and treeNode.right is not null, then treeNode.right.val = 2 * x + 2Now as the binary tree is contaminated. This indicates all val of the tree node have been changed to -1. We have to first recover the binary tree and then implement the FindElements class as follows −FindElements(TreeNode* root) Initializes the object with a contamined binary tree, we have to recover it first.bool ... Read More
JShell is a REPL (Read-Evaluate-Print-Loop) tool used to execute simple statements, evaluates it, and displays the result without a main() method. We can start it by simply type "jshell" in command-line prompt.We need to get the system properties by using System.getProperty() and System.getProperties() methods.In the below code snippet, we can able to display the system properties in the JShell tool by using static method property() of System class.Snippet-1jshell> System.getProperty("java.class.path") $1 ==> "C:\Program Files\Java\jdk-9.0.4\lib;C:\json-jars\json.jar;.;C:\json-jars\json-simple.jar;.;C:\json-jars\gson.jar;.;C:\json-jars\commons-io.jar;.;C:\json-jars\jackson-core.jar;.;C:\json-jars\jackson-databind.jar;.;C:\json-jars\jackson-annotations.jar;.;C:\json jars\flexjson.jar;.;C:\json-jars\jackson-dataformat-xml.jar;.;C:\json-jars\stax2-api.jar;.;C:\json-jars\jackson-dataformat-csv.jar;.;C:\json-jars\javax.json.jar;.;C:\json jars\javax.json-api.jar;.;C:\json-jars\jackson-module-jsonSchema.jar;.;C:\json-jars\json-lib.jar;.;C:\json-jars\commons-lang.jar;.;C:\json-jars\commons-logging.jar;.;"In the below code snippet, we have to use the “properties” object that extends Hashtable. So all properties can be listed as key/value pairs in the JShell tool by using ... Read More
 Data Structure
 Networking
 RDBMS
 Operating System
 Java
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP