Posts

Triplets with Sum between given range - The Coding Shala

Image
Home >> Interview Questions >> Triplets with sum between given range Triplets with Sum between given range Java Solution Given an array of real numbers greater than zero in the form of strings. Find if there exists a  triplet  (a,b,c) such that  1  <  a+b+c  <  2  .  Return  1  for  true  or  0  for  false . Example: Given [0.6, 0.7, 0.8, 1.2, 0.4] , You should return  1 as 0.6+0.7+0.4=1.7 1<1.7<2 Hence, the output is 1. Triplets with Sum between given range Java Solution Here an array will be given, we need to find three values whose sum will be in the given range. We can solve this using brute force using three loops but that will give time limit error.  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 public class Solution { pub...

Anti Diagonals - The Coding Shala

Image
Home >> Interview Questions >> Anti Diagonals Anti Diagonals Java Solution Give an N*N square matrix, return an array of its anti-diagonals. Look at the example for more details. Example: Input: 1 2 3 7 8 9 4 5 6 Return the following : [ [1], [2, 4], [3, 5, 7], [6, 8], [9] ] Input : 1 2 3 4 Return the following : [ [1], [2, 3], [4] ] Anti Diagonals Java Solution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class Solution { public ArrayList < ArrayList < Integer >> diagonal ( ArrayList < ArrayList < Integer >> A ) { ArrayList < ArrayList < Integer >> ans = new ArrayList < ArrayList < Integer >>(); int N = A . size (); int i = N - 1 ; while ( i >= 0 ){ ArrayList < Integer > tmp = new ArrayList < Integer >(); ...

Kth Row of Pascal's Triangle - The Coding Shala

Image
Home >> Interview Questions >> Kth row of pascal's triangle Kth Row of Pascal's Triangle Solution Java Given an index k, return the kth row of Pascal’s triangle. Example: Input : k = 3 Return : [1,3,3,1] Java Solution of Kth Row of Pascal's Triangle One simple method to get the Kth row of Pascal's Triangle is to generate Pascal Triangle till Kth row and return the last row.  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Solution { public ArrayList < Integer > getRow ( int A ) { ArrayList < ArrayList < Integer >> res = new ArrayList < ArrayList < Integer >>(); for ( int i = 0 ; i <= A ; i ++){ ArrayList < Integer > tmp = new ArrayList < Integer >(); for ( int j = 0 ; j <= i ; j ++){ if ( j == i || j == 0 ) tmp . add ( 1 ); else tmp . add ( r...

Pascal Triangle - The Coding Shala

Image
Home >> Interview Questions >> Pascal Triangle Pascal Triangle Java Solution Given numRows, generate the first numRows of Pascal’s triangle. Pascal’s triangle: To generate A[C] in row R, sum up A’[C] and A’[C-1] from previous row R - 1. Example: Given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Solution: (Java) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Solution { public ArrayList < ArrayList < Integer >> solve ( int A ) { ArrayList < ArrayList < Integer >> res = new ArrayList < ArrayList < Integer >>(); for ( int i = 0 ; i < A ; i ++){ ArrayList < Integer > temp = new ArrayList < Integer >(); for ( int j = 0 ; j <= i ; j ++){ if ( j == i || j == 0 ) temp . add ( 1 ); else { temp . add ...

Spiral Order Matrix II - The Coding Shala

Image
Home >> Interview Questions >> Spiral Order matrix 2 Spiral Order Matrix II Problem Solution Given an integer n, generate a square matrix filled with elements from 1 to n 2  in spiral order. Example: Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] Solution: (Java) The concept is the same as how to print the Spiral Order Matrix. Approach: Let's see for n= 4, then numbers will come from 1 to 16(n*n) in the matrix. As we need to insert numbers in spiral order so here we need 4 things to check, first and last column and top and bottom row. The following four steps we need to do: First, we need to insert numbers in the top row. Our loop will go from the left column to the right then will increase the top row by one and now the top is our second row. Now we are inserting the numbers in the right column, will start the loop from top to bottom, and then decrease the value of the right ...

Max Non Negative SubArray - The Coding Shala

Image
Home >> Interview Questions >> Max non-negative subarray Max Non-Negative SubArray InterviewBit Solution Find out the maximum sub-array of non-negative numbers from an array. The sub-array should be continuous. That is, a sub-array created by choosing the second and fourth elements and skipping the third element is invalid. The maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if  sum(A) > sum(B) . Example: A : [1, 2, 5, -7, 2, 3] The two sub-arrays are [1, 2, 5] [2, 3]. The answer is [1, 2, 5] as its sum is larger than [2, 3] NOTE:   If there is a tie, then compare with segment's length and return segment which has maximum length NOTE 2:   If there is still a tie, then return the segment with minimum starting index Solution: (Java) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 3...