Skip to main content

Posts

Showing posts from October, 2021

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')                 ++coun...

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 {   ...

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;   ...

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] ...

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: im...

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...

Coin Change Permutations

 1. You are given a number n, representing the count of coins. 2. You are given n numbers, representing the denominations of n coins. 3. You are given a number "amt". 4. You are required to calculate and print the number of permutations of the n coins using which the       amount "amt" can be paid. Note 1: You have an infinite supply of each coin denomination i.e. same coin denomination can be                    used for many installments in payment of "amt" Note 2: You are required to find the count of permutations and not combinations i.e.                   2 + 2 + 3 = 7 and 2 + 3 + 2 = 7 and 3 + 2 + 2 = 7 are different permutations of same                    combination. You should treat them as 3 and not 1. Input Format A number n n1 n2 .. n number of elements A number amt Output Format A number representing the cou...

Coin Change Combination

1. You are given a number n, representing the count of coins. 2. You are given n numbers, representing the denominations of n coins. 3. You are given a number "amt". 4. You are required to calculate and print the number of combinations of the n coins using which the       amount "amt" can be paid. Note 1: You have an infinite supply of each coin denomination i.e. same coin denomination can be                    used for many installments in payment of "amt" Note 2: You are required to find the count of combinations and not permutations i.e.                   2 + 2 + 3 = 7 and 2 + 3 + 2 = 7 and 3 + 2 + 2 = 7 are different permutations of same                    combination. You should treat them as 1 and not 3. Input Format A number n n1 n2 .. n number of elements A number amt Output Format A number representing the count of ...

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(); ...

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:...

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...

Climb Stairs With Minimum Moves

1. You are given a number n, representing the number of stairs in a staircase. 2. You are on the 0th step and are required to climb to the top. 3. You are given n numbers, where ith element's value represents - till how far from the step you       could jump to in a single move.  You can of-course fewer number of steps in the move. 4. You are required to print the number of minimum moves in which you can reach the top of       staircase. Note -> If there is no path through the staircase print null. Input Format A number n .. n more elements Output Format A number representing the number of ways to climb the stairs from 0 to top. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 20 Sample Input 10 3 3 0 2 1 2 4 2 0 0 Sample Output 4 Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         // write your code here    ...

Climb Stairs With Variable Jumps

 1. You are given a number n, representing the number of stairs in a staircase. 2. You are on the 0th step and are required to climb to the top. 3. You are given n numbers, where ith element's value represents - till how far from the step you       could jump to in a single move.        You can of course jump fewer number of steps in the move. 4. You are required to print the number of different paths via which you can climb to the top. Input Format A number n .. n more elements Output Format A number representing the number of ways to climb the stairs from 0 to top. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 20 Sample Input 10 3 3 0 2 1 2 4 2 0 0 Sample Output 5 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);   ...

Climb Stairs

1. You are given a number n, representing the number of stairs in a staircase. 2. You are on the 0th step and are required to climb to the top. 3. In one move, you are allowed to climb 1, 2 or 3 stairs. 4. You are required to print the number of different paths via which you can climb to the top. Input Format A number n Output Format A number representing the number of ways to climb the stairs from 0 to top. Constraints 0 <= n <= 20 Sample Input 4 Sample Output 7 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();         System.out.println(WaysUp(n));     }     public static int WaysUp(int n){         if(n == 0)  return 0;         else if(n<0)...

Fibonacci-dp

 1. You are given a number n. 2. You are required to print the nth element of fibonnaci sequence. Note -> Notice precisely how we have defined the fibonnaci sequence 0th element -> 0 1st element -> 1 2nd element -> 1 3rd element -> 2 4th element -> 3 5th element -> 5 6th element -> 8 Input Format A number n Output Format A number representing the nth element of fibonnaci sequence Constraints 0 <= n <= 45 Sample Input 10 Sample Output 55 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[] A = new int[n+1];     A[0] = 0;     A[1] = 1;     for(int i=2;i<=n;i++){         A[i] = A[i-1] + A[i-2];     }     System.out.println(A[n]);  } }

