
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Largest Good Number in Divisors of Given Number n in C++
In this problem, we are given a number N. Our task is to find the largest good number in the divisors of given number N.
A good number is a number in which every digit is larger than the sum of digits of its right (all less significant bits than it). For example, 732 is a good number, 7> 3+2 and 3>2.
Let's take an example to understand the problem,
Input : N = 15 Output : 15
Explanation −
Divisors of 15 : 1, 3, 5, 15.
Solution Approach
A simple solution to the problem is by finding all the divisors of N. And they find the largest good number which is extracted as a product of all prime divisors of the number.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int findLargestGoodNumber(int n){ vector<int> primeFactors; int x = n; for (int i = 2; i * i <= n; i++) { if (x % i == 0) { primeFactors.push_back(i); while (x % i == 0) x /= i; } } if (x > 1) primeFactors.push_back(x); int goodNumber = 1; for (int i = 0; i < primeFactors.size(); i++) goodNumber = goodNumber * primeFactors[i]; return goodNumber; } int main(){ int n = 28; cout<<"The largest good Number in divisor of "<<n<<" is "<<findLargestGoodNumber(n); return 0; }
Example
The largest good Number in divisor of 28 is 14
Advertisements