**Example:**

Given Input: [1, 2] Output: 3 1 2 0 Explanation: subsets are [0], [1], [2], [1, 2] Given Input: [2, 4, 6] 12 6 8 2 10 4 6 0

**Approach: Recursion**

Every element of the array has two choices, whether to include that element in the subset or exclude that element from the subset. So initialize sum = 0, now for each element in the array – add it to sum and make a recursive call, do not add it to sum and make a recursive call. Print the sum at the base case when the current element is the last element of an array.

**Time Complexity:** O(2^N)

**Complete Code:**

**Output:**

Given Input: [2, 4, 6] 12 6 8 2 10 4 6 0]]>

**Input: **Given a list of routing ranges for each bank, with a start number, end number, and bank name. For instance, range = [1001, 1005, BOFA] has to read as routing numbers from 1001 to 1005 belong to bank BOFA.

**Example-**

Input: [[123400,123500, BOFA], [123600,123700, WELLS FARGO], [12538,125480, GCU], [126100,126500, CHASE]] Routing number: 123650 belongs to bank WELLS FARGO Routing number: 12540 belongs to bank GCU Routing number: 126050 does not belong to any bank.

**Approach:**

Idea is to sort the given ranges as per the start number and then find the ** largest range** with a start is smaller than given

**Complete Code:**

**Output:**

Input: [[123400,123500, BOFA], [123600,123700, WELLS FARGO], [12538,125480, GCU], [126100,126500, CHASE]] Given routing number: 123650 belongs to bank WELLS FARGO Given routing number: 12540 belongs to bank GCU Given routing number: 126050 does not belong to any bank.

Reference: https://www.careercup.com/question?id=5669049954992128

**Example:**

Coordinates - [(2,5), (5,7),(1,4)] and interval = (1,6) Return true Explanation - Points 1 to 6 lies in list of interval given 1 to 4. 2 to 5 and 5 to 7. Coordinates - [(1,4),(6,7),(2,5)] and interval - (1,6) Return false Explanation - Distance between 5 to 6 is not covered in the list given so return false

**Approach: **

**Brute Force:**

Iterate through all the coordinates and for each coordinate compare the a and end with all other intervals and merge them if overlapping.

Time Complexity: **O(N^2)**

**Better Solution – Sorting. **

- Sort the given coordinates in ascending order of their start
.*x1* - Push first coordinate to stack.
- Iterate through the rest of the coordinates, for each
–*current coordinate*- Pop the top coordinate form the stack. Let’s call it
.*previous* - Check if
is greater than*x2 of previous*(means*x1 for current*is started before the*current coordinate*was ended, so these two coordinates can be merged). If yes then update the*previous coordinate*to*x2 of previous*, else just push the*x2 of current coordinate*to stack (no need to check the*current coordinate*with other coordinates because coordinates are sorted in ascending order).*previous coordinate*

- Pop the top coordinate form the stack. Let’s call it
- Now all the possible coordinates are merged.
- Pop coordinates from stack one at a time and checks if given interval lies within the range of popped coordinates, return true.
- If the last step is executed and no coordinates were found where given interval lies in the range, return false.

Time Complexity: **O(nlogn)**

**Complete Code:**

**Output:**

Given Coordinates: [[2,5], [5,7], [1,4]] Given Interval: [1,6] Given interval is COVERED in coordinates ------------------------------------------------------- Given Coordinates: [[1,7], [2,5], [5,7]] Given Interval: [1,8] Given interval is NOT COVERED in coordinates]]>

This problem can also be asked as “*Given a permutation of numbers you need to find the next smaller premutation OR largest permutation which is smaller than the given permutation*”

**Note:** In the case given permutation is smallest, return the given permutation.

**Example:**

Given Array: [1, 7, 3, 4, 5] Largest permutation smaller than a given array: [1, 5, 3, 4, 7] Given Array: [1, 2, 3, 4, 5] Original Array is already the smallest permutation: [1, 2, 3, 4, 5] Given Array: [4, 2, 5, 6] Largest permutation smaller than a given array: [2, 4, 5, 6]

