ADS Questions
ADS Questions
Q.1 :- Given an array of size ‘n’, the task is to find the length of the longest increasing
subsequence from the given array ‘arr’.
Note - A subsequence is a sequence that can be derived from another sequence by removing
zero or more elements, without changing the order of the given array.
Input :
5
13245
Output :
4
Explanation :
The either of the subsequences 1 2 4 5 or 1 3 4 5 are the longest and increasing subsequence.
Hence the length of the obtained subsequence is 4.
Solution :-
import java.util.Scanner;
Driver Code :-
import java.util.Scanner;
Test Cases :-
1. Input -
9
7 10 4 2 8 9 10 9 1
Output -
4
2. Input -
10
100 200 300 400 500 600 700 800 900 10
Output -
9
3. Input -
8
3 1 2 10 15 2 17 18
Output -
6
Q.2 :- Given a matrix of size m x n where ‘m’ denotes the row and ‘n’ denotes the column, your
job is to find the minimum cost path in the matrix traversal from top-left to bottom-right so that
the cost of traversing the path, keeping in mind that the traversal is only possible in rightward
and downward directions.
Input :
22
12
34
Output :
7
Explanation :
The minimum cost path is 1(start) 2(right) 4(down), hence the cost to traverse the path is 7.
Solution :-
import java.util.Scanner;
Driver Code :-
import java.util.Scanner;
Test Cases :-
1. Input -
23
112
223
Output -
7
2. Input -
33
314
416
5 9 10
Output -
21
3. Input -
25
10 9 8 7 6
15 14 13 12 11
Output -
51
Q.3 :- Given a positive integer K, the task is to find the minimum number of operations of the
following two types, required to change 0 to K:
Example
Input:
4
Output:
3
Explanation
1. 0 + 1 = 1
2. 1 * 2 = 2
3. 2 * 2 = 4
Solution :-
import java.util.Scanner;
Driver Code :-
import java.util.Scanner;
Test Cases :-
1. Input -
10
Output -
5
2. Input -
100
Output -
9
3. Input -
1500
Output -
17
Q.4 :- Given a n*n matrix where all numbers are distinct, find the maximum length path (starting
from any cell) such that all cells along the path are in increasing order with a difference of 1.
We can move in 4 directions from a given cell, i.e. Right, Down, Left and Up.
Example:
Input:
33
129
538
467
Output:
4
Solution :-
import java.util.Scanner;
Driver Code :-
import java.util.Scanner;
Test Cases :-
1. Input -
33
129
538
467
Output - 4
2. Input -
33
123
456
789
Output -
3
3. Input -
44
10 9 8 7
20 18 19 12
6543
15 16 17 18
Output -
3
Q.5 :- Two strings str1 and str2 are called isomorphic if there is a one-to-one mapping possible
for every character of str1 to every character of str2. And all occurrences of every character in
‘str1’ map to the same character in ‘str2’.
Examples:
Input: aab
xxy
Output: True
Explanation: ‘a’ is mapped to ‘x’ and ‘b’ is mapped to ‘y’.
Solution :-
import java.util.Arrays;
import java.util.Scanner;
Driver Code :-
import java.util.Arrays;
import java.util.Scanner;
Test Cases :-
1. Input - abc
xxy
Output - True
2. Input - aab
xyz
Output - False
3. Input - abc
xyz
Output - True
Q.6 : - Given a string str, and a pattern pat. The task is to check if the string and the pattern
match in the given format or not (without using regular expressions).
Example :-
Input:
DogCatDog
aba
Output:
true
Explanation:
The string “DogCatDog” matches the pattern of the scheme of “aba”, so the solution returns
“true”.
Solution :-
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Test {
public static boolean match(String str, int i, String pat, int j, Map<Character, String> map)
{
int n = str.length(), m = pat.length();
if (n < m) return false;
if (i == n && j == m) return true;
if (i == n || j == m) return false;
char curr = pat.charAt(j);
if (map.containsKey(curr))
{
String s = map.get(curr);
int k = s.length();
String ss;
if (i + k < str.length()) ss = str.substring(i, i + k);
else ss = str.substring(i);
if (ss.compareTo(s) != 0) return false;
return match(str, i + k, pat, j + 1, map);
}
for (int k = 1; k <= n - i; k++)
{
map.put(curr, str.substring(i, i + k));
if (match(str, i + k, pat, j + 1, map)) return true;
map.remove(curr);
}
return false;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.next();
String pat = sc.next();
Map<Character, String> map = new HashMap<>();
System.out.println(match(str, 0, pat, 0, map));
}
}
Driver Code :-
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Test {
public static boolean match(String str, int i, String pat, int j, Map<Character, String> map)
{
// WRITE YOUR CODE HERE…
}
Test Cases :-
1. Input :-
DogCatDog
aba
Output -
true
2. Input :-
ElephantTigerElephantTiger
abab
Output -
true
3. Input :-
AnimalsBirdsBirdsAnimal
abab
Output -
true
4. Input :-
WebSeriesSeries
aba
Output -
false
5. Input :-
ComputerScienceEngineering
abab
Output -
false