0% found this document useful (0 votes)
100 views

JAVA Advanced

The document describes a robot moving through a grid from start point (0,0) to destination (M-1,N-1). It can only move right or down. The problem is to calculate the total number of ways the robot can reach the destination. It then provides a sample input of grid size 5x5 and output of number of ways as 70.

Uploaded by

Lucky Mahanto
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

JAVA Advanced

The document describes a robot moving through a grid from start point (0,0) to destination (M-1,N-1). It can only move right or down. The problem is to calculate the total number of ways the robot can reach the destination. It then provides a sample input of grid size 5x5 and output of number of ways as 70.

Uploaded by

Lucky Mahanto
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Question 1

Maneuvering a Cave:

A Robot wants to move through a cave grid of size MxN . (M- Rows N- Columns).It
starts from (0,0) and destination is (M-1,N-1). It can only move right or down .
Calculate the total number of ways robot can reach the destination

Sample Input 1: Sample Output 1:


55 70
1 import java.util.Scanner;
2 public class Main
3 {
4 public static int totalWays(int cr, int cc, int dr, int dc)
5 {
6 if(cr > dr || cc > dc)
7 {
8 return 0;
9 }
10 if(cr == dr && cc == dc)
11 {
12 return 1;
13 }
14 int ways;
15 ways = totalWays(cr, cc + 1, dr, dc);
16 ways += totalWays(cr + 1, cc, dr, dc);
17 return ways;
18 }
19 public static int totalWaysWrapper(int m, int n)
20 {
21 return totalWays(0, 0, m - 1, n - 1);
22 }
1 public static void main(String[] args)
2 {
3 Scanner s=new Scanner (System.in);
4 int m, n;
5 m = s.nextInt();
6 n = s.nextInt();
7 System.out.print(totalWaysWrapper(m, n));
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
Question 2
Write the Java program to print the number of characters in the longest common
sub-sequence of two strings.

Sample Input 1: Sample Output 1:


abc 2
abd
Question 2
Sample Input 2: Sample Output 2:
abc 0
cab
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String args[])
5 {
6 Scanner sc = new Scanner(System.in);
7 String str1 = sc.nextLine();
8 String str2 = sc.nextLine();
9 char a[] = str1.toCharArray();
10 char b[] = str2.toCharArray();
11 int l1 = str1.length();
12 int l2 = str2.length();
13 int count = 0;
14 if(l1 == l2)
15 {
16 for (int i = 0; i < l1; i++)
17 {
18 if (a[i] == b[i])
19 {
20 count++;
21 }
22 }
1 System.out.println(count);
2 }
3 else
4 {
5 System.out.println("String length not equal");
6 }
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Question 3
Write a java program to print the minimum steps required to reach the end of the
given array.

Consider you are at the start of array i.e.)0th index position. Every index will hold
the value of maximum steps you take from that index in one jump(if the value is 0
then person cannot move).

You have to reach the end of the array n-1(n >1). Print the minimum number of steps
required to reach the endpoint. If it’s not possible to reach the end print -1.
Question 3
Input Constraint:
1 <= n <= 1000

1<= array element <= 50

Input Format:
First line contains the n

Second line contains n numbers separated using space.

Output Format:
Print the minimum number of steps or -1 if we cannot reach the end
Question 3
Sample Input: Sample Output:
5 2
24151

Explanation
From 0th index jump to 1st index and then jump to the 4th index.
1 import java.util.*;
2 public class MyClass {
3 public static void main(String args[]) {
4 Scanner sc= new Scanner(System.in);
5 int n =sc.nextInt();
6 int arr[]=new int[n];
7 for(int i=0;i<n;i++)
8 {
9 arr[i]=sc.nextInt();
10 }
11 int maxLevel = arr[0];
12 int step = arr[0];
13 int jump = 1;
14 if(arr[0] == 0)
15 {
16 jump=-1;
17 }
18
19
20
21
22
1 else
2 for(int i=1;i<n-1;i++)
3 {
4 maxLevel = Math.max(maxLevel,i+arr[i]);
5 step--;
6 if(step == 0)
7 {
8 jump++;
9 if(i>= maxLevel)
10 {
11 jump=-1;
12 break;
13 }
14 step=maxLevel-i;
15 }
16 }
17 System.out.print(jump);
18 }
19 }
20
21
22
Question 4
Perfect batch:

Write a java code to find if batches of size 3 are summing up to same value in the
given input.

Sample Input 1: Sample Output 1:


6 Perfect Batch
123501
Question 4
Sample Input 2: Sample Output 2:
6 Not a Perfect Batch
123456
1 import java.util.Scanner;
2 public class Main
3 {
4 public static void main(String[] args)
5 {
6 Scanner s=new Scanner(System.in);
7 int size;
8 int[] list = new int[10];
9 size = s.nextInt();
10 for(int i = 0; i < size; i++)
11 {
12 list[i] = s.nextInt();
13 }
14 largestElem(list, size);
15 }
16 public static void largestElem(int[] list, int size)
17 {
18 int batch1Sum = 0, batch2Sum = 0;
19 for(int i = 0; i < size / 2; i++)
20 {
21 batch1Sum = batch1Sum + list[i];
22 }
1 for(int i = size / 2; i < size; i++)
2 {
3 batch2Sum = batch2Sum + list[i];
4 }
5 if(batch1Sum == batch2Sum)
6 {
7 System.out.print("Perfect Batch");
8 } else
9 {
10 System.out.print("Not a Perfect Batch");
11 }
12 }
13 }
14
15
16
17
18
19
20
21
22
THANK YOU

You might also like