Archery

N archers are shooting arrows at targets. There are infinite targets numbered starting with 1. The 

ith archer shoots at all targets that are multiples of ki.

Find the smallest target that is hit by all the archers.

Example:

Input:  n = 3, a ={2,3,4}
Output: 12

Approach

C++

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

long long gcd(long long along long b)
{
    if (b == 0)
        return a;
    return gcd(ba % b);
}

long long archery(long long nlong long a[])
{
    long long ans = a[0];
    for (long long i = 1i < ni++)
    {
        ans = (ans * a[i]) / gcd(a[i], ans);
    }
    return ans;
}
int main()
{
    long long n = 3;

    long long a[n] = {234};

    cout << archery(na<< "\n";

    return 0;
}


No comments:

Post a Comment