Skip to main content

Posts

Showing posts with the label Algo

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

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

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

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

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

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

Cars and Bikes Problem Code: TYRES | CodeChef

Problem: Chef opened a company which manufactures cars and bikes. Each car requires   4 4  tyres while each bike requires  2 2  tyres. Chef has a total of  N N  tyres ( N N  is even). He wants to manufacture maximum number of cars from these tyres and then manufacture bikes from the remaining tyres. Chef's friend went to Chef to purchase a bike. If Chef's company has manufactured even a single bike then Chef's friend will be able to purchase it. Determine whether he will be able to purchase the bike or not. Input Format The first line contains an integer  T T  denoting the number of test cases. The  T T  test cases then follow. The first line of each test case contains an integer  N N  denoting the number of tyres. Output Format For each test case, output  YES  or  NO  depending on whether Chef's friend will be able to purchase the bike or not. Output is case insensitive. Constraints 1 ≤ T ≤ 100 1 ≤ T ≤...