Home > Back-end >  program to find The number of prime numbers in array using cpp
program to find The number of prime numbers in array using cpp

Time:09-22

this is the problem enter link description here

the problem in function prime but it think it is true , and i can not find solution

i submit it in codeforces but it give me .Wrong answer on test 5
:-

the input :

39
81 46 4 5 2 71 66 97 51 84 50 64 68 99 58 45 64 86 14 44 7 49 45 72 94 19 33 68 83 12 89 88 39 36 51 11 57 9 54

the wrong !!

#include <iostream>
#include <math.h>
using namespace std;
int maximum(int arr[], int n)
{
    int max = INT_MIN;
    for (int i = 0; i < n; i  )
    {
        if (max < arr[i]) { max = arr[i]; }
    }
    return max;
}
int minimum(int arr[], int n)
{
    int min = INT_MAX;
    for (int i = 0; i < n; i  )
    {
        if (min > arr[i]) { min = arr[i]; }
    }
    return min;
}
int prime(int arr[], int n)
{
    int con = 0;
    bool flag = true;
    for (int i = 0; i < n; i  )
    {
        if (arr[i] == 2)
        {
            con  ;
        }
        else if (arr[i] > 2)
        {
            for (int j = 2; j < n; j  )
            {
                if (arr[i] % j == 0)
                {
                    flag = false;
                    break;
                }
                else
                {
                    flag = true;
                }
            }
            if (flag == true)
                con  ;
        }
    }

    return con;

}
int palindrome(int arr[], int n)
{
    int i = 0, con = 0;
    while (n--)
    {
        int temp;
        temp = arr[i];
        int reverseNumber = 0, rightDigit;
        while (temp != 0)
        {
            rightDigit = temp % 10;
            reverseNumber = (reverseNumber * 10)   rightDigit;
            temp = temp / 10;
        }
        if (reverseNumber == arr[i]) {
            con  ;
        }
        i  ;

    }
    return con;
}
int divisors(int arr[], int n)
{
    int max = 0;
    int con = 0;
    int x = arr[0];
    for (int i = 0; i < n; i  )
    {
        int temp = arr[i];
        for (int j = 1; j <= arr[i]; j  )
        {
            if (arr[i] % j == 0)
            {
                con  ;
            }
        }
        if (max < con)
        {
            max = con;
            x = arr[i];
        }
        else if (max == con)
        {
            if (x < arr[i])
            {
                x = arr[i];
            }
        }

        con = 0;
    }
    return x;
}
int main()
{
    int n; cin >> n;
    int arr[1001];
    for (int i = 0; i < n; i  )
        cin >> arr[i];
    cout << "The maximum number : " << maximum(arr, n) << endl;
    cout << "The minimum number : " << minimum(arr, n) << endl;
    cout << "The number of prime numbers : " << prime(arr, n) << endl;
    cout << "The number of palindrome numbers : " << palindrome(arr, n) << endl;
    cout << "The number that has the maximum number of divisors : " << divisors(arr, n) << endl;
    divisors(arr, n);
    return 0;
}

CodePudding user response:

This for loop

for (int j = 2; j < n; j  )

is incorrect. It seems you mean

for (int j = 2; j < arr[i]; j  )

Also you should declare the variable flag within the else statement where it is used. For example

for (int i = 0; i < n; i  )
{
    if (arr[i] == 2)
    {
        con  ;
    }
    else if (arr[i] > 2)
    { 
        bool flag = false;
        //...
  • Related