Skip to main content

Posts

Latest here:

Pattern 1 | aCoding | Day 1

Recent posts

Flood Fill

 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. The numbers can be 1 or 0 only. 4. You are standing in the top-left corner and have to reach the bottom-right corner.  Only four moves are allowed 't' (1-step up), 'l' (1-step left), 'd' (1-step down) 'r' (1-step right). You can only move to cells which have 0 value in them. You can't move out of the boundaries or in the cells which have value 1 in them (1 means obstacle) 5. Complete the body of floodfill function - without changing signature - to print all paths that can be used to move from top-left to bottom-right. Note1 -> Please check the sample input and output for details Note2 -> If all four moves are available make moves in the order 't', 'l', 'd' and 'r' Input Format A number n A number m e11 e12.. e21 e22.. ..

Print 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 pri tMazePath function - without changing signature - to print 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 Print paths (one path in each line) in order hinted by Sample output Constraints 0 <= n <= 10 0 <= m <= 10 Sample Input 2 2 Sample Output hv vh 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();             printMazePaths(1,1,n,m,"");     }    

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++){             char ch = str.charAt(i);             if(ch == '[')                 s.push(']');             else if(ch == '{'

Lucky Number | SnackDown 2021 - Online Qualifiers

Given three digits  A A ,  B B , and  C C  of the lottery ticket, tell whether Chef wins something or not? Input Format First line will contain  T T , the number of test cases. Then the test cases follow. Each test case contains a single line of input, three space separated integers  A , B , C A , B , C . Output Format For each testcase, output in a single line answer  "YES"  if Chef wins a positive amount with the lottery and  "NO"  if not. You may print each character of the string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). Constraints 1 ≤ T ≤ 1000 1 ≤ T ≤ 1000 0 ≤ A , B , C ≤ 9 0 ≤ A , B , C ≤ 9 Sample Input 1  3 0 0 0 7 8 9 2 7 7 Sample Output 1  NO YES YES Explanation Test Case  1 1 :  Since no digit is equal to  7 7 , Chef fails to win any amount in the lottery. Test Case  2 2 :  Since the first digit is equal to  7 7 , Chef will win some amount

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++){             if(i<m)                 dp[i] = 1;             else if(i == m)                 dp[i] = 2;             else                  dp[i] = dp[i-1] + dp[i-m];  

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();         int[] A = new int[n];         for(int i=0;i<n;i++)             A[i] =