Write a C code of O(n) order to remove all the odd numbers from the array. Example: the
array contains 10, 2, 3, 7, 8, 6, 11. The output should be 10, 2, 8, and 6
#include <stdio.h>
#define SIZE 10
// size - original size of array; will be updated after remnoving odd integers
void RemoveOdd(int *arr, int *size) {
int sz = *size;
for (int i = 0, j = 0; i < *size; i++) {
if (arr[i] % 2 == 0) {
arr[j] = arr[i];
j++;
sz--;
}
}
*size = sz;
}
int main() {
int arr[SIZE];
for (int i = 0; i < SIZE; i++) arr[i] = i + 1;
int size = SIZE;
RemoveOdd(arr, &size);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Comments
Leave a comment