**Approach:**

- We need to find the two numbers so that swapping these numbers will produce the permutation which is largest but smaller than the given permutation.
- Let’s say these two elements are
and*first_number*.*second_number* - Iterate the given array from right to left and find the first index where the left element is greater than the right element. Once found, the element at the left index will be our
. If no such index were found means given array is smaller permutation possible, in that case, return the given array.*first_number* - Now find the maximum element (which is smaller than first_number) from the right element(found in the previous step) to the end of the array. This maximum element will be our
.*second_number* - Swapping
and*first_number*will be our answer for permutation which is largest but smaller than the given permutation.*second_number* - See the walk-through of an example below

Iterate the given array from right to left and find the first index where left element is greater than the right element. So in the given array 7>3. SoGiven Array: [1, 7, 3, 4, 5]Now find the maximum element from 3, 4, 5which is smaller thanfirst_number = 7., which is 5. So ourfirst_number = 7Swap 5 and 7 impliessecond_number = 5.updated array:which is the largest but smaller than the given permutation [1, 7, 3, 4, 5][1, 5, 3, 4, 7].

Time Complexity: **O(N)**

**Complete Code:**

**Output:**

Given Array: [1, 7, 3, 4, 5] Largest permutation smaller than given array: [1, 5, 3, 4, 7]]]>

**Conditions: **

- All K numbers must be between 1 and 9 and unique.
- All numbers in K are positive.

**Example:**

N= 5 K=2 Output: [1, 4] [2, 3] N =12 K=3 Output: [1, 2, 9] [1, 3, 8] [1, 4, 7] [1, 5, 6] [2, 3, 7] [2, 4, 6] [3, 4, 5]

**Approach: Use Recursion**

- Given
and*N*.*K* - Start with
= 0,*sum*= 1,*start*=null*combinationList* - Iterate through
=*i*to*start*.*9*- Add
to*i*.*sum* - Put
to*i**combinationList.* - Make a recursive call with
and*K-1*(to avoid duplicates) and*start = start + 1**combinationList.* - In tail recursion, backtrack and remove i from the
to find more solutions*combinationList* - Base case: If
, check if*K=0**sum=N*.*combinationList*

- Add

**Complete Code:**

**Output:**

N = 12 K = 3 [1, 2, 9] [1, 3, 8] [1, 4, 7] [1, 5, 6] [2, 3, 7] [2, 4, 6] [3, 4, 5]]]>

Flip all the bits of a number to get the complement of that number.

**Example: **

N = 8 Output: 7 Explanation: N = 8, binary representation: 1 0 0 0 Flip all the bits = 0 1 1 1 => 7 ____________________________________ N = 13 Output: 2 Explanation: N = 13, binary representation: 1 1 0 1 Flip all the bits = 0 0 1 0 => 2 ____________________________________

**Approach: Power of 2**

- Let’s say given number is N.
- Take x = 1, power = 1.
- while x < N, do power = power * 2 and x = x + power.
- result = x – N.

**Example:** N = 8

x = 1, power = 1, x < 8 power = power * 2 = 1 * 2 = 2, x = x + power = 1 + 2 = 3 x = 3, power = 2, x < 8 power = power * 2 = 2 * 2 = 4, x = x + power = 3 + 4 = 7 x = 7, power = 7, x < 8 power = power * 2 = 4 * 2 = 8, x = x + power = 7 + 8 = 15 x = 15, Now x > 8 result = x - N => 15 - 8 = 7

**Code:**

**Output:**

N = 10, Complement: 5

**Approach: XOR + Most Significant Bit**

- Let’s say given number is N.
- Create a mask (it would be all 1’s) for the number N.
- Do mask^N will produce the complement of N.

**Mask: **The XOR returns TRUE (1) only when one of the bits is true, else return FALSE (0). This means, 1^1=0 and 0^0=0. If we XOR 1 and 0 bit, we will get 1. Thus effectively flipping the bits. That’s the reason we need mask will all 1’s.

