I came across these two questions and found similarities.

- Buy and Sell only 1 time to get maximum profit.
- 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:** 5

**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. …

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…

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:

- Each cell contains an integer from 1 and n (inclusive).
- No integer appears twice in the same row or the same column.
- 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 = 1

Output : 1

Input : n = 3

Output: 3 2 1…

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:

- initialize stairs and ladder as the first element of the array.
- Keep track of the maximum ladder we got as of now.
- decrease the stairs as we climb up.
- if stairs become zero in the current ladder make a jump to another ladder and recalculate the stairs left in that ladder to reach the top(stairs = ladder level)

Below is the implementation of the code.

`class Solution {`

public int…

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…

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

- Symmetric Tree:

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

**Input:**

5

/ \

1 1

/ \

2 2

Output: True

Explanation: Tree is mirror image of

itself i.e. tree is symmetric

Here comes the solution for the same.

/*

class of the node of the tree is as

class 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 not

class GfG

{…

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

Now, let's discuss the solution:

- Check for a balanced tree:

**Input:**

10

/ \

20 30

/ \

40 60

**Output:** 1

Explanation: The max difference in height

of 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…

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…

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…

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…

Software Developer