Given an array arr[] of size N, the task is to print all the perfect numbers from an array whose sum of digits is also a perfect number.
Examples:
Input: arr[] = { 3, 8, 12, 28, 6 }
Output: 6
Explanation: The array element arr[4] (= 6) is a perfect number. The array element arr[3] (= 28) is a perfect number but its sum of digits (= 10) is not a perfect number.
Input: arr[] = { 1, 2, 3 }
Output: 1
Approach: Follow the steps below to solve the problem:
- Declare a function, isPerfect() to check if the number is a perfect number or not.
- Declare another function, sumOfDigits() to calculate the sum of all the digits of a number.
- Traverse the array arr[]:
- If arr[i] is a perfect number:
- Initialize a variable, say digitSum, to store the sum of digits of the current array element.
- If digitSum is also a perfect number, print that number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPerfect(int N)
{
int sumOfDivisors = 1;
for (int i = 2; i <= N / 2; ++i) {
if (N % i == 0) {
sumOfDivisors += i;
}
}
if (sumOfDivisors == N) {
return 1;
}
else
return 0;
}
int sumOfDigits(int N)
{
int sum = 0;
while (N != 0) {
sum += (N % 10);
N = N / 10;
}
return sum;
}
void countPerfectNumbers(int arr[], int N)
{
for (int i = 0; i < N; ++i) {
if (isPerfect(arr[i])) {
int sum = sumOfDigits(arr[i]);
if (isPerfect(sum)) {
cout << arr[i] << " ";
}
}
}
}
int main()
{
int arr[] = { 3, 8, 12, 28, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
countPerfectNumbers(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static boolean isPerfect(int N)
{
int sumOfDivisors = 1;
for (int i = 2; i <= N / 2; ++i) {
if (N % i == 0) {
sumOfDivisors += i;
}
}
if (sumOfDivisors == N) {
return true;
}
else
return false;
}
static int sumOfDigits(int N)
{
int sum = 0;
while (N != 0) {
sum += (N % 10);
N = N / 10;
}
return sum;
}
static void countPerfectNumbers(int arr[], int N)
{
for (int i = 0; i < N; ++i) {
if (isPerfect(arr[i])) {
int sum = sumOfDigits(arr[i]);
if (isPerfect(sum)) {
System.out.print(arr[i] + " ");
}
}
}
}
public static void main(String[] args)
{
int arr[] = { 3, 8, 12, 28, 6 };
int N = arr.length;
countPerfectNumbers(arr, N);
}
}
|
Python3
def isPerfect(N):
sumOfDivisors = 1;
for i in range(2, int(N / 2) + 1):
if (N % i == 0):
sumOfDivisors += i;
if (sumOfDivisors == N):
return True;
else:
return False;
def sumOfDigits(N):
sum = 0;
while (N != 0):
sum += (N % 10);
N = N // 10;
return sum;
def countPerfectNumbers(arr, N):
for i in range(N):
if (isPerfect(arr[i])):
sum = sumOfDigits(arr[i]);
if (isPerfect(sum)):
print(arr[i], end=" ");
if __name__ == '__main__':
arr = [3, 8, 12, 28, 6];
N = len(arr);
countPerfectNumbers(arr, N);
|
C#
using System;
class GFG
{
static bool isPerfect(int N)
{
int sumOfDivisors = 1;
for (int i = 2; i <= N / 2; ++i) {
if (N % i == 0) {
sumOfDivisors += i;
}
}
if (sumOfDivisors == N) {
return true;
}
else
return false;
}
static int sumOfDigits(int N)
{
int sum = 0;
while (N != 0) {
sum += (N % 10);
N = N / 10;
}
return sum;
}
static void countPerfectNumbers(int []arr, int N)
{
for (int i = 0; i < N; ++i) {
if (isPerfect(arr[i])) {
int sum = sumOfDigits(arr[i]);
if (isPerfect(sum)) {
Console.Write(arr[i] + " ");
}
}
}
}
static public void Main()
{
int []arr = { 3, 8, 12, 28, 6 };
int N = arr.Length;
countPerfectNumbers(arr, N);
}
}
|
Javascript
<script>
function isPerfect(N)
{
let sumOfDivisors = 1;
for (let i = 2; i <= Math.floor(N / 2); ++i)
{
if (N % i === 0)
{
sumOfDivisors += i;
}
}
if (sumOfDivisors === N) {
return 1;
}
else
return 0;
}
function sumOfDigits(N)
{
let sum = 0;
while (N != 0) {
sum += (N % 10);
N = Math.floor(N / 10);
}
return sum;
}
function countPerfectNumbers(arr, N)
{
for (let i = 0; i < N; ++i) {
if (isPerfect(arr[i])) {
let sum = sumOfDigits(arr[i]);
if (isPerfect(sum)) {
document.write(arr[i] + " ");
}
}
}
}
let arr = [ 3, 8, 12, 28, 6 ];
let N = arr.length;
countPerfectNumbers(arr, N);
</script>
|
Time Complexity: O(N3 * log N)
Auxiliary Space: O(1)
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!