0% found this document useful (0 votes)
9 views14 pages

ADS Questions

The document contains a series of programming problems related to algorithms and data structures, each with a description, input/output examples, and solutions in Java. Problems include finding the longest increasing subsequence, minimum cost path in a matrix, minimum operations to reach a number, longest increasing path in a matrix, checking if two strings are isomorphic, and matching a string with a pattern. Each problem is accompanied by driver code and test cases for validation.

Uploaded by

divyaamsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

ADS Questions

The document contains a series of programming problems related to algorithms and data structures, each with a description, input/output examples, and solutions in Java. Problems include finding the longest increasing subsequence, minimum cost path in a matrix, minimum operations to reach a number, longest increasing path in a matrix, checking if two strings are isomorphic, and matching a string with a pattern. Each problem is accompanied by driver code and test cases for validation.

Uploaded by

divyaamsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

NOTE :-

1. Q.1 & Q.2 are of 10 marks each.


2. Q.3, Q.4, Q.5 & Q.6 are of 5 marks each.
3. All the questions are compulsory.
4. The total marks scored will be out of 40.

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;

public class Main {


public static int lis(int n, int[] arr) {
int[] memo = new int[n];
memo[0] = 1;
for(int i=1; i < n; i++) {
memo[i] = 1;
for(int j=i-1; j >=0; j--) {
if(arr[j] >= arr[i])
continue;
int pA = memo[j] + 1;
if(pA > memo[i]) {
memo[i] = pA;
}
}
}
int max = 0;
for(int i=0; i < n; i++) {
if(max < memo[i])
max = memo[i];
}
return max;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i < n; i++)
arr[i] = sc.nextInt();
int ans = lis(n, arr);
System.out.println(ans);
}
}

Driver Code :-

import java.util.Scanner;

public class LongestIncreasingSubsequence {


public static int lis(int n, int[] arr) {
// WRITE YOUR CODE HERE…
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0; i < n; i++)
arr[i] = sc.nextInt();
int ans = lis(n, arr);
System.out.println(ans);
}
}

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;

public class MinCostPath {


if(m == 0 && n == 0) return cost[m][n];
if(m < 0 || n < 0) return Integer.MAX_VALUE;
if(memo[m][n] != -1) return memo[m][n];
memo[m][n] = Integer.min(minCostPath(m, n-1, cost, memo), minCostPath(m-1, n, cost,
memo)) + cost[m][n];
return memo[m][n];
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[][] cost = new int[m][n];
for(int i=0; i < m; i++) {
for(int j=0; j < n; j++) {
cost[i][j] =sc.nextInt();
}
}

int[][] memo = new int[m][n];


for(int i=0; i < m; i++) {
for(int j=0; j < n; j++) {
memo[i][j] = -1;
}
}
int ans = minCost(m-1, n-1, cost, memo);
System.out.println(ans);
}
}

Driver Code :-

import java.util.Scanner;

public class MinCostPath {


// WRITE YOUR CODE HERE…
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[][] cost = new int[m][n];
for(int i=0; i < m; i++) {
for(int j=0; j < n; j++) {
cost[i][j] =sc.nextInt();
}
}

int[][] memo = new int[m][n];


for(int i=0; i < m; i++) {
for(int j=0; j < n; j++) {
memo[i][j] = -1;
}
}
int ans = minCost(m-1, n-1, cost, memo);
System.out.println(ans);
}
}

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:

● Add one to the operand


● Multiply the operand by 2.

Example
Input:
4
Output:
3

Explanation
1. 0 + 1 = 1
2. 1 * 2 = 2
3. 2 * 2 = 4

Solution :-

import java.util.Scanner;

public class Main {


public static int minOperations(int k) {
int dp[] = new int[k + 1];
for(int i = 1; i <= k; i++) {
dp[i] = dp[i - 1] + 1;
if (i % 2 == 0) {
dp[i] = Math.min(dp[i], dp[i / 2] + 1);
}
}
return dp[k];
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int min_operations = minOperations(k);
System.out.println(min_operations);
}
}

Driver Code :-

import java.util.Scanner;

public class Main {


public static int minOperations(int k) {
// WRITE YOUR CODE HERE…
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int min_operations = minOperations(k);
System.out.println(min_operations);
}
}

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;

public class Main {


public static int n = 3;
public static int findLongestFromACell(int i, int j, int[][] mat, int[][] dp) {
if (i < 0 || i >= n || j < 0 || j >= n)
return 0;
if (dp[i][j] != -1) return dp[i][j];
int x = Integer.MIN_VALUE, y = Integer.MIN_VALUE, z = Integer.MIN_VALUE, w =
Integer.MIN_VALUE;
if (j < n - 1 && ((mat[i][j] + 1) == mat[i][j + 1])) x = dp[i][j] = 1 + findLongestFromACell(i, j + 1,
mat, dp);
if (j > 0 && (mat[i][j] + 1 == mat[i][j - 1])) y = dp[i][j] = 1 + findLongestFromACell(i, j - 1, mat,
dp);
if (i > 0 && (mat[i][j] + 1 == mat[i - 1][j])) z = dp[i][j] = 1 + findLongestFromACell(i - 1, j, mat,
dp);
if (i < n - 1 && (mat[i][j] + 1 == mat[i + 1][j])) w = dp[i][j] = 1 + findLongestFromACell(i + 1, j,
mat, dp);
return dp[i][j] = Math.max(x, Math.max(y, Math.max(z, Math.max(w, 1))));
}

public static int finLongest(int[][] mat) {


int result = 1;
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dp[i][j] = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (dp[i][j] == -1) findLongestFromACell(i, j, mat, dp);
result = Math.max(result, dp[i][j]);
}
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println(finLongest(matrix));
}
}

Driver Code :-

import java.util.Scanner;

public class Main {


public static int n = 3;
public static int findLongestFromACell(int i, int j, int[][] mat, int[][] dp) {
// WRITE YOUR CODE HERE…
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println(finLongest(matrix));
}
}

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;

public class Test {


static int size = 256;
static String areIsomorphic(String str1, String str2)
{
int m = str1.length();
int n = str2.length();
if (m != n)
return "False";
Boolean[] marked = new Boolean[size];
Arrays.fill(marked, Boolean.FALSE);
int[] map = new int[size];
Arrays.fill(map, -1);

// Process all characters one by one


for (int i = 0; i < n; i++) {
if (map[str1.charAt(i)] == -1) {
if (marked[str2.charAt(i)] == true)
return "False";
marked[str2.charAt(i)] = true;
map[str1.charAt(i)] = str2.charAt(i);
}
else if (map[str1.charAt(i)] != str2.charAt(i))
return "False";
}
return "True";
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
String ans = areIsomorphic(str1, str2);
System.out.println(ans);
}
}

Driver Code :-

import java.util.Arrays;
import java.util.Scanner;

public class Test {


static int size = 256;
public static String areIsomorphic(String str1, String str2)
{
// WRITE YOUR CODE HERE…
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
String str1 = sc.next();
String str2 = sc.next();
String ans = areIsomorphic(str1, str2);
System.out.println(ans);
}
}

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…
}

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));
}
}

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

You might also like