There are n stairs. One person will go to 1st to nth stairs. Maximum how many stairs he/she can cross in one step is also given. With this information, we have to find possible ways to go to the nth stairs. Let us consider one can cross a maximum two stairs in each step. So we can find recursive relations to solve this problem. One can move to nth stair, either from (n-1)th stair or from (n-2)th stair. So ways(n) = ways(n-1) + ways(n-2).Suppose the number of stairs, say 10, the maximum number of stairs that can be jumped in ... Read More
Suppose we have a number x, and x is a non-negative number. We have to find the square root of x without using any library functions. So we have to create our own function to evaluate sqrt(x). In this function, the decimal digit of the output will be truncated.Suppose the value of x is 4, then the result will be 2 if the x is 8, then the result will be also 2, as sqrt(8) is 2.82842. But we will take only the integer part.To solve this, follow these steps −initialize l = 1, and h = x + 1, ... Read More
Suppose we have an array of integers, say A. A will hold n elements, and they are non-negative. The whole array A is representing one large number. So if A = [5, 3, 2, 4] is given, it indicates the number 5324. We have to take that array A, then increase the number by 1, and again return the number like an array as given. So after increasing A will be [5, 3, 2, 5]To solve this, we will follow these steps.Take the array and append each character into a string to make it stringthen convert the string to an ... Read More
Suppose we have an integer array A. We have to find the contiguous subarrays which length will be at least one, and that has the largest sum, and also return its sum. So if the array A is like A = [-2, 1, -3, 4, -1, 2, 1, -5, 4], then the sum will be 6. And the subarray will be [4, -1, 2, 1]To solve this we will try to use the Dynamic programming approach.define an array dp same as the size of A, and fill it with 0dp[0] := A[0]for i = 1 to the size of A ... Read More
Here we will see the Count and Say sequence. This is a sequence whose few terms are like below −111211211111221The string will be read like1 (One)11 (One 1) So read the previous 1, and say “One 1”21 (Two 1) So read the previous 11, and say “Two 1”1211 (One 2 one 1) So read the previous 21, and say “One 2 one 1”111221 (One 1 one 2 two 1) So read the previous 1211, and say “One 1 one 2 two 1”Suppose we have a number n, 1
Suppose we have two strings str and sub_str. We have to find the first occurrence of sub_str in the str. So if the string str is “helloworld”, and substring is “lo”, then the result will be 3.This can be done using the strstr() function in C. We have to design another function that is similar to the strstr() in C.To solve this, follow these steps −i := 0, j := 0, m := length of sub_str and n := length of strif m = 0, then return 0while i < n and n – i + 1 = m, doif ... Read More
Suppose we have a sorted list A. We have to return the length of the array after removing all duplicate entries. We have to perform this in O(1) extra space. So we have to do the operation in-place.For an example, suppose A = [1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 5, 5, 5, 6] Then the output will be 6, as there are six distinct elements.To solve this, follow these steps −If the list is empty, return 0otherwise, initially take prev = first element of A. And define length = 0for i := 1 to n-1, doif ... Read More
Suppose we have two sorted lists A and B. We have to merge them and form only one sorted list C. The size of lists may different.For an example, suppose A = [1,2,4,7] and B = [1,3,4,5,6,8], then merged list C will be [1,1,2,3,4,4,5,6,7,8]We will solve this using recursion. So the function will work like below −Suppose the lists A and B of function merge()if A is empty, then return B, if B is empty, then return Aif value in A
Suppose we have a set of strings in an array. We have to find the Longest Common Prefix amongst the string in the array. Here we will assume that all strings are lower case strings. And if there is no common prefix, then return “”.So if the array of a string is like ["school", "schedule", "Scotland"], then the Longest Common Prefix is “sc” as this is present in all of these string.To solve this, we will take the first string as curr, now take each string from the array and read them character by character, and check the characters between ... Read More
A Module is a combination of both code and data that has a name, declares dependencies on other modules, exports packages that contain the public types that can be accessible outside this module and specifies the services it uses or the service implementations it provides. All of these have specified in a module-info.java file, which is included in the root directory of a module.There are two types of "export" clause can be used in "module-info.java" file.1) export : By default, the type public of a module is no longer visible outside the module. To make the public types of a given package visible from ... Read More