Example: Input: Output: , , , , , ] Before solving this problem, have a look at similar kind of problem Subsets. Similar to the subset 1 problem, we can solve this problem by 2 ways. Iterative Recursive/ Backtrack. We shall discuss both of the solutions …
Example 1: Input: 2 Output: Explanation: 00 - 0 01 - 1 11 - 3 10 - 2 Before solving this problem, let us understand what grey code is. Before knowing about grey code, we shall understand about the binary format. Below are some numbers and …
Example: Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5 Solution: Let us understand the question first. The question has 2 parts: Part 1: Here we need to partition the list in such a way that the nodes less than “x” should come to left and …
Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Solution: For the solution, we take 2 nodes a “dummy” node and a “current” node. Use “dummy” node, in case if the head is needed to be deleted. “current” node is used to iterate throughout the list. So in …
Example 1: Input: nums = , key = 0 Output: true Before solving this problem, please look at the similar problem. Search in Rotated Sorted Array. We achieve this by below 2 lines: while (low < high && array == array) low++; // skip duplicates from …
Each element can appear only once or twice. Example 1: Given nums = The function should return 6, because . As each element can appear only twice. The solution is simple. We shall 2 variables, one will hold the new length and other is used to …
This problem is to place n number of queens on a chessboard such that, no two queens attack each other. Return all the solutions. Example: Input: 4 Output: , ] Introduction: A queen in a chess board can move it her left, right and diagonal. …
Given a 2D matrix and some words, return true of the letters are constructed with the combination cells that are horizontally or vertically adjacent to each other. Example: Matrix = , , ] Given word = "ABCCED", return true. Given word = "ABCD", return false. This …
Note: The solution set must not contain duplicate subsets. Example: Input: nums = Output: , , , , , , , ] The problem can be solved in 2 ways: Iterative Backtracking Solution 1: Iterative Solution. In this solution, …
Example: Input: n = 4, k = 2 Output: , , , , , , ] This problem can be solved in 2 ways. Iterative Recursion/Backtrack Solution 1: Iterative approach: In the iterative solution, we take a temp array of size “k”. Then we move to the …