December 26, 2020

Maximum size square sub-matrix with all 1s

 

Maximum size square sub-matrix with all 1s

 Approach: Think how you what information you are getting while traversing  the matrix. I find that while traversing the matrix i try to store that how many ones i have encountered  in past. So that is the key of solve the problem, just use your memory/result to get the result. Maximum size square can find as maximum number in the result matrix.

it can used to print the small matrix, but in actual there can be multiple places where you can find the square size coordinates.so returning the maximum size can solve the problem. To get the coordinates we in count how many max size matrix we have then we can try to use it.that coordinates with max square sum.

complexity space:(m*n) time: O(m*n)

December 23, 2020

Print different views of Tree like a Top, Bottom, Left, Right?

Question: Print different views of Tree like a Top, Bottom, Left, Right?

Problem link: https://www.hackerrank.com/challenges/tree-top-view/problem
Answer 
I am using Recursive approach to solve this Question, As it is easier to understand and explain, but it can lend up in stack overflow problem in case of a huge tree. It is good if we have some idea about the iterative approach. Followup Question may be asked that is the internal working of TreeMap and its data structure and its time complexity of inserting the data and traversing it.
1. TopView
 
To solve this Question, First, start thinking about how we can traverse the tree.
We can use recursion with the base condition that starts traversing left part of the tree and then right part. Here we got the hit that if we maintain horizontal distance from the root node that can give us the Top/Bottom view tree. 
To get the particular tree we use the level to get the specific Top/Bottom view tree.
Pair is a class that holds the two number that is node value and its level.
Time Complexity: O(n)
Space Complexity: O(n)
2. Bottom View
Only this below condition is changed.In order to get the bottom view of tree.
!map.containsKey(dist) || level >= map.get(dist).level
Time Complexity: O(n)
Space Complexity: O(n)
3. Left View 
now, this becomes easier as here we just need to maintain the max level till now while doing pre-order traversal.
Time Complexity: O(n)
Space Complexity: O(1)
4.Right View
now, this becomes easier as here we just need to maintain the max level till now while doing post-order traversal.
Time Complexity: O(n)
Space Complexity: O(1)
Code Solution :
Console 
6
1 2 5 3 6 4
Tree:  
  1
    \
     2
      \
       5
      /  \
     3    6
      \
       4
Top View: 
1 2 5 6 
Bottom View: 
1 3 4 6 
Left View: 
1 2 5 3 4 
Right View: 
1 2 5 6 4 

March 29, 2020

Google Code jam 2019 | Qualification Round | Foregone Solution


Problem: Someone just won the Code Jam lottery, and we owe them N jamcoins! However, when we tried to print out an oversized check, we encountered a problem. The value of N, which is an integer, includes at least one digit that is a 4... and the 4 key on the keyboard of our oversized check printer is broken.

Fortunately, we have a workaround: we will send our winner two checks for positive integer amounts A and B, such that neither A nor B contains any digit that is a 4, and A + B = N. Please help us find any pair of values A and B that satisfy these conditions.

Input

The first line of the input gives the number of test cases, TT test cases follow; each consists of one line with an integer N.

Output

For each test case, output one line containing Case #x: A B, where x is the test case number (starting from 1), and A and B are positive integers as described above.
It is guaranteed that at least one solution exists. If there are multiple solutions, you may output any one of them. (See "What if a test case has multiple correct solutions?" in the Competing section of the FAQ. This information about multiple solutions will not be explicitly stated in the remainder of the 2019 contest.)

Solution :
Problem link: https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/0000000000088231?show=progress