Skip to main content

Posts

Showing posts with the label AA Question

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

Buy And Sell Stocks - 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 required to print the maximum profit you can make if you are allowed infinite transactions. 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. Constraints 0 <= n <= 20 0 <= n1, n2, .. <= 10 Sample Input 9 11 6 7 19 4 1 6 18 4 Sample Output 30 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(); ...

Next Greater Element To The Right

1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. 3. You are required to "next greater element on the right" for all elements of array 4. Input and output is handled for you. "Next greater element on the right" of an element x is defined as the first element to right of x having value greater than x. Note: If an element does not have any element on it's right side greater than it, consider -1 as it's "next greater element on right" e.g. for the array [2 5 9 3 1 12 6 8 7] Next greater for 2 is 5 Next greater for 5 is 9 Next greater for 9 is 12 Next greater for 3 is 12 Next greater for 1 is 12 Next greater for 12 is -1 Next greater for 6 is 8 Next greater for 8 is -1 Next greater for 7 is -1 Input Format Input is managed for you Output Format Output is managed for you Constraints 0 <= n < 10^5 -10^9 <= a[i] <= 10^9 Sample Input 5 5 3 8 -2 7 Sample Output 8 8 -1...

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 - Two 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 required to print the maximum profit you can make if you are allowed two 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 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 30 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(); ...

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

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

Friends Pairing

1. You are given a number n, representing the number of friends. 2. Each friend can stay single or pair up with any of it's friends. 3. You are required to print the number of ways in which these friends can stay single or pair up. E.g. 1 person can stay single or pair up in 1 way. 2 people can stay singles or pair up in 2 ways. 12 => 1-2, 12. 3 people (123) can stay singles or pair up in 4 ways. 123 => 1-2-3, 12-3, 13-2, 23-1. Input Format A number n Output Format A number representing the number of ways in which n friends can stay single or pair up. Constraints 0 <= n <= 20 Sample Input 4 Sample Output 10 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();          ...

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

Paint House - Many Colors

1. You are given a number n and a number k separated by a space, representing the number of houses and number of colors. 2. In the next n rows, you are given k space separated numbers representing the cost of painting nth house with one of the k colors. 3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color. Input Format A number n n1-0th n1-1st n1-2nd .. n1-kth n2-0th n2-1st n2-2nd .. n2-kth .. n number of elements Output Format A number representing the minimum cost of painting all houses without painting any consecutive house with same color. Constraints 1 <= n <= 1000 1 <= k <= 10 0 <= n1-0th, n1-1st, .. <= 1000 Sample Input 4 3 1 5 7 5 8 4 3 2 9 1 2 4 Sample Output 8 Solution import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         // input       ...

Paint House

 1. You are given a number n, representing the number of houses. 2. In the next n rows, you are given 3 space separated numbers representing the cost of painting nth house with red or blue or green color. 3. You are required to calculate and print the minimum cost of painting all houses without painting any consecutive house with same color. Input Format A number n n1red n1blue n1green n2red n2blue n2green .. n number of elements Output Format A number representing the minimum cost of painting all houses without painting any consecutive house with same color. Constraints 1 <= n <= 1000 0 <= n1red, n1blue, .. <= 1000 Sample Input 4 1 5 7 5 8 4 3 2 9 1 2 4 Sample Output 8 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); ...