Skip to main content

Posts

Showing posts with the label A 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++){           ...

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

Partition Into Subsets

1. You are given a number n, representing the number of elements. 2. You are given a number k, representing the number of subsets. 3. You are required to print the number of ways in which these elements can be partitioned in k non-empty subsets. E.g. For n = 4 and k = 3 total ways is 6 12-3-4 1-23-4 13-2-4 14-2-3 1-24-3 1-2-34 Input Format A number n A number k Output Format A number representing the number of ways in which these elements can be partitioned in k non-empty subsets. Constraints 0 <= n <= 20 0 <= k <= n Sample Input 4 3 Sample Output 6 Solution:  import java.io.*; import java.util.*; public class Main {              public static long partitionKSubset(int n, int k) {         // write your code here         if(n == 0 || k==0 || n<k) return 0;                  long dp[][] = new long[k+1][n+1];         ...

Tiling With 2 * 1 Tiles

 1. You are given a number n representing the length of a floor space which is 2m wide. It's a 2 * n board. 2. You've an infinite supply of 2 * 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 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 Sample Input 8 Sample Output 34 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 prev = 1, next = 1, sum = 0;         for(int i=2;i<=n;i++){             sum = prev + next;             prev =next...