Skip to main content

Posts

Showing posts with the label aCoding

Pattern 1 | aCoding | Day 1

Problem:   1. You are given a number n. 2. You've to create a pattern of * and separated by tab as shown in output format. Input Format A number n Constraints 1 <= n <= 100 Sample Input 2 Sample Output * * * Solution: import java.util.*; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); // write ur code here int n = scn.nextInt(); for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) System.out.print("*\t"); System.out.println(); } } } Follow us for more such content...

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

N Queens

1. You are given a number n, the size of a chess board. 2. You are required to place n number of queens in the n * n cells of board such that no queen can kill another. Note - Queens kill at distance in all 8 directions 3. Complete the body of printNQueens function - without changing signature - to calculate and print all safe configurations of n-queens. Use sample input and output to get more idea. Input Format A number n Output Format Safe configurations of queens as suggested in sample output Constraints 1 <= n <= 10 Sample Input 4 Sample Output 0-1, 1-3, 2-0, 3-2, . 0-2, 1-0, 2-3, 3-1, . Solution: import java.io.*; import java.util.*; public class Main {   public static void main(String[] args) throws Exception {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     int n = Integer.parseInt(br.readLine());     int[][] chess = new int[n][n];     printNQueens(chess, "", 0);   }   public static v...

Print 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. Complete the body of printEncodings function - without changing signature - to calculate and print all encodings of str. Use the input-output below to get more understanding on what is required 123 -> abc, aw, lc 993 -> iic 013 -> Invalid input. A string starting with 0 will not be passed. 103 -> jc 303 -> No output possible. But such a string maybe passed. In this case print nothing. Input Format A string str Output Format Permutations of str in order hinted by Sample output Constraints 0 <= str.length <= 10 Sample Input 655196 Sample Output feeaif feesf Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception { ...

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 Maze Paths 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 printMazePath 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 <= 5 0 <= m <= 5 Sample Input 3 3 Sample Output h1h1v1v1 h1h1v2 h1v1h1v1 h1v1v1h1 h1v1d1 h1v2h1 h1d1v1 h2v1v1 h2v2 v1h1h1v1 v1h1v1h1 v1h1d1 v1h2v1 v1v1h1h1 v1v1h2 v1d1h1 v2h1h1 v2h2 d1h1v1 d1v1h1 d1d1 d2 Solution: import java.io.*; import java.util.*; publ...

Print 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 printStairPaths function - without changing signature - to print 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 Print paths (one path in each line) in order hinted by 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();         printStairPaths(n,"");     }     public static void printStairPaths(int n, String path) {       ...

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

Print Subsequence

 1. You are given a string str. 2. Complete the body of printSS function - without changing signature - to calculate and print all subsequences of str. Use sample input and output to take idea about subsequences.. Input Format A string str Output Format Subsequences of str in order hinted by Sample output Constraints 0 <= str.length <= 7 Sample Input yvTA Sample Output yvTA yvT yvA yv yTA yT yA y vTA vT vA v TA T A 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();         printSS(str,"");     }     public static void printSS(String str, String ans) {         if(str.length() == 0){             System.out.println(ans);             return;     ...

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

Power-linear

  1. You are given a number x. 2. You are given another number n. 3. You are required to calculate x raised to the power n. Don't change the signature of power function . Input Format A number x A number n Output Format x raised to the power n Constraints 1 <= x <= 10 0 <= n <= 9 Sample Input 2 5 Sample Output 32 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 x = sc.nextInt();         int n = sc.nextInt();         System.out.println(power(x,n));     }     public static int power(int x, int n){         if(n == 0) return 1;         return x * power(x,n-1);     } }

Tower Of Hanoi

  1. There are 3 towers. Tower 1 has n disks, where n is a positive number. Tower 2 and 3 are empty. 2. The disks are increasingly placed in terms of size such that the smallest disk is on top and largest disk is at bottom. 3. You are required to 3.1. Print the instructions to move the disks. 3.2. from tower 1 to tower 2 using tower 3 3.3. following the rules 3.3.1 move 1 disk at a time. 3.3.2 never place a smaller disk under a larger disk. 3.3.3 you can only move a disk at the top. 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 number n, representing number of disks A number n1, representing id of tower 1 A number n2, representing id of tower 2 A number n3, representing id of tower 3 Output Format n[n1 -> n2] .. A set of instructions in abov...

Print Zigzag

  1. Here are a few sets of inputs and outputs for your reference Input1 -> 1 Output1 -> 1 1 1 Input2 -> 2 Output2 -> 2 1 1 1 2 1 1 1 2 Input2 -> 3 Output3 -> 3 2 1 1 1 2 1 1 1 2 3 2 1 1 1 2 1 1 1 2 3 2. Figure out the pattern and complete the recursive function pzz to achieve the above for any positive number n. 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 number n Output Format As discussed in point 1 of description Constraints 1 <= n <= 10 Sample Input 3 Sample Output 3 2 1 1 1 2 1 1 1 2 3 2 1 1 1 2 1 1 1 2 3 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(); pzz(n); ...

Rotate By 90 Degree

  1. You are given a number n, representing the number of rows and number of columns. 2. You are given n*n numbers, representing elements of 2d array a. 3. You are required to rotate the matrix by 90 degree clockwise and then display the contents using display function. *Note - you are required to do it in-place i.e. no extra space should be used to achieve it .* Input Format A number n e11 e12.. e21 e22.. .. n * n number of elements Output Format output is taken care of by display function Constraints 1 <= n <= 10^2 -10^9 <= e1, e2, .. n * n elements <= 10^9 Sample Input 4 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 Sample Output 41 31 21 11 42 32 22 12 43 33 23 13 44 34 24 14 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][n]; for(int i=0;i<n;i++...

Exit Point Of A Matrix

  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 (1's and 0's), representing elements of 2d array a. 4. Consider this array a maze and a player enters from top-left corner in east direction. 5. The player moves in the same direction as long as he meets '0'. On seeing a 1, he takes a 90 deg right turn. 6. You are required to print the indices in (row, col) format of the point from where you exit the matrix. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format row col (of the point of exit) Constraints 1 <= n <= 10^2 1 <= m <= 10^2 e1, e2, .. n * m elements belongs to the set (0, 1) Sample Input 4 4 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 Sample Output 1 3 import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { // write your code here Sc...