Selection Sort

 1. You are given an array(arr) of integers. 2. You have to sort the given array in increasing order using selection sort. Input Format An Integer n  arr1 arr2.. n integers Output Format Check the sample ouput and question video. Constraints 1 <= N <= 10000 -10^9 <= arr[i] <= 10^9 Sample Input 5 7  -2  4  1  3 Sample Output Comparing -2 and 7 Comparing 4 and -2 Comparing 1 and -2 Comparing 3 and -2 Swapping 7 and -2 Comparing 4 and 7 Comparing 1 and 4 Comparing 3 and 1 Swapping 7 and 1 Comparing 7 and 4 Comparing 3 and 4 Swapping 4 and 3 Comparing 4 and 7 Swapping 7 and 4 -2 1 3 4 7 Solution: import java.io.*; import java.util.*; public class Main {   public static void selectionSort(int[] arr) {     //write your code here     for(int i=0;i<arr.length-1;i++){         int idx = i;         for(int j=i+1;j<arr.length;j++)             if(isSmaller(ar...

Bubble Sort

1. You are given an array(arr) of integers. 2. You have to sort the given array in increasing order using bubble sort. Input Format An Integer n  arr1 arr2.. n integers Output Format Check the sample ouput and question video. Constraints 1 <= N <= 10000 -10^9 <= arr[i] <= 10^9 Sample Input 5 7  -2  4  1  3 Sample Output Comparing -2 and 7 Swapping -2 and 7 Comparing 4 and 7 Swapping 4 and 7 Comparing 1 and 7 Swapping 1 and 7 Comparing 3 and 7 Swapping 3 and 7 Comparing 4 and -2 Comparing 1 and 4 Swapping 1 and 4 Comparing 3 and 4 Swapping 3 and 4 Comparing 1 and -2 Comparing 3 and 1 Comparing 1 and -2 -2 1 3 4 7 Solution: import java.io.*; import java.util.*; public class Main {   public static void bubbleSort(int[] arr) {     //write your code here     for(int i=0;i<arr.length;i++){         for(int j=1;j<arr.length-i;j++)             if(isSmaller(arr,j,j-1)){   ...

Knights Tour

1. You are given a number n, the size of a chess board. 2. You are given a row and a column, as a starting point for a knight piece. 3. You are required to generate the all moves of a knight starting in (row, col) such that knight visits       all cells of the board exactly once. 4. Complete the body of printKnightsTour function - without changing signature - to calculate and       print all configurations of the chess board representing the route      of knight through the chess board. Use sample input and output to get more idea. Note -> When moving from (r, c) to the possible 8 options give first precedence to (r - 2, c + 1) and                 move in clockwise manner to                explore other options. Input Format A number n A number row A number col Output Format All configurations of the chess board representing route of knights thro...

Reverse a Number

  /* @ToDo     Reverse a Number    7325 => 5237    12345 => 54321 */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE          freopen ( "../asset/input.txt" , "r" , stdin );          freopen ( "../asset/output.txt" , "w" , stdout );     #endif     // Code here!!      int   n ;  cin >> n ;      int   num  =  0 ;      while ( n > 0 ){          num  = ( num * 10 ) + ( n % 10 );          n  /=  10 ;     }       cout << nu...

If a Prime Number

  /* @ToDo     If a Prime Number    9 => Not a Prime Number    7 => Prime Number */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE          freopen ( "../asset/input.txt" , "r" , stdin );          freopen ( "../asset/output.txt" , "w" , stdout );     #endif     // Code here!!     int   n ;  cin >> n ;     bool   flag  =  true ;     for ( int   i = 2 ; i * i <= n ; i ++)      if ( n % i == 0 ){          flag  =  false ;          break ;   ...

Zig Zag Pattern

  /* @ToDo     Zig Zag Pattern         *               *                *       *       *       *        *               *               *        */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE          freopen ( "../asset/input.txt" , "r" , stdin );          freopen (...

Star Pattern

  /* @ToDo     Star Pattern                  *                *   *   *            *   *   *   *   *        *   *   *   *   *   *   *    *   *   *   *   *   *   *   *   *    *   *   *   *   *   *   *   *   *        *  ...

Palindromic Pattern

  /* @ToDo     Palindromic Pattern                 1                2   1   2            3   2   1   2   3        4   3   2   1   2   3   4    5   4   3   2   1   2   3   4   5        */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE        ...

Number Pattern

  /* @ToDo     Number Pattern                 1                    1       2                1       2       3            1       2       3       4        1       2       3       4       5            */ #include   <iost...

Rhumbus Pattern

  /* @ToDo     Rhumbus Pattern                 *   *   *   *   *                *   *   *   *   *            *   *   *   *   *        *   *   *   *   *    *   *   *   *   *        */ #include   <iostream> using   namespace   std ; int   main (){       #ifndef  ONLINE_JUDGE        ...

Subscribe to Get's Answer by Email