Example 1: Input: hello Output: holle Example 2: Input: prodevelopertutorial Output: pradivolupertotereol We can solve this problem in 2 methods. Method 1: By using STL functions. In this method we use “find_first_of” and “find_last_of” find_first_of(“characters_to_search_for”, start_from_index) : It will return the position of the first …
Example: Array = {1, 2, 3, 4, 5 ,6, 7}; n = 4 Output: Explanation: rotate 1 steps to the right: rotate 2 steps to the right: rotate 3 steps to the right: rotate 4 steps to the right: This problem can be solved in 2 …
The complexity of the operations should be as follows: * Insertion of an element – O(1) * Deletion of an element – O(1) * Finding an element – O(1) According to question, we need to insert, delete and find the element in constant time. This …
Example: Input: 4->2->1->3 Output: 1->2->3->4 Insertion sort is a comparison based sorting algorithm. In this algorithm we divide the list into 2 parts. The left most part is sorted part, and the right most part is unsorted part. In insertion sort, after each pass at least …
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… Example 1: Given 1->2->3->4, reorder it to 1->4->2->3. The algorithm will works as shown below: Step 1: Find the middle of the list. Step 2: Reverse the second half of the list. Step 3: …
Example: Input: words = maxWidth = 16 Output: This question can be divided into 2 different parts. In the first part, we need to check how many words can be fit into a single line. In the second part, determine the space to be inserted between …
In the previous question, we wanted to know if there is a cycle. But in this question we want to know the node from where the cycle begins. For example: 1 -> 2 -> 3 ->4 ->3 Entrance of the cycle is 3. The steps …
Solution Explanation: Take an extra pointer “fast” and assign its starting point to head. Every iteration moves the “fast” pointer 2 steps forward and “head” pointer 1 step forward. At certain point, if there is a cycle, both “head” and “fast” pointer will meet at the …
Example 1: Input: s = "applepenapple", wordDict = Output: true Explanation: "applepenapple" can be seperated as "apple pen apple". This problem can be solved by using DP . We shall have a look at the code, before explaining the code. Solution in C++ #include<iostream> …