**Explanation: **

Let’s say N = 1 0 1 0

mask = 1 1 1 1

Complement = N^mask = 0 1 0 1

**How to create Mask:**

*Method 1: *

- Find the most significant bit location (say it is
*msb*) and take x = 1. - while msb>0, left shift x by 1 and do x = x OR 1 (this will put 1 at that position).

Example: N = 1 0 1 0, most significant bit = 3 while(3>0) msb = 3,x = 1, x << 1 => 1 0 then 1 0 OR 1 => 1 1 msb = 2, x = 1 1, x<<1 => 1 1 0 then 1 1 0 OR 1 => 1 1 1 msb = 1, x = 1 1 1, x<<1 => 1 1 1 0 then 1 1 1 0 OR 1 => 1 1 1 1 msb = 0, mask = 1 1 1 1 x = 1, left shift by 3 => x = 1 0 0 0 0 Mask = x - 1 = 1 1 1 1

**Method 2:** Find the highest set bit. left shift by 1 and then subtract 1 from it.

Example: N = 1 0 1 0, highest set bit= 1 0 0 0 left shift by 1 => 1 0 0 0 0 Mask = x - 1 = 1 1 1 1

**Code:**

**Output:**

N = 10, Complement: 5 N = 10, Complement: 5]]>

**Example:**

Given Array: [8, 3, 5, 1, -4, -8], required sum: 0 Output: [-8, -4, 1, 3, 8] [-8, 3, 5] [-8, 8] [-4, 1, 3]

Given Array: [3, 1, -4, 2, 0], required sum: 0 Output: [-4, 0, 1, 3] [-4, 1, 3] [0]

**Approach:**

- Given
.*arrA[]* - Sort the
in ascending order.*arrA[]* - Start with
= 0,*currSum*= 0,*startIndex*=null*combinationList* - Iterate through
=*i*to*startIndex*.*length(arrA[])*- Add
to*arrA[i]*.*currSum* - Put
to*arrA[i]**combinationList.* - Make a recursive call with
(to process next elements) and*startIndex=i+1**combinationList.* - In tail recursion, backtrack and remove
from the*arrA[i]*to find more solutions*combinationList* - Base case: if
and*currSum=0**combinationList is not empty*.*combinationList*

- Add

**Complete Code:**

**Output:**

Given Array: [8, 3, 5, 1, -4, -8], required sum: 0 [-8, -4, 1, 3, 8] [-8, 3, 5] [-8, 8] [-4, 1, 3]

**Reference:** https://www.careercup.com/question?id=6130274301640704

There are N people in a social network and there are friends group. Each person can be part of a friend group of a specific size.

Number of people in the social network – N

An array of size N specifying group size for each person. – (g0, g2, g3,….., gn). The first person (at index 0) is part of a friend group of maximum size g0, the second person(at index 1) is part of a friend group of maximum size g2 and so on.

**Objective:** Write an algorithm to create the minimum number of friend groups so that each person can be put into one of these groups as per the given group size of each person.

**Output: **Print minimum no of groups and persons assigned to each group.

**Example:**

N = 7 people = {3, 3, 3, 3, 3, 1, 3} Person 0 is being part of a group of maximum size 3, person 1 is be to part of a group of maximum size 3 ..., person 5 is be to part of a group of maximum size 1. Output: 5 0 1 2 3 4 6 ************ N = 2 people = {3, 2} Person 0 is being part of a group of maximum size 3, person 1 is be to part of a group of maximum size 2 Output: 0 1 Note: person 0 can be part of maximum group size 3, So this person can be part of a group size 2 where person 1 belongs. So minimum no of groups required are 1 in which both person 0 and person 1 can be together. ************ N = 9 people = {3, 2, 3, 3, 2, 2, 3, 2, 6}; Output: 1 4 5 7 0 2 3 6 8

**Approach: **

