The kingdom Jabari, after conquering the whole planet of Ukanda, decides to host a party for every minister of the kingdom. The king of Jabari decides to gift gold coins to every minister who attends the party. However, the problem is that the number of ministers attending the party is uncertain and the treasurer doesn’t want to have any excess or shortage of money after distribution. It doesn’t matter how many gold coins are given to each attendee but they must be given equally. The chief analyst of the kingdom has a dataset of
values which are the possible number of ministers to turn up for the party. Help the organizers find the least number of party coins, to be asked by the treasurer to distribute to the attendees so that at the end of the party the treasurer isn’t furied.
Example:
Input: n = 3, a = [1,1,1]
Output: 1
Approach
C++
#include <bits/stdc++.h>using namespace std;long long gcd(long long a, long long b){if (b == 0)return a;return gcd(b, a % b);}long long lcm(long long arr[], long long n){long long ans = arr[0];for (long long i = 1; i < n; i++){ans = (ans * arr[i]) / (gcd(arr[i], ans));}return ans;}void riseOfJabari(long long n, long long a[]){sort(a, a + n);if (a[0] == 0)cout << 0 << "\n";else{long long ans = lcm(a, n);cout << ans << "\n";}}int main(){long long n = 3;long long a[n] = {1, 1, 1};riseOfJabari(n, a);return 0;}
No comments:
Post a Comment