Skip to main content

Posts

Showing posts with the label AAA Question

Balanced Brackets

1. You are given a string exp representing an expression. 2. You are required to check if the expression is balanced i.e. closing brackets and opening brackets match up well. e.g. [(a + b) + {(c + d) * (e / f)}] -> true [(a + b) + {(c + d) * (e / f)]} -> false [(a + b) + {(c + d) * (e / f)} -> false ([(a + b) + {(c + d) * (e / f)}] -> false Input Format A string str Output Format true or false Constraints 0 <= str.length <= 100 Sample Input [(a + b) + {(c + d) * (e / f)}] 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);         String str = sc.nextLine();         int n = str.length();         Stack<Character> s = new Stack<>();         for(int i=0;i<n;i++){           ...

Tiling With M * 1 Tiles

 1. You are given a number n and a number m separated by line-break representing the length and breadth of a m * n floor. 2. You've an infinite supply of m * 1 tiles. 3. You are required to calculate and print the number of ways floor can be tiled using tiles. Input Format A number n A number m Output Format A number representing the number of ways in which the number of ways floor can be tiled using tiles. Constraints 1 <= n <= 100 1 <= m <= 50 Sample Input 39 16 Sample Output 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 m = sc.nextInt();                 int[] dp = new int[n+1];         for(int i=1;i<=n;i++){           ...

Duplicate Brackets

 1. You are given a string exp representing an expression. 2. Assume that the expression is balanced  i.e. the opening and closing brackets match with each other. 3. But, some of the pair of brackets maybe extra/needless.  4. You are required to print true if you detect extra brackets and false otherwise. e.g.' ((a + b) + (c + d)) -> false (a + b) + ((c + d)) -> true Input Format A string str Output Format true or false Constraints 0 <= str.length <= 100 Sample Input (a + b) + ((c + d)) 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);         String str = sc.nextLine();         Stack<Character> s = new Stack<>();         int n = str.length();         boolean flag = false;     ...

Buy And Sell Stocks - K Transactions Allowed

 1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are given a number k, representing the number of transactions allowed. 3. You are required to print the maximum profit you can make if you are allowed k transactions at-most. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy). Input Format A number n .. n more elements A number k Output Format A number representing the maximum profit you can make if you are allowed a single transaction. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 10 0 <= k <= n / 2 Sample Input 6 9 6 7 6 3 8 1 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      ...

Buy And Sell Stocks With Cooldown - Infinite Transaction Allowed

 1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are required to print the maximum profit you can make if you are allowed infinite transactions, but have to cooldown for 1 day after 1 transaction i.e. you cannot buy on the next day after you sell, you have to cooldown for a day at-least before buying again. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy). Input Format A number n .. n more elements Output Format A number representing the maximum profit you can make if you are allowed infinite transactions with cooldown of 1 day. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 10 Sample Input 12 10 15 17 20 16 18 22 20 22 20 23 25 Sample Output 19 Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] arg...

Buy And Sell Stocks With Transaction Fee - Infinite Transactions Allowed

 1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are give a number fee, representing the transaction fee for every transaction. 4. You are required to print the maximum profit you can make if you are allowed infinite transactions, but has to pay "fee" for every closed transaction. Note - There can be no overlapping transaction. One transaction needs to be closed (a buy followed by a sell) before opening another transaction (another buy). Input Format A number n .. n more elements A number fee Output Format A number representing the maximum profit you can make if you are allowed infinite transactions with transaction fee. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 10 0 <= fee <= 5 Sample Input 12 10 15 17 20 16 18 22 20 22 20 23 25 3 Sample Output 13 Solution  import java.io.*; import java.util.*; public class Main {     public static void main...

Buy And Sell Stocks - One Transaction Allowed

 1. You are given a number n, representing the number of days. 2. You are given n numbers, where ith number represents price of stock on ith day. 3. You are required to print the maximum profit you can make if you are allowed a single transaction. Input Format A number n .. n more elements Output Format A number representing the maximum profit you can make if you are allowed a single transaction. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 10 Sample Input 9 11 6 7 19 4 1 6 18 4 Sample Output 17 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];         for(int i=0;i<n;i++)             A[i] = sc.nextInt(); ...

Paint Fence

1. You are given a number n and a number k in separate lines, representing the number of fences and number of colors. 2. You are required to calculate and print the number of ways in which the fences could be painted so that not more than two consecutive  fences have same colors. Input Format A number n A number k Output Format A number representing the number of ways in which the fences could be painted so that not more than two fences have same colors. Constraints 1 <= n <= 10 1 <= k <= 10 Sample Input 8 3 Sample Output 3672 Solution import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         // input         Scanner sc = new Scanner(System.in);         int n = sc.nextInt();         int k = sc.nextInt();                  int diff = k * (k-1), same = k;  ...

Maximum Sum Non Adjacent Elements

