Find All Prime Numbers in Array

Find All Prime Numbers in Array

Medium C++ Arrays 42 views
Explanation Complexity

Problem Statement

Identify and display all prime numbers present in given array. This combines prime checking logic with array traversal.

Logic: For each number, check if it's prime using division method

Input Format

An integer n (size of array)
Then n integers.

Output Format

All prime numbers present in the array (space-separated).
If no prime exists, print nothing (or empty output).

Example

6
2 4 5 6 7 9
2 5 7

Constraints

• n ≥ 1

• Array elements ≥ 1

Concept Explanation

A prime number is a number greater than 1 that has only two divisors: 1 and itself.
Each array element is checked separately.

Step-by-Step Explanation

Prime Check Logic (for one number)

1.If number ≤ 1 → not prime.

2.Check divisibility from 2 to √number.

3.If divisible by any value → not prime.

4.If no divisor found → prime.

Array Traversal Logic

1.Traverse the array from index 0 to n-1.

2.For each element, apply the prime check logic.

3.If the number is prime:

• Print it.

4.Continue until the end of the array.

Concept Explanation

A prime number is a number greater than 1 that has only two divisors: 1 and itself.
Each array element is checked separately.

Step-by-Step Explanation

Prime Check Logic (for one number)

1.If number ≤ 1 → not prime.

2.Check divisibility from 2 to √number.

3.If divisible by any value → not prime.

4.If no divisor found → prime.

Array Traversal Logic

1.Traverse the array from index 0 to n-1.

2.For each element, apply the prime check logic.

3.If the number is prime:

• Print it.

4.Continue until the end of the array.

Input / Output Format

Input Format
An integer n (size of array)
Then n integers.
Output Format
All prime numbers present in the array (space-separated).
If no prime exists, print nothing (or empty output).
Constraints
• n ≥ 1

• Array elements ≥ 1

Examples

Input:
6 2 4 5 6 7 9
Output:
2 5 7

Example Solution (Public)

C++
void question10_prime_in_array() {
    int arr[] = {10, 13, 15, 17, 20, 23};
    int size = 6;
    
    cout << "Prime numbers: ";
    for(int i = 0; i < size; i++) {
        bool isPrime = true;
        if(arr[i] < 2) isPrime = false;
        
        for(int j = 2; j * j <= arr[i]; j++) {
            if(arr[i] % j == 0) {
                isPrime = false;
                break;
            }
        }
        
        if(isPrime) {
            cout << arr[i] << " ";
        }
    }
    cout << endl;
}

Official Solution Code

void question10_prime_in_array() {
    int arr[] = {10, 13, 15, 17, 20, 23};
    int size = 6;
    
    cout << "Prime numbers: ";
    for(int i = 0; i < size; i++) {
        bool isPrime = true;
        if(arr[i] < 2) isPrime = false;
        
        for(int j = 2; j * j <= arr[i]; j++) {
            if(arr[i] % j == 0) {
                isPrime = false;
                break;
            }
        }
        
        if(isPrime) {
            cout << arr[i] << " ";
        }
    }
    cout << endl;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.