Move all values equal to K to the end of the Array
Last Updated :
02 Aug, 2022
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.
Examples:
Input: arr = [2, 1, 2, 2, 2, 3, 4, 2], K = 2
Output: [4, 1, 3, 2, 2, 2, 2, 2]
Explanation:
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 ]
Explanation:
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:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > moveElementToEnd(vector< int > array, int toMove)
{
int i = 0;
int j = array.size() - 1;
while (i < j) {
while (i < j && array[j] == toMove)
j--;
if (array[i] == toMove)
swap(array[i], array[j]);
i++;
}
return array;
}
int main( int argc, char * argv[])
{
vector< int > arr = { 1, 1, 3, 5, 6 };
int K = 1;
vector< int > ans = moveElementToEnd(arr, K);
for ( int i = 0; i < arr.size(); i++)
cout << ans[i] << " " ;
return 0;
}
|
C
#include <stdio.h>
void swap( int * xp, int * yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void moveElementToEnd( int array[], int toMove, int arr_size)
{
int i = 0;
int j = arr_size - 1;
while (i < j) {
while (i < j && array[j] == toMove)
j--;
if (array[i] == toMove)
swap(&array[i], &array[j]);
i++;
}
for ( int i = 0; i < arr_size; i++)
printf ( "%d " , array[i]);
}
int main()
{
int arr[] = { 1, 1, 3, 5, 6 };
int K = 1;
int arr_size = sizeof (arr) / sizeof (arr[0]);
moveElementToEnd(arr, K, arr_size);
return 0;
}
|
Java
class GFG{
static int [] moveElementToEnd( int []array,
int toMove)
{
int i = 0 ;
int j = array.length - 1 ;
while (i < j)
{
while (i < j && array[j] == toMove)
j--;
if (array[i] == toMove)
swap(array, i, j);
i++;
}
return array;
}
static int [] swap( int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void main(String[] args)
{
int []arr = { 1 , 1 , 3 , 5 , 6 };
int K = 1 ;
int []ans = moveElementToEnd(arr, K);
for ( int i = 0 ; i < arr.length; i++)
System.out.print(ans[i] + " " );
}
}
|
Python3
def moveElementToEnd(array, toMove):
i = 0
j = len (array) - 1
while (i < j):
while (i < j and array[j] = = toMove):
j - = 1
if (array[i] = = toMove):
array[i], array[j] = array[j] , array[i]
i + = 1
return array
if __name__ = = "__main__" :
arr = [ 1 , 1 , 3 , 5 , 6 ]
K = 1
ans = moveElementToEnd(arr, K)
for i in range ( len (arr)):
print (ans[i] ,end = " " )
|
C#
using System;
class GFG{
static int [] moveElementToEnd( int []array,
int toMove)
{
int i = 0;
int j = array.Length - 1;
while (i < j)
{
while (i < j && array[j] == toMove)
j--;
if (array[i] == toMove)
swap(array, i, j);
i++;
}
return array;
}
static int [] swap( int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void Main( string [] args)
{
int []arr = { 1, 1, 3, 5, 6 };
int K = 1;
int []ans = moveElementToEnd(arr, K);
for ( int i = 0; i < arr.Length; i++)
Console.Write(ans[i] + " " );
}
}
|
Javascript
<script>
function moveElementToEnd(array, toMove)
{
let i = 0;
let j = array.length - 1;
while (i < j)
{
while (i < j && array[j] == toMove)
j--;
if (array[i] == toMove)
swap(array, i, j);
i++;
}
return array;
}
function swap(arr, i, j)
{
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
let arr = [ 1, 1, 3, 5, 6 ];
let K = 1;
let ans = moveElementToEnd(arr, K);
for (let i = 0; i < arr.length; i++)
document.write(ans[i] + " " );
</script>
|
Time complexity: O(N), where N is the length of the array.
Space complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...