Given an array arr of size N and an integer K, the task is to print the array after moving all value equal to K at the end of the array.
Input: arr = [2, 1, 2, 2, 2, 3, 4, 2], K = 2
Output: [4, 1, 3, 2, 2, 2, 2, 2]
2 is the number which has to be moved to the end of the array arr. Therefore, after making the change the array is [4, 1, 3, 2, 2, 2, 2, 2]. The numbers 4, 1, and 3 could be ordered differently.
Input: arr = [1, 1, 3, 5, 6], K = 1
Output: [6, 5, 3, 1, 1 ]
1 is the number which has to be moved to the end of the array arr. Therefore, after making the change the array is [6, 5, 3, 1, 1 ].
Approach: To solve the problem mentioned above we use Two Pointer Technique.
- Initialize two pointers where the left pointer marks the start of the array and the other one that is right one marks the end of the array, respectively.
- Decrement the count of right pointer long as it points to K, and increment the left pointer as long as it doesn’t point to the integer m.
- When both pointers aren’t moving, swap their values in place.
- Repeat this process until the pointers pass each other.
Below is the implementation of the above approach:
6 5 3 1 1
Time complexity: O(N), where N is the length of the array.
Space complexity: O(1)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Replace two consecutive equal values with one greater
- Counting values greater than equal to x after increments
- Move all zeroes to end of array
- Find Partition Line such that sum of values on left and right is equal
- Move all zeroes to end of array using Two-Pointers
- Move all zeroes to end of array | Set-2 (Using single traversal)
- Move all zeros to start and ones to end in an Array of random integers
- Move all zeroes to end of array using List Comprehension in Python
- Queries to search an element in an array and move it to front after every query
- Count minimum number of "move-to-front" moves to sort an array
- Permutation of an array that has smaller values from another array
- Find an element in array such that sum of left array is equal to sum of right array
- Count elements such that there are exactly X elements with values greater than or equal to X
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Sort an array which contain 1 to n values
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Number of subsets with same AND, OR and XOR values in an Array
- Sum of decomposition values of all suffixes of an Array
- Possible to form a triangle from array values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.