Skip to main content

Posts

Showing posts with the label CodingDare

Target Sum Subsets

 1. You are given a number n, representing the count of elements. 2. You are given n numbers. 3. You are given a number "tar". 4. Complete the body of printTargetSumSubsets function - without changing signature - to calculate and print all subsets of given elements, the contents of which sum to "tar". Use sample input and output to get more idea. Input Format Input Format A number n n1 n2 .. n number of elements A number tar Output Format Comma separated elements of the subset, the contents of which add to "tar" .. all such subsets, each in a single line (the elements of each subset should be comma separated) Constraints 1 <= n <= 30 0 <= n1, n2, .. n elements <= 20 0 <= tar <= 50 Sample Input 5 10 20 30 40 50 60 Sample Output 10, 20, 30, . 10, 50, . 20, 40, . Solution: import java.io.*; import java.util.*; public class Main {     public static void main(String[] args) throws Exception {         Scanner sc = new Scanner(System.in); ...

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

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

Spiral Display

  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. 4. You are required to traverse and print the contents of the 2d array in form of a spiral. Note - Please check the sample output for details. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format e11 e21 .. en1 en2 en3 .. enm e(n-1)m .. e1m e1(m-1) .. e12 e22 e32 .. Constraints 1 <= n <= 10^2 1 <= m <= 10^2 -10^9 <= e1, e2, .. n * m elements <= 10^9 Sample Input 3 5 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 Sample Output 11 21 31 32 33 34 35 25 15 14 13 12 22 23 24 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 m = sc.nextInt(); ...

The State Of Wakanda - 1

  The historic state of Wakanda has various monuments and souvenirs which are visited by many travelers every day. The guides follow a prescribed route of visiting the monuments which improve them understand the relevance of each monument. The route of the monument is fixed and expressed in a 2-d matrix where the travelers visit the prescribed next monument. For example 1 2 3 4 5 6 7 8 9 is the prescribed route and the visitors travels this path: 1->2->3->4->5->6->7->8->9 However, a certain visitor decides to travel a different path as follows: 1. He first travels southwards till no further south places are available. 2. He then moves only 1 place eastwards. 3. He starts to move again towards north till any further north moves are available. This continues till all the places are covered. For example, the monuments are named as follows: 1 2 3 4 5 6 7 8 9 Path followed by traveler: 1->4->7->8->5->2->3->6->9 You are required...

Matrix Multiplication

  1. You are given a number n1, representing the number of rows of 1st matrix. 2. You are given a number m1, representing the number of columns of 1st matrix. 3. You are given n1*m1 numbers, representing elements of 2d array a1. 4. You are given a number n2, representing the number of rows of 2nd matrix. 5. You are given a number m2, representing the number of columns of 2nd matrix. 6. You are given n2*m2 numbers, representing elements of 2d array a2. 7. If the two arrays representing two matrices of dimensions n1 * m1 and n2 * m2 can be multiplied, display the contents of prd array as specified in output Format. 8. If the two arrays can't be multiplied, print "Invalid input". Input Format A number n1 A number m1 e11 e12.. e21 e22.. .. n1 * m1 number of elements of array a1 A number n2 A number m2 e11' e12'.. e21' e22'.. .. n2 * m2 number of elements of array a2 Output Format e11' e12' e13' .. e21' e22' e23' .. .. elements of prd ar...

Java Data Structures Stacks | JavaTutorial

  Syntax to create an Stack: // Create Integer type stack Stack < Integer > myStack = new Stack<>(); The above code create an stack named myStack which can store integers. Some more functions which we can use with stack: myStack.push(x)  : Use  push()  to push an element into the stack. myStack.pop()  : To remove an element from the top of the stack, we use the  pop()  method. myStack.peek()  : The  peek()  method returns an object from the top of the stack. myStack.empty()  : To check whether a stack is  empty or not , we use the  empty()  method. Now, try to solve the below problem using stack : Given a string A consisting only of ’(‘ and ’)’ . You need to find whether parantheses in A is balanced or not ,if it is balanced then print 1 else print 0 . Examples of some correctly balanced strings are: “()()”, “((()))”, “((()))” Examples of some unbalanced strings are: “()(“, “(())”, “((“, “)(“ etc. ...

Java Data Structures ArrayList | JavaTutorial

Unlike arrays, arraylists can automatically adjust its capacity when we add or remove elements from it. Hence, arraylists are also known as dynamic arrays Syntax of Creating an ArrayList: // create Integer type arraylist ArrayList arrayList = new ArrayList<>() In the above program, we have used Integer not int . It is because we cannot use primitive types while creating an arraylist. Instead, we have to use the corresponding wrapper classes . Basic syntax: ArrayListName.size()  : use this to get the size of arraylist. ArrayListName.add(x)  : Use this to append an element  x  to the ArrayList. ArrayListName.get(i)  : use this to access the  i th  index element in the ArrayList. Remember ArrayList uses 0 based indexing. To read more about ArrayList click here Task: You are given a stream of positive integers as input and the stream ends when you encounter an negative element . You need to save this numbers in an ArrayList and then print this num...

Subsets Of Array

1. You are given a number n, representing the count of elements. 2. You are given n numbers. 3. You are required to print all subsets of arr. Each subset should be on separate line. For more clarity check out sample input and output. Input Format A number n n1 n2 .. n number of elements Output Format [Tab separated elements of subset] .. All subsets Constraints 1 <= n <= 10 0 <= n1, n2, .. n elements <= 10^3 Sample Input 3 10 20 30 Sample Output - - - - - 30 - 20 - - 20 30 10 - - 10 - 30 10 20 - 10 20 30 Solution: import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) throws Exception {     // write your code here     Scanner scn = new Scanner(System.in);     int n= scn.nextInt();     int[] A = new int[n];     for(int i=0;i<n;i++)         A[i] = scn.nextInt();              int num ...