According to the comments:
Is there any value that will never be displayed in arr , but int can be represented?
You can accept this as an int max .
Now you can use removeIndices
#include<iostream> #include<limits> int removeIndices(int* arr, int n, int* indices, int m){ const int NEVER_IN_ARRAY = std::numeric_limits<int>::max(); for(int i = 0; i < m; i++) arr[indices[i]] = NEVER_IN_ARRAY; for(int from = 0, to = 0; from < n; from++) if(arr[from] != NEVER_IN_ARRAY) arr[to++] = arr[from]; return n - m; } int main(){ int arr[] = {12, 5, 10, 7, 4, 1, 9}, n = 7, indices[] = {2, 3, 5}, m = 3; int newSize = removeIndices(arr, n, indices, m); for(int i = 0; i < newSize; i++) std::cout << arr[i] << " "; return 0; }
Edit: Using
#include<algorithm> #include<functional>
We can do it:
int removeIndices(int* arr, int n, int* indices, int m){ const int NEVER_IN_ARRAY = std::numeric_limits<int>::max(); std::for_each(indices, indices + m, [arr](int index){ arr[index] = NEVER_IN_ARRAY; }); int* p = std::remove_if(arr, arr + n, std::bind2nd(std::equal_to<int>(), NEVER_IN_ARRAY)); return p - arr; }