Skip to main content

Posts

Count A+b+c+ Subsequences

 1. You are given a string str. 2. You are required to calculate and print the count of subsequences of the nature a+b+c+. For abbc -> there are 3 subsequences. abc, abc, abbc For abcabc -> there are 7 subsequences. abc, abc, abbc, aabc, abcc, abc, abc. Input Format A string str Output Format count of subsequences of the nature a+b+c+ Constraints 0 < str.length <= 10 Sample Input abcabc Sample Output 7 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 counta = 0, countb = 0, countc = 0;         for(int i=0;i<str.length();i++){             char ch = str.charAt(i);             if(ch == 'a')                 ++counta;             else if(ch == 'b')                 ++countb;             else if(ch == 'c')                 ++countc;         }                  long total;         if(co

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

Unbounded 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. Note: Each item can be taken any number of times. You are allowed to put the same item again                    and again. Input Format A number n v1 v2 .. n number of elements w1 w2 .. n number of elements A number cap Output Format A number representing the maximum value that can be created in the bag without overflowing it's capacity Constraints 1 <= n <= 20 0 <= v1, v2, .. n elements <= 50 0 < w1, w2, .. n elements <= 10 0 < cap <= 10 Sample Input 5 15 14 10 45 30 2 5 1 3 4 7 Sample Output 100 Solution: import java.io.*; import java.util.*; pu

Zero One Knapsack | Recursion

 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. Note: Each item can be taken 0 or 1 number of times. You are not allowed to put the same item again and again. Input Format A number n v1 v2 .. n number of elements w1 w2 .. n number of elements A number cap Output Format A number representing the maximum value that can be created in the bag without overflowing it's capacity Constraints 1 <= n <= 20 0 <= v1, v2, .. n elements <= 50 0 < w1, w2, .. n elements <= 10 0 < cap <= 10 Sample Input 5 15 14 10 45 30 2 5 1 3 4 7 Sample Output 75 Solution: import java.io.*; import java.util.*; public class Main {

Subscribe to Get's Answer by Email