1. You are given a number n, representing the count of elements. 2. You are given n numbers, representing n elements. 3. You are required to find the maximum sum of a subsequence with no adjacent elements. Input Format A number n n1 n2 .. n number of elements Output Format A number representing the maximum sum of a subsequence with no adjacent elements. Constraints 1 <= n <= 1000 -1000 <= n1, n2, .. n elements <= 1000 Sample Input 6 5 10 10 100 5 6 Sample Output 116 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 ncld =...

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 {         // in         Scanner sc = new Scanner(System.in);         String str = sc.nextLine();         sc.close();         // process         int a = 0, ab = 0, abc = 0, n = str.length();         for(int i=0;i<n;i++){             char ch = str.charAt(i);   ...

Print Permutations

 1. You are given a string str. 2. Complete the body of printPermutations function - without changing signature - to calculate and print all permutations of str. Use sample input and output to take idea about permutations. Note -> The online judge can't force you to write the function recursively but that is what the spirit of question is. Write recursive and not iterative logic. The purpose of the question is to aid learning recursion and not test you. Input Format A string str Output Format Permutations of str in order hinted by Sample output Constraints 0 <= str.length <= 7 Sample Input abc Sample Output abc acb bac bca cab cba 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();         printPermutations(str,"");     }     pu...

Print Kpc

 1. You are given a string str. The string str will contains numbers only, where each number stands for a key pressed on a mobile phone. 2. The following list is the key to characters map     0 -> .;    1 -> abc    2 -> def    3 -> ghi    4 -> jkl    5 -> mno    6 -> pqrs    7 -> tu    8 -> vwx    9 -> yz 3. Complete the body of printKPC function - without changing signature - to print the list of all words that could be produced by the keys in str. Use sample input and output to take idea about output.                           Input Format A string str Output Format Words that can be produced by pressed keys indictated by str in order hinted by Sample output Constraints 0 <= str.length <= 10 str contains numbers only Sample Input 78 Sample Output tv tw tx uv uw ux Solution: import j...

Get Maze Path With Jumps

  1. You are given a number n and a number m representing number of rows and columns in a maze. 2. You are standing in the top-left corner and have to reach the bottom-right corner. 3. In a single move you are allowed to jump 1 or more steps horizontally (as h1, h2, .. ), or 1 or more steps vertically (as v1, v2, ..) or 1 or more steps diagonally (as d1, d2, ..). 4. Complete the body of getMazePath function - without changing signature - to get the list of all paths that can be used to move from top-left to bottom-right. Use sample input and output to take idea about output. Input Format A number n A number m Output Format Contents of the arraylist containing paths as shown in sample output Constraints 0 <= n <= 10 0 <= m <= 10 Sample Input 2 2 Sample Output [h1v1, v1h1, d1] Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         Scanner sc = new Scanner(Syste...

Get Maze Paths

  1. You are given a number n and a number m representing number of rows and columns in a maze. 2. You are standing in the top-left corner and have to reach the bottom-right corner. Only two moves are allowed 'h' (1-step horizontal) and 'v' (1-step vertical). 3. Complete the body of getMazePath function - without changing signature - to get the list of all paths that can be used to move from top-left to bottom-right. Use sample input and output to take idea about output. Input Format A number n A number m Output Format Contents of the arraylist containing paths as shown in sample output Constraints 0 <= n <= 10 0 <= m <= 10 Sample Input 3 3 Sample Output [hhvv, hvhv, hvvh, vhhv, vhvh, vvhh] 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(), m = sc.nextInt();   ...

Get Stair Paths

  1. You are given a number n representing number of stairs in a staircase. 2. You are standing at the bottom of staircase. You are allowed to climb 1 step, 2 steps or 3 steps in one move. 3. Complete the body of getStairPaths function - without changing signature - to get the list of all paths that can be used to climb the staircase up. Use sample input and output to take idea about output. Input Format A number n Output Format Contents of the arraylist containing paths as shown in sample output Constraints 0 <= n <= 10 Sample Input 3 Sample Output [111, 12, 21, 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);         int n = sc.nextInt();         ArrayList<String> res = getStairPaths(n);         System.out.println(res);     }     pub...

Get Kpc

  1. You are given a string str. The string str will contains numbers only, where each number stands for a key pressed on a mobile phone. 2. The following list is the key to characters map : 0 -> .; 1 -> abc 2 -> def 3 -> ghi 4 -> jkl 5 -> mno 6 -> pqrs 7 -> tu 8 -> vwx 9 -> yz 3. Complete the body of getKPC function - without changing signature - to get the list of all words that could be produced by the keys in str. Use sample input and output to take idea about output. Input Format A string str Output Format Contents of the arraylist containing words as shown in sample output Constraints 0 <= str.length <= 10 str contains numbers only Sample Input 78 Sample Output [tv, tw, tx, uv, uw, ux] Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         Scanner sc = new Scanner(System.in);     ...

Get Subsequence

  1. You are given a string str. 2. Complete the body of getSS function - without changing signature - to calculate all subsequences of str. Use sample input and output to take idea about subsequences. Input Format A string str Output Format Contents of the arraylist containing subsequences as shown in sample output Constraints 0 <= str.length <= 20 Sample Input abc Sample Output [, c, b, bc, a, ac, ab, abc] 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();         ArrayList<String> res = new ArrayList<>();         res = gss(str);         System.out.println(res);     }     public static ArrayList<String> gss(String str) {         ArrayList<String>...