♠️
Problem solving
Search a Range
Given a sorted array of integers A(0 based index) of size N, find the starting
and ending position of a given integer B in array A.
Your algorithm’s runtime complexity must be in the order of O(log n).
Return an array of size 2, such that first element = starting position of B in A
and second element = ending position of B in A, if B is not found in A return
[-1, -1].
Input Format
The first argument given is the integer array A.
The second argument given is the integer B.
Output Format
Return an array of size 2, such that first element = starting position of B in A
and second element = ending position of B in A, if B is not found in A return
[-1, -1].
Constraints
1 <= N <= 10^6
1 <= A[i], B <= 10^9
For Example
Input 1:
A = [5, 7, 7, 8, 8, 10]
B=8
Output 1:
[3, 4]
Explanation 1:
First occurence of 8 in A is at index 3
Second occurence of 8 in A is at index 4
ans = [3, 4]
Problem solving 1
Input 2:
A = [5, 17, 100, 111]
B=3
Output 2:
[-1, -1]
Profit Maximization
Given an array A , representing seats in each row of a stadium. You need to
sell tickets to B people.
Each seat costs equal to the number of vacant seats in the row it belongs
to. The task is to maximize the profit by selling the tickets to B people.
Problem Constraints
1 <= |A| <= 100000
1 <= B <= 1000000
Input Format
First argument is the array A.
Second argument is integer B.
Output Format
Return one integer, the answer to the problem.
Example Input
Input 1:
A = [2, 3]
B=3
Input 2:
A = [1, 4]
B=2
Example Output
Output 1:
7
Output 2:
7
Example Explanation
Explanation 1:
Problem solving 2
First you serve the seat with number = 3. Then with 2 and then with 2.
hence answer = 3 + 2 + 2 = 7.
Explanation 2:
You give both tickets from the row with 4 seats. 4 + 3 = 7.
Min Steps in Infinite Grid
Problem Description
You are in an infinite 2D grid where you can move in any of the 8 directions
(x,y) to
(x-1, y-1),
(x-1, y) ,
(x-1, y+1),
(x , y-1),
(x , y+1),
(x+1, y-1),
(x+1, y) ,
(x+1, y+1)
You are given a sequence of points and the order in which you need to
cover the points.. Give the minimum number of steps in which you can
achieve it. You start from the first point.
NOTE: This question is intentionally left slightly vague. Clarify the question
by trying out a few cases in the “See Expected Output” section.
Input Format
Given two integer arrays A and B, where A[i] is x coordinate and B[i] is y
coordinate of ith point respectively.
Output Format
Return an Integer, i.e minimum number of steps.
Example Input
Input 1:
A = [0, 1, 1]
B = [0, 1, 2]
Example Output
Output 1:
2
Problem solving 3
Example Explanation
Explanation 1:
Given three points are: (0, 0), (1, 1) and (1, 2).
It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move
from (1, 1) to (1, 2).
Stairs
You are climbing a stair case and it takes A steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can
you climb to the top?
Input Format:
The first and the only argument contains an integer A, the number of steps.
Output Format:
Return an integer, representing the number of ways to reach the top.
Constrains:
1 <= A <= 36
Example :
Input 1:
A = 2 Output 1:
2 Explanation 1:
[1, 1], [2] Input 2:
A = 3 Output 2:
3 Explanation 2:
[1 1 1], [1 2], [2 1]
Problem solving 4