Skip to main content

Posts

Showing posts from September, 2021

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

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. Problem Constraints 1 <= Number of Testcases <= 100 1 <= |A|

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 numbers in reverse order. NOTE: See

2d Arrays Demo

  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 display the contents of 2d array as suggested by output format below. Input Format A number n A number m e11 e12.. e21 e22.. .. n * m number of elements Output Format e11 e12 e13 .. e21 e22 e23 .. Constraints 1 <= n <= 10^2 1 <= m <= 10^2 -10^9 <= e1, e2, .. n * m elements <= 10^9 Sample Input 2 4 11 12 13 14 21 22 23 24 Sample Output 11 12 13 14 21 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 scn = new Scanner(System.in); int n = scn.nextInt(); int m = scn.nextInt(); int[][] A = new int[n][m]; for(int i=0;i<n;i++) for(int j=0;j<m;j++) A[i][j] = scn.nextInt(); for(int i=0;i<n;i++

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 = (int)Math.pow(2,n);     for(int i=0;i<num;i++){         String str = "";         int temp

First Index And Last Index

  1. You are given a number n, representing the size of array a. 2. You are given n numbers, representing elements of array a. Asssumption - Array is sorted. Array may have duplicate values. Input Format A number n n1 n2 .. n number of elements A number d Output Format A number representing first index A number representing last index Constraints 1 <= n <= 1000 1 <= n1, n2, .. n elements <= 100 1 <= d <= 100 Sample Input 15 1 5 10 15 22 33 33 33 33 33 40 42 55 66 77 33 Sample Output 5 9 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 d = scn.nextInt(); System.out.println(binarySearch(0,n-1,A,d,true)); System.out.println(binarySearch(0,n-1,A,d,false)); } public static int binarySearch(int lt, int rt,

Broken Economy

  In a country of novice government, the economic system is changed where only coins are used that too of various denominations. Whenever a foreigner visits this country, they visit a money exchanger to get the currency of the same country. As the foreigner is unaware of the denomination of the country, the money exchange prefers to tell them the denomination which is the nearest maximum and nearest minimum to the denomination mentioned by the foreigner. In case they get the correct guess of the denomination, they are told the same denomination. The denominations are always quoted in ascending order. Example 1: In a country, 8 given denominations are as follows [5, 10, 15, 22, 33, 40, 42, 55] The foreigner asks for denomination 25. The money exchange tells them that denominations of 33 and 22 are available. Example 2: In a country, 5 given denominations are as follows [7, 14, 18, 25, 30] The foreigner asks for the denomination of 18. The money exchange tells them a denomination of 18 i

Classes and Objects | Accenture TFA

  Question  1 Correct Mark 1.00 out of 1.00 Flag question Question text ___ and _____ are the access specifiers that can be applied to top level Class. Select one or more: public   protected default   private Feedback Your answer is correct. The correct answers are: default, public Question  2 Correct Mark 1.00 out of 1.00 Flag question Question text class Sample{      private double num = 100;    private int square(int a){ return a*a;    } }   public class Test{    public static void main(String args[]){   Sample obj = new Sample();   System.out.println(obj.num);  System.out.println(obj.square(10));    }   } Select one: Compile time error   Run time error Executes but no output 100 Feedback Your answer is correct. The correct answer is: Compile time error Question  3 Correct Mark 1.00 out of 1.00 Flag question Question text Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere. class Test { Choose... private protected public d