Find prime numbers in the first half and second half of an array

Given an array arr of size N. The task is to find the prime numbers in the first half (up to index N/2) and the second half (all the remaining elements) of an array.
Examples:Â
Input : arr[] = {2, 5, 10, 15, 17, 21, 23 }Â
Output :2 5 and 17 23Â
Prime numbers in the first half of an array are 2, 5 and in the second half are 17, 23
Input : arr[] = {31, 35, 40, 43}Â
Output :31 and 43Â
Approach:Â
First Traverse the array up to N/2 and check all the elements whether they are prime or not and print the prime numbers. Then Traverse the array from N/2th element till N and find whether elements are prime or not and print all those elements which are prime.
Below is the implementation of the above approach:Â Â
C++
// C++ program to print the prime numbers in the// first half and second half of an array#include <bits/stdc++.h>using namespace std;Â
// Function to check if a number is prime or notbool prime(int n){Â Â Â Â for (int i = 2; i * i <= n; i++)Â Â Â Â Â Â Â Â if (n % i == 0)Â Â Â Â Â Â Â Â Â Â Â Â return false;Â
    return true;}Â
// Function to find whether elements are prime or notvoid prime_range(int start, int end, int* a){    // Traverse in the given range    for (int i = start; i < end; i++) {Â
        // Check if a number is prime or not        if (prime(a[i]))            cout << a[i] << " ";    }}Â
// Function to print the prime numbers in the// first half and second half of an arrayvoid Print(int arr[], int n){Â
    cout << "Prime numbers in the first half are ";    prime_range(0, n / 2, arr);    cout << endl;Â
    cout << "Prime numbers in the second half are ";    prime_range(n / 2, n, arr);    cout << endl;}Â
// Driver Codeint main(){Â
    int arr[] = { 2, 5, 10, 15, 17, 21, 23 };Â
    int n = sizeof(arr) / sizeof(arr[0]);Â
    // Function call    Print(arr, n);Â
    return 0;} |
Java
// Java program to print the prime numbers in the // first half and second half of an arrayimport java.util.*;Â
class GFG {Â
// Function to check if // a number is prime or notstatic boolean prime(int n){Â Â Â Â for(int i = 2; i * i <= n; i++)Â Â Â Â Â Â Â Â if(n % i == 0)Â Â Â Â Â Â Â Â Â Â Â Â return false;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return true;}Â
// Function to find whether elements// are prime or notstatic void prime_range(int start,                         int end, int[] a){    // Traverse in the given range    for (int i = start; i < end; i++)     {             // Check if a number is prime or not        if(prime(a[i]))            System.out.print(a[i] + " ");    }}Â
Â
// Function to print the prime numbers in the // first half and second half of an arraystatic void Print(int arr[], int n){Â
    System.out.print("Prime numbers in the first half are ");    prime_range(0, n / 2, arr);    System.out.println();Â
    System.out.print("Prime numbers in the second half are ");    prime_range(n / 2, n, arr);    System.out.println();}Â
// Driver Codepublic static void main(String[] args) {Â Â Â Â int arr[] = { 2, 5, 10, 15, 17, 21, 23 };Â
    int n = arr.length;         // Function call    Print(arr, n);}}Â
// This code is contributed by Princi Singh |
Python3
# Python3 program to print the # prime numbers in the first half # and second half of an arrayÂ
# Function to check if # a number is prime or notdef prime(n):Â
    for i in range(2, n):        if i * i > n:            break        if(n % i == 0):            return False;Â
    return True     # Function to find whether # elements are prime or notdef prime_range(start, end, a):         # Traverse in the given range    for i in range(start, end):Â
        # Check if a number is prime or not        if(prime(a[i])):            print(a[i], end = " ")Â
# Function to print the # prime numbers in the first half# and second half of an arraydef Print(arr, n):Â
    print("Prime numbers in the",          "first half are ", end = "")    prime_range(0, n // 2, arr)    print()Â
    print("Prime numbers in the",           "second half are ", end = "")    prime_range(n // 2, n, arr)    print()Â
# Driver Codearr = [2, 5, 10, 15, 17, 21, 23]Â
n = len(arr)Â
# Function callPrint(arr, n)Â
# This code is contributed # by Mohit Kumar |
C#
// C# program to print the prime numbers in the // first half and second half of an arrayusing System;Â Â Â Â Â class GFG {Â
// Function to check if // a number is prime or notstatic Boolean prime(int n){Â Â Â Â for(int i = 2; i * i <= n; i++)Â Â Â Â Â Â Â Â if(n % i == 0)Â Â Â Â Â Â Â Â Â Â Â Â return false;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return true;}Â
// Function to find whether elements// are prime or notstatic void prime_range(int start,                         int end, int[] a){    // Traverse in the given range    for (int i = start; i < end; i++)     {             // Check if a number is prime or not        if(prime(a[i]))            Console.Write(a[i] + " ");    }}Â
Â
// Function to print the prime numbers in the // first half and second half of an arraystatic void Print(int []arr, int n){Â
    Console.Write("Prime numbers in the first half are ");    prime_range(0, n / 2, arr);    Console.WriteLine();Â
    Console.Write("Prime numbers in the second half are ");    prime_range(n / 2, n, arr);    Console.WriteLine();}Â
// Driver Codepublic static void Main(String[] args) {Â Â Â Â int []arr = { 2, 5, 10, 15, 17, 21, 23 };Â
    int n = arr.Length;         // Function call    Print(arr, n);}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>// Javascript program to print the prime numbers in the// first half and second half of an arrayÂ
// Function to check if a number is prime or notfunction prime(n){Â Â Â Â for (let i = 2; i * i <= n; i++)Â Â Â Â Â Â Â Â if (n % i == 0)Â Â Â Â Â Â Â Â Â Â Â Â return false;Â
    return true;}Â
// Function to find whether elements are prime or notfunction prime_range(start, end, a){Â
    // Traverse in the given range    for (let i = start; i < end; i++) {Â
        // Check if a number is prime or not        if (prime(a[i]))            document.write(a[i] + " ");    }}Â
// Function to print the prime numbers in the// first half and second half of an arrayfunction Print(arr, n){Â
    document.write("Prime numbers in the first half are ");    prime_range(0, parseInt(n / 2), arr);    document.write("<br>");Â
    document.write("Prime numbers in the second half are ");    prime_range(parseInt(n / 2), n, arr);    document.write("<br>");}Â
// Driver Codelet arr = [ 2, 5, 10, 15, 17, 21, 23 ];let n = arr.length;Â
// Function callPrint(arr, n);Â
// This code is contributed by rishavmahato348.</script> |
Prime numbers in the first half are 2 5 Prime numbers in the second half are 17 23
Â
Time Complexity: O(n * Â sqrt(n)), as we are using a loop for traversing n times and each time for checking if a number is prime or not we are using a loop to traverse sqrt(n) times which will cost O(sqrt(n)).
Auxiliary Space: O(1), as we are not using any extra space.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



