Skip to main content

Posts

Showing posts with the label AA Question

Count Encodings

 1. You are given a string str of digits. (will never start with a 0) 2. You are required to encode the str as per following rules     1 -> a     2 -> b     3 -> c     ..     25 -> y     26 -> z 3. You are required to calculate and print the count of encodings for the string str.      For 123 -> there are 3 encodings. abc, aw, lc      For 993 -> there is 1 encoding. iic       For 013 -> This is an invalid input. A string starting with 0 will not be passed.      For 103 -> there is 1 encoding. jc      For 303 -> there are 0 encodings. But such a string maybe passed. In this case       print 0. Input Format A string str Output Format count of encodings Constraints 0 < str.length <= 10 Sample Input 123 Sample Output 3 Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         Scanner sc = new Scanner(System.in);         String str = sc.nextLine();                  int count =

Arrange Buildings

1. You are given a number n, which represents the length of a road. The road has n plots on it's each side. 2. The road is to be so planned that there should not be consecutive buildings on either side of the road. 3. You are required to find and print the number of ways in which the buildings can be built on both side of roads. Input Format A number n Output Format A number representing the number of ways in which the buildings can be built on both side of roads. Constraints 0 < n <= 45 Sample Input 6 Sample Output 441 Solution: import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception {     // write your code here     Scanner scn = new Scanner(System.in);     long n = scn.nextInt();     long ob = 1;     long os = 1;     for (int i = 2; i <= n; i++) {       long nb = os;       long ns = os + ob;       ob = nb;       os = ns;     }     long total = ob + os;     System.out.println(total * total);  } }

Count Binary Strings

1. You are given a number n. 2. You are required to print the number of binary strings of length n with no consecutive 0's. Note: In this problem, you are given a number n. All we need to print is the number of binary strings of length n with no consecutive 0's For example: Sample Input: 3 Sample Output: 5 How 5? We have a total of eight binary numbers for length 3, out of which we have 5 numbers in which there are no consecutive zeros. Input Format A number n Output Format A number representing the number of binary strings of length n with no consecutive 0's. Constraints 0 < n <= 45 Sample Input 6 Sample Output 21 Solution: import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception {     // write your code here     Scanner sc = new Scanner(System.in);     int n = sc.nextInt();          int dp[][] = new int[n+1][2];     dp[1][0] = 1;     dp[1][1] = 1;          for(int i=2;i<=n;i++){         dp[i][0] = dp[

Fractional Knapsack

1. You are given a number n, representing the count of items. 2. You are given n numbers, representing the values of n items. 3. You are given n numbers, representing the weights of n items. 3. You are given a number "cap", which is the capacity of a bag you've. 4. You are required to calculate and print the maximum value that can be created in the bag without overflowing it's capacity. Note1: Items can be added to the bag even partially. But you are not allowed to put same items again and again to the bag. Input Format A number n v1 v2 .. n number of elements w1 w2 .. n number of elements A number cap Output Format A decimal number representing the maximum value that can be created in the bag without overflowing it's capacity 1. You are given a number n, representing the count of items. 2. You are given n numbers, representing the values of n items. 3. You are given n numbers, representing the weights of n items. 3. You are given a number "cap", which i

Target Sum Subsets - Dp

1. You are given a number n, representing the count of elements. 2. You are given n numbers. 3. You are given a number "tar". 4. You are required to calculate and print true or false, if there is a subset the elements of which add       up to "tar" or not. Input Format A number n n1 n2 .. n number of elements A number tar Output Format true or false as required Constraints 1 <= n <= 30 0 <= n1, n2, .. n elements <= 20 0 <= tar <= 50 Sample Input 5 4 2 7 1 3 10 Sample Output true Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         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 tar = sc.nextInt();                  boolean[][] dp = new boolean[n+1][tar+1];         for(int j=0;j<=n;j++)             dp[j][0] = true;                  for(

Goldmine

1. You are given a number n, representing the number of rows. 2. You are given a number m, representing the number of columns. 3. You are given n*m numbers, representing elements of 2d array a, which represents a gold mine. 4. You are standing in front of left wall and are supposed to dig to the right wall. You can start from       any row in the left wall. 5. You are allowed to move 1 cell right-up (d1), 1 cell right (d2) or 1 cell right-down(d3). 6. Each cell has a value that is the amount of gold available in the cell. 7. You are required to identify the maximum amount of gold that can be dug out from the mine. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format An integer representing Maximum gold available. Constraints 1 <= n <= 10^2 1 <= m <= 10^2 0 <= e1, e2, .. n * m elements <= 1000 Sample Input 6 6 0 1 4 2 8 2 4 3 6 5 0 4 1 2 4 1 4 6 2 0 7 3 2 2 3 1 5 9 2 4 2 7 0 8 5 1 Sample Output 33 Solution: import java.io.*;

Min Cost In Maze Traversal

1. You are given a number n, representing the number of rows. 2. You are given a number m, representing the number of columns. 3. You are given n*m numbers, representing elements of 2d array a, which represents a maze. 4. You are standing in top-left cell and are required to move to bottom-right cell. 5. You are allowed to move 1 cell right (h move) or 1 cell down (v move) in 1 motion. 6. Each cell has a value that will have to be paid to enter that cell (even for the top-left and bottom-       right cell). 7. You are required to traverse through the matrix and print the cost of path which is least costly. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format The cost of least costly path. Constraints 1 <= n <= 10^2 1 <= m <= 10^2 0 <= e1, e2, .. n * m elements <= 1000 Sample Input 6 6 0 1 4 2 8 2 4 3 6 5 0 4 1 2 4 1 4 6 2 0 7 3 2 2 3 1 5 9 2 4 2 7 0 8 5 1 Sample Output 23 Solution: import java.io.*; import java.util.*; publi

Subscribe to Get's Answer by Email