- Iterate through given array.
- Create a Map for each group size present, with group size as key and person’s index as the value in the map.
- Now start forming the group, Iterate through map, lowest key first (will start forming smallest group first since person who has to be part of a smaller group, cannot be part of a bigger group if needed and vice versa is true) and start putting persons in a group size (same as key). for ease create TreeMap which will have keys in sorted order.
- Pull the person from higher group size to lower If needed to complete the group.
- See the walkthrough of the example below for more understanding.

N = 7 groups = {3, 3, 1, 3, 2, 3} Tree Map:key(Group Size)values(Persons)1 2 2 4 3 0 1 3 5 First group: size 1, person 2 Second group: size 2, person- 4, (put person 0 in this group as well to complete the group, this way the total number of groups can be minimized) Third group: size 3, person: 1 3 Result Group size: 1, Persons: Group size: 2, Persons: 4 Group size: 3, Persons: 1 3

**Complete Code:**

**Output:**

Group size: 1, Persons: 2 Group size: 2, Persons: 4 0 Group size: 3, Persons: 1 3 5 ______________________________________________ Group size: 2, Persons: 1 4 Group size: 2, Persons: 5 7 Group size: 3, Persons: 0 2 3 Group size: 3, Persons: 6 8]]>

**Example: **

**Approach:**

**For Undirected Graph – **It will be a spanning tree (read about spanning tree) where all the nodes are connected with no cycles and adding one more edge will form a cycle. In the spanning tree, there are V-1 edges.

**For Directed Graph –** Construct the graph similar to topological order (Read about topological order – where all the edges go to one direction and there will not be any circular dependency, means there is no cycle). Take the first vertex and have a directed edge to all the other vertices, so V-1 edges, second vertex to have a directed edge to rest of the vertices so V-2 edges, third vertex to have a directed edge to rest of the vertices so V-3 edges, and so on. This will construct a graph where all the edges in one direction and adding one more edge will produce a cycle.

So the total number of edges = (V-1) + (V-2) + (V-3) +———+2+1 = V(V-1)/2. (sum of first N natural numbers is N(N+1)/2)

**Complete Code:**

**Output:**

Maximum edges to make Acyclic Undirected Graph with 3 vertices are: 2 Maximum edges to make Acyclic Directed Graph with 3 vertices are: 3 --------------------------------- Maximum edges to make Acyclic Undirected Graph with 4 vertices are: 3 Maximum edges to make Acyclic Directed Graph with 4 vertices are: 6 --------------------------------- Maximum edges to make Acyclic Undirected Graph with 5 vertices are: 4 Maximum edges to make Acyclic Directed Graph with 5 vertices are: 10]]>

**Prefix notation** is a notation for writing arithmetic expressions in which the operands appear after their operators. Let’s assume the below

- Operands are real numbers (could be multiple digits).
- Permitted operators: +,-, *, /, ^(exponentiation)
- Blanks are used as a
**separator**in expression. - Parenthesis are permitted

**Example:**

Postfix: + 500 40 Output: 540 Explanation: Infix expression of the above prefix is: 500 + 40 which resolves to 540 Postfix: - / * 20 * 50 + 3 6 300 2 Output: 28 Explanation: Infix expression of above prefix is: 20 * (50 *(3+6))/300-2 which resolves to 28

**Approach**: Use Stack

**Algorithm:**

Reverse the given expression and Iterate through it, one character at a time

- If the character is a digit, initialize String temp;
- while the next character is not a digit
- do temp = temp + currentDigit

- convert Reverse temp into Number.
- push Number to the stack.

- while the next character is not a digit
- If the character is an operator,
- pop the operand from the stack, say it’s s1.
- pop the operand from the stack, say it’s s2.
- perform
**(s1 operator s2)**and push it to stack.

- Once the expression iteration is completed, The stack will have the final result. Pop from the stack and return the result.

Please see the walkthrough of an example below for more understanding.

**Complete Code:**

**Output:**

Prefix Expression: - / * 20 * 50 + 3 6 300 2 Evaluation: 28.0]]>