# Best Time to Buy and Sell Stock |

I came across these two questions and found similarities.

1. Buy and Sell only 1 time to get maximum profit.
2. Buy and Sell multiple times to get maximum profit.

Here comes the solutions for the same, The code is the same for both the questions but with minor differences.

Solution 1: Buy and Sell single time:

`Input: prices = [7,1,5,3,6,4]Output: 5Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. …`

# Maximum Sum Path in Two Arrays

This is a good question, So decided to solve it.

Given two sorted arrays, such that the arrays may have some common elements. Find the sum of the maximum sum path to reach from the beginning of any array to end of any of the two arrays. We can switch from one array to another array only at common elements.
Note: The common elements do not have to be at the same indexes.
Expected Time Complexity: O(m+n), where m is the number of elements in ar1[] and n is the number of elements in ar2[].

Examples:

`Input: ar1[] = {2…`

# Construct a unique matrix n x n for an input n

This is marked as Hard on the geek's platform.

Given an odd integer n, find a matrix of size n x n with the following conditions:

1. Each cell contains an integer from 1 and n (inclusive).
2. No integer appears twice in the same row or the same column.
3. All 1’s must be at every possible distance from the center of the matrix. The center of a n x n square is cell ((n-1)/2, (n-1)/2) for odd n.

Example :

`Input  : n = 1Output : 1Input : n = 3Output: 3 2 1…`

# Minimum number of jumps to reach end | Jump Game II

This question is based on ladder and stairs, We need to find the minimum number of jumps to reach the end.

Here we will follow few steps:

1. initialize stairs and ladder as the first element of the array.
2. Keep track of the maximum ladder we got as of now.
3. decrease the stairs as we climb up.

Below is the implementation of the code.

`class Solution {    public int…`

# Subtree of Another Tree

This question is similar to check if two trees are symmetric or not.

https://leetcode.com/problems/subtree-of-another-tree/

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree that consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.

Example 1:
Given tree s:

`3    / \   4   5  / \ 1   2`

Given tree t:

`4   / \ 1   2`

Return true, because t has the same…

# Determine if Two Trees are Identical | Symmetric Tree(Mirror Image)

I came across these 2 questions and found a similarity in the logic part.

1. Symmetric Tree:

We need to check symmetry for the same tree or say mirror image.

`Input:         5       /   \      1     1     /       \    2         2Output: TrueExplanation: Tree is mirror image ofitself i.e. tree is symmetric`

Here comes the solution for the same.

`/*class of the node of the tree is asclass Node{    int data;    Node left;    Node right;    Node(int data){        this.data = data;        left=null;        right=null;    }}*/// complete this function// return true/false if the is Symmetric or notclass GfG{…`

# Check for Balanced Tree | Diameter of Binary Tree

I came across these 2 questions and found similarities in both of them.

Now, let's discuss the solution:

1. Check for a balanced tree:
`Input:       10     /   \    20   30   /   \ 40   60Output: 1Explanation: The max difference in heightof left subtree and right subtree is 1.Hence balanced.`

Here First we need to find the height of the binary tree for both part i.e. left tree and right tree for each node, if the difference between them is greater than 1 then it is not balanced tree, return false.

How to find the height of the binary…

# Inner Classes in Java

Recently I study the topic and sharing the notes for the same.

Inner Classes

Without existing one type of object if there is no chance of existing another type of object then we should go for inner classes.
Note(we can declare anything inside anything means class inside interface or interface inside the class)
Ex: 1 class Car{
class Engine{}
}
Here without Car object, there is no chance of an Engine object.

Ex: 2 Map Interface: A map is a group of key-value pair and each key-value pair is called an entry, without an existing map object there is no…

# Interview Question

I came across this question in an interview recently for the SDE-2 position.

There is a shop with old-style cash registers. the price of each item is typed manually. Given a list of items and their correct prices, compare the prices to those entered when each item was sold. Determine the number of errors in the selling price.

`Input:products = ['eggs','milk','cheese']productPrices = [2.89,3.29,5.79]productsSold = ['eggs','eggs','milk','cheese']sellingPrices = [2.89,2.99,3.29,5.97]Output: errors = 2`

Here Comes the Solution for the same.

Use 2 HashMap, one for Storing the original product name and product price, HashMap<String, Float> map2 = new…

# Generics

Here are the notes I made after studying the Generics concept in java.

GENERICS:
1. Introduction.
2. Generic Classes.
3. Bounded Types.
4. Generic Methods and Wild Card Characters.
5. Communication with non-generic code.
6. Conclusions.

Generics: The main Objective of Generics is
1. To provide type-safety and
2. To resolve the type-casting problem.
______________________________________________________________________________________________________________________________________________
1. Introduction:

Type-casting is a headache in the collection, So to overcome this SUN people introduces the Generics concept in the 1.5 version.

Type-Safety:
Arrays are type-safe we can give the guarantee of type of elements present inside the array(except objects type)
Ex: If we…

## Vipul Gupta

Software Developer

Get the Medium app