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 not bool 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 not 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])) cout << a[i] << " "; } } // Function to print the prime numbers in the // first half and second half of an array void 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 Code int 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 array import java.util.*; class GFG { // Function to check if // a number is prime or not static 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 not static 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 array static 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 Code public 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 not def 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 not def 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 array def 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 Code arr = [2, 5, 10, 15, 17, 21, 23] n = len(arr) # Function call Print(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 array using System; class GFG { // Function to check if // a number is prime or not static 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 not static 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 array static 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 Code public 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 |
Prime numbers in the first half are 2 5 Prime numbers in the second half are 17 23
Recommended Posts:
- Find the sum of the first half and second half elements of an array
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Sort first half in ascending and second half in descending order | Set 2
- Sort first half in ascending and second half in descending order | 1
- Count number of rotated strings which have more number of vowels in the first half than second half
- Find if array has an element whose value is half of array sum
- Find elements larger than half of the elements in an array
- Check if at least half array is reducible to zero by performing some operations
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Find the sum of prime numbers in the Kth array
- Number of subarrays such that XOR of one half is equal to the other
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check if the frequency of any character is more than half the length of the string
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



