
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the largest good number in the 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
- Related Articles
- Find the number of divisors of all numbers in the range [1, n] in C++
- Count the number of common divisors of the given strings in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Find sum of divisors of all the divisors of a natural number in C++
- First triangular number whose number of divisors exceeds N in C++
- Find largest sum of digits in all divisors of n in C++
- Find the number of line segments in the given figure."\n
- How to find the n number of largest values in an R vector?
- Largest N digit number divisible by given three numbers in C++
- Find number from its divisors in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Find all divisors of a natural number in java
- Program to find count of numbers having odd number of divisors in given range in C++
- Count the numbers < N which have equal number of divisors as K in C++
- Find the largest number that can be formed with the given digits in C++

Advertisements