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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Move all zeroes to end of array
- Move all zeroes to end of array using List Comprehension in Python
- 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 Two-Pointers
- Move all negative elements to end in order with extra space allowed
- Move all negative numbers to beginning and positive to end with constant extra space
- Double the first element and move zero to end
- Maximum sum subarray such that start and end values are same
- Split an Array A into Subsets having equal Sum and sizes equal to elements of Array B
- Sort first k values in ascending order and remaining n-k values in descending order
- Sort a String in decreasing order of values associated after removal of values smaller than X
- Count minimum number of "move-to-front" moves to sort an array
- Queries to search an element in an array and move it to front after every query
- Largest subsequence such that all indices and all values are multiples individually
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Count minimum right flips to set all values in an array
- Sum of values of all possible non-empty subsets of the given array
- Product of values of all possible non-empty subsets of given Array
- Find the original Array using XOR values of all adjacent elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.