Remove all the prime numbers from the given array
Given an array arr[] of N integers, the task is to remove all the prime numbers.
Examples:
Input: arr[] = {4, 6, 5, 3, 8, 7, 10, 11, 14, 15}
Output: 4 6 8 10 14 15Input: arr[] = {2, 4, 7, 8, 9, 11}
Output: 4 8 9
Approach: Traverse the array and check if the current number is prime, if it is then left shift all the elements after it to remove this prime number and decrease the value of the array length. Repeat this for all the elements of the array. To check the number is prime or not, use Sieve of Eratosthenes to generate all the primes.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;const int sz = 1e5;bool isPrime[sz + 1];// Function for Sieve of Eratosthenesvoid sieve(){ memset(isPrime, true, sizeof(isPrime)); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= sz; i++) { if (isPrime[i]) { for (int j = i * i; j < sz; j += i) { isPrime[j] = false; } } }}// Function to print the elements of the arrayvoid printArray(int arr[], int len){ for (int i = 0; i < len; i++) { cout << arr[i] << ' '; }}// Function to remove all the prime numbersvoid removePrimes(int arr[], int len){ // Generate primes sieve(); // Traverse the array for (int i = 0; i < len; i++) { // If the current element is prime if (isPrime[arr[i]]) { // Shift all the elements on the // right of it to the left for (int j = i; j < len; j++) { arr[j] = arr[j + 1]; } // Decrease the loop counter by 1 // to check the shifted element i--; // Decrease the length len--; } } // Print the updated array printArray(arr, len);}// Driver codeint main(){ int arr[] = { 4, 6, 5, 3, 8, 7, 10, 11, 14, 15 }; int len = sizeof(arr) / sizeof(int); removePrimes(arr, len); return 0;} |
Java
// Java implementation of the approachclass GFG{static int sz = (int) 1e5;static boolean []isPrime = new boolean[sz + 1];// Function for Sieve of Eratosthenesstatic void sieve(){ for (int i = 0; i < sz + 1; i++) isPrime[i] = true; isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= sz; i++) { if (isPrime[i]) { for (int j = i * i; j < sz; j += i) { isPrime[j] = false; } } }}// Function to print the elements of the arraystatic void printArray(int arr[], int len){ for (int i = 0; i < len; i++) { System.out.print(arr[i] + " "); }}// Function to remove all the prime numbersstatic void removePrimes(int arr[], int len){ // Generate primes sieve(); // Traverse the array for (int i = 0; i < len; i++) { // If the current element is prime if (isPrime[arr[i]]) { // Shift all the elements on the // right of it to the left for (int j = i; j < len-1; j++) { arr[j] = arr[j + 1]; } // Decrease the loop counter by 1 // to check the shifted element i--; // Decrease the length len--; } } // Print the updated array printArray(arr, len);}// Driver codepublic static void main(String[] args){ int arr[] = { 4, 6, 5, 3, 8, 7, 10, 11, 14, 15 }; int len = arr.length; removePrimes(arr, len);}} // This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approachsz = 10**5isPrime = [True for i in range(sz + 1)]# Function for Sieve of Eratosthenesdef sieve(): isPrime[0] = isPrime[1] = False i = 2 while i * i < sz: if (isPrime[i]): for j in range(i * i, sz, i): isPrime[j] = False i += 1# Function to print the elements of the arraydef prArray(arr, lenn): for i in range(lenn): print(arr[i], end = " ")# Function to remove all the prime numbersdef removePrimes(arr, lenn): # Generate primes sieve() # Traverse the array i = 0 while i < lenn: # If the current element is prime if (isPrime[arr[i]]): # Shift all the elements on the # right of it to the left for j in range(i, lenn - 1): arr[j] = arr[j + 1] # Decrease the loop counter by 1 # to check the shifted element i -= 1 # Decrease the length lenn -= 1 i += 1 # Print the updated array prArray(arr, lenn)# Driver codeif __name__ == '__main__': arr = [4, 6, 5, 3, 8, 7, 10, 11, 14, 15] lenn = len(arr) removePrimes(arr, lenn)# This code is contributed by Mohit Kumar |
C#
// C# implementation of the approachusing System;class GFG{static int sz = (int) 1e5;static bool []isPrime = new bool[sz + 1];// Function for Sieve of Eratosthenesstatic void sieve(){ for (int i = 0; i < sz + 1; i++) isPrime[i] = true; isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= sz; i++) { if (isPrime[i]) { for (int j = i * i; j < sz; j += i) { isPrime[j] = false; } } }}// Function to print the elements// of the arraystatic void printArray(int []arr, int len){ for (int i = 0; i < len; i++) { Console.Write(arr[i] + " "); }}// Function to remove// all the prime numbersstatic void removePrimes(int []arr, int len){ // Generate primes sieve(); // Traverse the array for (int i = 0; i < len; i++) { // If the current element is prime if (isPrime[arr[i]]) { // Shift all the elements on the // right of it to the left for (int j = i; j < len - 1; j++) { arr[j] = arr[j + 1]; } // Decrease the loop counter by 1 // to check the shifted element i--; // Decrease the length len--; } } // Print the updated array printArray(arr, len);}// Driver codepublic static void Main(String[] args){ int []arr = { 4, 6, 5, 3, 8, 7, 10, 11, 14, 15 }; int len = arr.Length; removePrimes(arr, len);}}// This code is contributed by PrinciRaj1992 |
Javascript
<script>// Javascript implementation of the approachconst sz = 1e5;let isPrime = new Array(sz + 1);// Function for Sieve of Eratosthenesfunction sieve(){ isPrime.fill(true) isPrime[0] = isPrime[1] = false; for(let i = 2; i * i <= sz; i++) { if (isPrime[i]) { for(let j = i * i; j < sz; j += i) { isPrime[j] = false; } } }}// Function to print the elements of the arrayfunction printArray(arr, len){ for(let i = 0; i < len; i++) { document.write(arr[i] + ' '); }}// Function to remove all the prime numbersfunction removePrimes(arr, len){ // Generate primes sieve(); // Traverse the array for(let i = 0; i < len; i++) { // If the current element is prime if (isPrime[arr[i]]) { // Shift all the elements on the // right of it to the left for(let j = i; j < len; j++) { arr[j] = arr[j + 1]; } // Decrease the loop counter by 1 // to check the shifted element i--; // Decrease the length len--; } } // Print the updated array printArray(arr, len);}// Driver codelet arr = [ 4, 6, 5, 3, 8, 7, 10, 11, 14, 15 ];let len = arr.length;removePrimes(arr, len);// This code is contributed by _saurabh_jaiswal</script> |
Output:
4 6 8 10 14 15


