Junction Number is a number N if it can be written as K + SumOfDIgits(k) for at least two K.
101, 103, 105, 107, 109, 111, 113, 115….
Check if a number N is an Junction Number
Given a number N, the task is to check if N is an Junction Number or not. If N is an Junction Number then print “Yes” else print “No”.
Examples:
Input: N = 1106
Output: Yes
Explanation:
1106 = 1093 + (1+0+9+3) = 1102 + (1+1+0+2)
Input: N = 11
Output: No
Approach: Since Junction Number is a number N if it can be written as K + SumOfDIgits(k) for at least two K. So for all numbers from 1 to N in a loop we will check if i + sumOfdigitsof(i) equals to N or not. If equals to N print “Yes” else print “No”.
Below is the implementation of the above approach:
// C++ implementation for the // above approach #include <bits/stdc++.h> using namespace std;
// Function to find the // sum Of digits of N int sum( int n)
{ // To store sum of N and
// sumOfdigitsof(N)
int sum = 0;
while (n != 0) {
// extracting digit
int r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
} // Function to check Junction numbers bool isJunction( int n)
{ // To store count of ways n can be
// represented as i + SOD(i)
int count = 0;
for ( int i = 1; i <= n; i++) {
if (i + sum(i) == n)
count++;
}
return count >= 2;
} // Driver Code int main()
{ int N = 111;
// Function Call
if (isJunction(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
} |
// Java program for above approach class GFG{
// Function to find the // sum Of digits of N static int sum( int n)
{ // To store sum of N and
// sumOfdigitsof(N)
int sum = 0 ;
while (n != 0 )
{
// extracting digit
int r = n % 10 ;
sum = sum + r;
n = n / 10 ;
}
return sum;
} // Function to check Junction numbers static boolean isJunction( int n)
{ // To store count of ways n can be
// represented as i + SOD(i)
int count = 0 ;
for ( int i = 1 ; i <= n; i++)
{
if (i + sum(i) == n)
count++;
}
return count >= 2 ;
} // Driver Code public static void main(String[] args)
{ int N = 111 ;
// Function Call
if (isJunction(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
} } // This code is contributed by Pratima Pandey |
# Python3 program for the above approach import math
# Function to find the # sum Of digits of N def sum1(n):
# To store sum of N and
# sumOfdigitsof(N)
sum1 = 0
while (n ! = 0 ):
# extracting digit
r = n % 10
sum1 = sum1 + r
n = n / / 10
return sum1
# Function to check Junction numbers def isJunction(n):
# To store count of ways n can be
# represented as i + SOD(i)
count = 0
for i in range ( 1 , n + 1 ):
if (i + sum1(i) = = n):
count = count + 1
return count > = 2
# Driver Code if __name__ = = '__main__' :
# Given Number
n = 111
# Function Call
if (isJunction(n) = = 1 ):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by rock_cool |
// C# program for above approach using System;
class GFG{
// Function to find the // sum Of digits of N static int sum( int n)
{ // To store sum of N and
// sumOfdigitsof(N)
int sum = 0;
while (n != 0)
{
// extracting digit
int r = n % 10;
sum = sum + r;
n = n / 10;
}
return sum;
} // Function to check Junction numbers static bool isJunction( int n)
{ // To store count of ways n can be
// represented as i + SOD(i)
int count = 0;
for ( int i = 1; i <= n; i++)
{
if (i + sum(i) == n)
count++;
}
return count >= 2;
} // Driver Code public static void Main()
{ int N = 111;
// Function Call
if (isJunction(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
} } // This code is contributed by Nidhi Biet |
<script> // Javascript program for above approach // Function to find the
// sum Of digits of N
function sum( n) {
// To store sum of N and
// sumOfdigitsof(N)
let sum = 0;
while (n != 0) {
// extracting digit
let r = n % 10;
sum = sum + r;
n = parseInt(n / 10);
}
return sum;
}
// Function to check Junction numbers
function isJunction( n) {
// To store count of ways n can be
// represented as i + SOD(i)
let count = 0;
for ( i = 1; i <= n; i++) {
if (i + sum(i) == n)
count++;
}
return count >= 2;
}
// Driver Code
let N = 111;
// Function Call
if (isJunction(N))
document.write( "Yes" );
else
document.write( "No" );
// This code contributed by Rajput-Ji </script> |
Yes
Time Complexity: O(n)
Space Complexity: O(1)
Reference: http://www.numbersaplenty.com/set/junction_number/