Consider a permutation of numbers 1 to N written on a paper. Let’s denote the product of its element as P and the sum of its elements as S Given a positive integer N, your task is to determine whether P is divisible by S or not.
Example:
Input: n = 3
Output: YES
Approach
C++
#include <bits/stdc++.h>using namespace std;bool isPrime(int n){if (n <= 1)return false;for (int i = 2; i * i <= n; i++)if (n % i == 0)return false;return true;}void doesItDivide(int n){if ((n + 1) % 2 == 0)cout << "YES\n";else{if (isPrime(n + 1))cout << "NO\n";elsecout << "YES\n";}}int main(){int n = 3;doesItDivide(n);return 0;}
No comments:
Post a Comment