Given an array arr[] consisting of N distinct positive integers, the task is to print the first K distinct Moran Numbers from the given array.
A number N is a Moran number if N divided by the sum of its digits gives a prime number.
Examples: 18, 21, 27, 42, 45
Examples:
Input: arr[] = {192, 21, 18, 138, 27, 42, 45}, K = 4
Output: 21, 27, 42, 45
Explanation:
- The sum of digits of the integer 21 is 2 + 1 = 3. Therefore, dividing 21 by its sum of digits = 21 / 3 = 7, which is a prime number.
- The sum of digits of the integer 27 is 2 + 7 = 9. Therefore, dividing 27 by its sum of digits results in 27 / 9 = 3, which is a prime number.
- The sum of digits of the integer 42 is 4 + 2 = 6. Therefore, dividing 42 by its sum of digits results in 42 / 6 = 7, which is a prime number.
- The sum of digits of the integer 45 is 4 + 5 = 9. Therefore, dividing 45 by its sum of digits results in 45 / 9 = 5, which is a prime number.
Input: arr[]={127, 186, 198, 63, 27, 91}, K = 3
Output: 27, 63, 198
Approach: Follow the steps given below to solve the problem:
- Sort the array
- Traverse the sorted array and for each element, check if it is a Moran number or not
- If found to be true, insert the element in a Set and increment the counter until it reaches K.
- Print the elements in the Set when the size of the set is equal to K.
Below is the implementation of the above approach:
C++
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int digiSum( int a)
{
int sum = 0;
while (a) {
sum += a % 10;
a = a / 10;
}
return sum;
}
bool isPrime( int r)
{
bool s = true ;
for ( int i = 2; i * i <= r; i++) {
if (r % i == 0) {
s = false ;
break ;
}
}
return s;
}
bool isMorannumber( int n)
{
int dup = n;
int sum = digiSum(dup);
if (n % sum == 0) {
int c = n / sum;
if (isPrime(c)) {
return true ;
}
}
return false ;
}
void FirstKMorannumber( int a[],
int n, int k)
{
int X = k;
sort(a, a + n);
set< int > s;
for ( int i = n - 1; i >= 0
&& k > 0;
i--) {
if (isMorannumber(a[i])) {
s.insert(a[i]);
k--;
}
}
if (k > 0) {
cout << X << " Moran numbers are"
<< " not present in the array" << endl;
return ;
}
set< int >::iterator it;
for (it = s.begin(); it != s.end(); ++it) {
cout << *it << ", " ;
}
cout << endl;
}
int main()
{
int A[] = { 34, 198, 21, 42,
63, 45, 22, 44, 43 };
int K = 4;
int N = sizeof (A) / sizeof (A[0]);
FirstKMorannumber(A, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int digiSum( int a)
{
int sum = 0 ;
while (a != 0 )
{
sum += a % 10 ;
a = a / 10 ;
}
return sum;
}
static boolean isPrime( int r)
{
boolean s = true ;
for ( int i = 2 ; i * i <= r; i++)
{
if (r % i == 0 )
{
s = false ;
break ;
}
}
return s;
}
static boolean isMorannumber( int n)
{
int dup = n;
int sum = digiSum(dup);
if (n % sum == 0 )
{
int c = n / sum;
if (isPrime(c))
{
return true ;
}
}
return false ;
}
static void FirstKMorannumber( int [] a,
int n, int k)
{
int X = k;
Arrays.sort(a);
TreeSet<Integer> s = new TreeSet<Integer>();
for ( int i = n - 1 ; i >= 0 && k > 0 ; i--)
{
if (isMorannumber(a[i]))
{
s.add(a[i]);
k--;
}
}
if (k > 0 )
{
System.out.println(X + " Moran numbers are" +
" not present in the array" );
return ;
}
for ( int value : s)
System.out.print(value + ", " );
System.out.print( "\n" );
}
public static void main(String[] args)
{
int [] A = { 34 , 198 , 21 , 42 ,
63 , 45 , 22 , 44 , 43 };
int K = 4 ;
int N = A.length;
FirstKMorannumber(A, N, K);
}
}
|
Python3
import math
def digiSum(a):
sums = 0
while (a ! = 0 ):
sums + = a % 10
a = a / / 10
return sums
def isPrime(r):
s = True
for i in range ( 2 , int (math.sqrt(r)) + 1 ):
if (r % i = = 0 ):
s = False
break
return s
def isMorannumber(n):
dup = n
sums = digiSum(dup)
if (n % sums = = 0 ):
c = n / / sums
if isPrime(c):
return True
return False
def FirstKMorannumber(a, n, k):
X = k
a.sort()
s = set ()
for i in range (n - 1 , - 1 , - 1 ):
if (k < = 0 ):
break
if (isMorannumber(a[i])):
s.add(a[i])
k - = 1
if (k > 0 ):
print (X, end = ' Moran numbers are not '
'present in the array' )
return
lists = sorted (s)
for i in lists:
print (i, end = ', ' )
if __name__ = = '__main__' :
A = [ 34 , 198 , 21 , 42 ,
63 , 45 , 22 , 44 , 43 ]
K = 4
N = len (A)
FirstKMorannumber(A, N, K)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int digiSum( int a)
{
int sum = 0;
while (a != 0)
{
sum += a % 10;
a = a / 10;
}
return sum;
}
static bool isPrime( int r)
{
bool s = true ;
for ( int i = 2; i * i <= r; i++)
{
if (r % i == 0)
{
s = false ;
break ;
}
}
return s;
}
static bool isMorannumber( int n)
{
int dup = n;
int sum = digiSum(dup);
if (n % sum == 0)
{
int c = n / sum;
if (isPrime(c))
{
return true ;
}
}
return false ;
}
static void FirstKMorannumber( int [] a,
int n, int k)
{
int X = k;
Array.Sort(a);
SortedSet< int > s = new SortedSet< int >();
for ( int i = n - 1; i >= 0 && k > 0; i--)
{
if (isMorannumber(a[i]))
{
s.Add(a[i]);
k--;
}
}
if (k > 0)
{
Console.WriteLine(X + " Moran numbers are" +
" not present in the array" );
return ;
}
foreach ( var val in s)
{
Console.Write(val + ", " );
}
Console.Write( "\n" );
}
public static void Main()
{
int [] A = { 34, 198, 21, 42,
63, 45, 22, 44, 43 };
int K = 4;
int N = A.Length;
FirstKMorannumber(A, N, K);
}
}
|
Javascript
<script>
function digiSum(a)
{
let sum = 0;
while (a) {
sum += a % 10;
a = Math.floor(a / 10);
}
return sum;
}
function isPrime(r)
{
let s = true ;
for (let i = 2; i * i <= r; i++) {
if (r % i == 0) {
s = false ;
break ;
}
}
return s;
}
function isMorannumber(n)
{
let dup = n;
let sum = digiSum(dup);
if (n % sum == 0) {
let c = n / sum;
if (isPrime(c)) {
return true ;
}
}
return false ;
}
function FirstKMorannumber(a, n, k)
{
let X = k;
a = a.sort((x, y) => x - y)
let s = new Set();
for (let i = n - 1; i >= 0
&& k > 0;
i--) {
if (isMorannumber(a[i])) {
s.add(a[i]);
k--;
}
}
if (k > 0) {
document.write(X + " Moran numbers are"
+ " not present in the array" + "<br>" );
return ;
}
s = [...s].sort((a, b) => a - b)
for (let it of s) {
document.write(it + ", " );
}
document.write( "<br>" );
}
let A = [ 34, 198, 21, 42,
63, 45, 22, 44, 43 ];
let K = 4;
let N = A.length;
FirstKMorannumber(A, N, K);
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 May, 2021
Like Article
Save Article