Skip to main content

Posts

Showing posts with the label Java

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

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

Test Match Series Problem Code: TESTSERIES | SnackDown 2021 - Online Qualifiers

A  5  match test series between India and England has just concluded. Every match could have ended either as a win for India, a win for England, or a draw. You know the result of all the matches. Determine who won the series or if it ended in a draw. A team is said to have won the series if it wins strictly more test matches than the other team. 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, five space-separated integers  R 1 , R 2 , R 3 , R 4 , R 5 R 1 , R 2 , R 3 , R 4 , R 5  denoting the results of all the five matches.  R i = 0 R i = 0  denotes that the test match ends in a draw.  R i = 1 R i = 1  denotes that the test match is won by India.  R i = 2 R i = 2  denotes that the test match is won by England. Output Format For each test output  "DRAW"  if the series ends in a draw,  "INDIA"  if the series is won by ...

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

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

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