Amenable numbers
Last Updated :
13 Jul, 2021
Given a number N, the task is to check if N is an Amenable Number or not. If N is an Amenable number then print “Yes” else print “No”.
Amenable numbers are numbers if there exists a multiset of integers whose size, sum and product equal to the number.
For example, 8 is an Amenable Number because there is a multiset of integers {-1, -1, 1, 1, 1, 1, 2, 4} whose size, sum and product is 8.
Examples:
Input: N = 8
Output: Yes
Explanation:
8 is an Amenable Number because there is a multiset of integers {-1, -1, 1, 1, 1, 1, 2, 4} whose size, sum and product is 8.
Input: N = 30
Output: No
Explanation:
30 is not an Amenable Number because there doesn’t exists a multiset of integers whose size, sum and product is 30.
Approach:
The first few Amenable Numbers are 1, 5, 8, 9, 12, 13, 16, 17, 20, 21….. which can be represented as the form 4K or 4K + 1.
Therefore, any number N of the form 4K or 4K + 1 is an Amenable Numbers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isAmenableNum( int N)
{
return (N % 4 == 0
|| (N - 1) % 4 == 0);
}
int main()
{
int n = 8;
if (isAmenableNum(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isAmenableNum( int N)
{
return (N % 4 == 0 || (N - 1 ) % 4 == 0 );
}
public static void main(String[] args)
{
int n = 8 ;
if (isAmenableNum(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isAmenableNum(N):
return (N % 4 = = 0 or
(N - 1 ) % 4 = = 0 );
N = 8 ;
if (isAmenableNum(N)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool isAmenableNum( int N)
{
return (N % 4 == 0 || (N - 1) % 4 == 0);
}
public static void Main(String[] args)
{
int n = 8;
if (isAmenableNum(n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isAmenableNum( N)
{
return (N % 4 == 0 || (N - 1) % 4 == 0);
}
let n = 8;
if (isAmenableNum(n)) {
document.write( "Yes" );
} else {
document.write( "No" );
}
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...