0% found this document useful (0 votes)
7 views10 pages

21bf1a3708 - 03 - 06 - 2024

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

21bf1a3708 - 03 - 06 - 2024

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

1.

Longest Subsequence:

public class LCSDP {

public static int LCS(char a[],char b[],int i,int j){

int dp[][]=new int [i+1][j+1];

for(y=0;y<=j;y++){

if(x==0 || y==0)

dp[x][y]=0;

else if(a[x-1]==b[y-1]){

dp[x][y]=dp[x-1][y-1]+1;

else{

dp[x][y]=Math.max(dp[x-1][y],dp[x][y-1]);

int x,y;

for(x=0;x<=i;x++){}

return dp[i][j];

public static void main(String args[]){

String str1="bd";

String str2="abcd";

char a[]=str1.toCharArray();

char b[]=str2.toCharArray();

System.out.print(LCS(a,b,str1.length(),str2.length()));

Ouput:2

2.LONGEST INCREASING SUBSEQUENCE:(DYNAMIC PROGRAMMING)😊


import java.util.*;

class LCSDP {

static int lis(int arr[], int n) {

int lis[] = new int[n];

int i, j, max = 0;

// Initialize LIS values for all indexes

for (i = 0; i < n; i++)

lis[i] = 1;

// Compute optimized LIS values in a bottom-up manner

for (i = 1; i < n; i++) {

for (j = 0; j < i; j++) {

if (arr[i] > arr[j] && lis[i] < lis[j] + 1)

lis[i] = lis[j] + 1;

// Print LIS array values

System.out.println("LIS values: " + Arrays.toString(lis));

// Pick maximum of all LIS values

for (i = 0; i < n; i++) {

if (max < lis[i])

max = lis[i];

return max;

}
public static void main(String[] args) {

int arr[] = {10, 22, 9, 33, 21, 50, 41, 60};

int n = arr.length;

System.out.println("Length of LIS is " + lis(arr, n));

OUTPUT: Length of LIS is 5

RECURSION OF LCS

class LCs{

static int f(int ind, int prev_ind, int[] arr, int n, int[][] dp) {

if (ind == n)

return 0;

if (dp[ind][prev_ind + 1] != -1)

return dp[ind][prev_ind + 1];

int len = 0 + f(ind + 1, prev_ind, arr, n, dp);

if (prev_ind == -1 || arr[ind] > arr[prev_ind])

len = Math.max(len, 1 + f(ind + 1, ind, arr, n, dp));

return dp[ind][prev_ind + 1] = len;

public static void main(String[] args) {

System.out.println("LONGEST SUBSEQUENCE");

int n = 5;

int arr[] = {2, 8, 5, 9, 6};

int[][] dp = new int[n][n + 1];

for (int i = 0; i < n; i++) {

for (int j = 0; j < n + 1; j++) {


dp[i][j] = -1;

System.out.println(f(0, -1, arr, n, dp));

OUTPUT: LONGEST SUBSEQUENCE

3.STAIR CASE(DYNAMIC APPROAC)

import java.util.*;

public class Stair {

public static int ways(int n) {

if (n==0||n==1){

return 1;

else if(n==2){

return 2;

int way[]=new int[n+1];

way[0]=1;

way[1]=1;

way[2]=2;

for(int i=3;i<=n;i++){

way[i]=way[i-1]+way[i-2]+way[i-3];

return way[n];

public static void main(String [] args){

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

System.out.println("No.of ways"+ways(n));
}

OUTPUT:

No.of ways4

4.HOUSE ROBBERS

import java.util.*;

public class House{

public static int rob(int[] nums) {

int n = nums.length;

if (n == 0) {

return 0;

} else if (n == 1) {

return nums[0];

int[] dp = new int[n];

dp[0] = nums[0];

dp[1] = Math.max(nums[0], nums[1]);

for (int i = 2; i < n; i++) {

dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);

return dp[n - 1];

public static void main(String[] args) {

int[] nums = {6,9,6};

System.out.println(rob(nums));

}
}

OUTPUT:12

5.COIN CHANGE (RECURSIVE PROGRAM):

import java.util.*;

public class Coin {

public static int count(int n, int[] c, int sum) {

if (sum == 0) {

return 1;

if (sum < 0) {

return 0;

if (n <= 0 && sum > 0) {

return 0;

return count(n - 1, c, sum) + count(n, c, sum - c[n - 1]);

public static void main(String[] args) {

int[] nums = {2, 3,5,6};

int sum = 10;

System.out.println(count(nums.length, nums, sum));

OUTPUT:5

COIN CHANGE(DYNAMIC PROGRAM):

import java.util.*;

public class Main {

public static int count(int n, int[] c, int sum) {

int dp[] = new int[sum + 1];


dp[0] = 1;

for (int coin : c) {

for (int i = coin; i <= sum; i++) {

dp[i] += dp[i - coin];

return dp[sum];

public static void main(String[] args) {

int[] nums = {2, 3, 5, 6};

int sum = 10;

System.out.println(count(nums.length, nums, sum));

OUTPUT:5

6. KMP(Knuth-Morris-Pratt) Algorithm:

import java.util.*;

public class KMP {

public static void computeLPSArray(String pattern, int[] lps) {

int prevLPS = 0;

int i = 1;

lps[0] = 0;

while (i < pattern.length()) {

if (pattern.charAt(i) == pattern.charAt(prevLPS)) {

prevLPS++;

lps[i] = prevLPS;

i++;

} else {
if (prevLPS != 0) {

prevLPS = lps[prevLPS - 1];

} else {

lps[i] = 0;

i++;

public static void main(String[] args) {

String pattern = "AAACAAAAAC";

int[] lps = new int[pattern.length()];

computeLPSArray(pattern, lps);

System.out.println("LPS array: " + Arrays.toString(lps));

OUTPUT:

LPS array: [0, 1, 2, 0, 1, 2, 3, 3, 3, 4]

@BOTH SORTING AND INDEXXING PATTERN IN KMP

import java.util.*;

public class Main {

public static void computeLPSArray(String pattern, int[] lps) {

int prevLPS = 0;

int i = 1;

lps[0] = 0;

while (i < pattern.length()) {

if (pattern.charAt(i) == pattern.charAt(prevLPS)) {

prevLPS++;
lps[i] = prevLPS;

i++;

} else {

if (prevLPS != 0) {

prevLPS = lps[prevLPS - 1];

} else {

lps[i] = 0;

i++;

public static void kmp(String pattern, String text, int[] lps) {

int i = 0, j = 0;

while (i < text.length()) {

if (pattern.charAt(j) == text.charAt(i)) {

i++;

j++;

if (j == pattern.length()) {

System.out.println("Pattern found at index " + (i - j));

j = lps[j - 1];

} else if (i < text.length() && pattern.charAt(j) != text.charAt(i)) {

if (j != 0) {

j = lps[j - 1];

} else {

i++;

}
}

public static void main(String[] args) {

String pattern = "AAACAAAAAC";

String text = "AAACAAAAACAAACAAAAAC";

int[] lps = new int[pattern.length()];

computeLPSArray(pattern, lps);

kmp(pattern, text, lps);

System.out.println("LPS array: " + Arrays.toString(lps));

OUTPUT: Pattern found at index 10

LPS array: [0, 1, 2, 0, 1, 2, 3, 3, 3, 4]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy