Average student

Given an array of n elements.

solve(x): no the element present in the array which has exactly x divisor.

Kitu is giving an exam which has only two question 

  • Evaluate 2+341
  • Evaluate solve(2)+solve(3)solve(4)solve(1)

Since the first part is very easy so she can solve it but she wants your help in the second part.

Find the value of solve(2)+solve(3)solve(4)solve(1) .

Example:

Input:  n=3, arr = [2,3,6]
Output: 1

Approach

C++

#include <bits/stdc++.h>
using namespace std;
#define m 10000

int averageStudent(int nvector<int&arr)
{
    int y1 = 0y2 = 0y3 = 0y4 = 0;
    int b[m + 1];
    for (int i = 0i <= m; i++)
        b[i] = 1;
    for (int i = 2i <= m; i++)
    {
        for (int j = 1j * i <= m; j++)
            b[i * j]++;
    }
    for (int i = 0i < ni++)
    {
        int x = b[arr[i]];
        if (x == 1)
            y1++;
        else if (x == 2)
            y2++;
        else if (x == 3)
            y3++;
        else if (x == 4)
            y4++;
    }
    return y2 + y3 - y1 - y4;
}
int main()
{
    int n = 3;
    vector<int>arr = {236};

    cout << averageStudent(narr);
    return 0;
}


No comments:

Post a Comment