 # Programming Question: Leetcode-17

Approach to solve question leetcode — 17 Letter Combinations of a Phone Number

1. For solving the problem recursively, we need helper function , in which digits, result and empty string (current string) will be passed as well as there will be a function which will return corresponding characters for each digit.
2. for each number in digits , we will get possible characters of that number (for 2 ‘abc’ , for 3 ‘def’ like that).
3. We will add each possible character into the current string and call the function recursively starting form next digit.
4. then we cut the current string from end otherwise it will be added in next steps recursively.

Code:

--

--

# Programming Question: Leetcode-16

Approach to solve leetcode question- 16 3Sum Closest

1. This question is somewhat similar to previous question leetcode -15 3Sum with a small change.

2. In this solution , we will take each element of array nums , and then for each element we will take two elements ahead of that element. Find the total of all three and compare which one is closer to the target.

3.return the closest distance from target.

Code :

--

--

# Programming Question: Leetcode-15

Approach to solve leetcode question-15 3Sum

We can get all combinations of 3 elements and see their sum is equal to 0 or not and return all combinations which sums to 0, but this approach will not be efficient.

Instead we will use following approach:

1. we will sort the array first.

2. we will take each element of array and for elements after this , we will choose two elements , calculate sum of all elements and if it is 0 , add all elements in result array . It will be clear in code.

Code :

--

--

# Programming Question : Leetcode 11

Approach to solve leetcode 11 — Container With Most Water

1. In this question, we are asked to calculate area between two bars, that will be calculated as (distance between bars)*(height of smaller bar).
2. we will start two pointers one from start and other from end , and keep calculating area between the bars. Now, as we need smaller bars(because water can be filled up to the height of smaller bar) so if right side bar is smaller , we will decrement its pointer and if left side bar is smaller we will decrement its pointer until start is less than or equal to end.
3. at the end we will return maximum area .

Code:

--

--