
- 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
Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
In this tutorial, we are going to write a program that finds the largest number formed from the array that is divisible by 2, 3, and 5.
Let's see the steps to solve the problem.
- Initialise the array.
- The number must end with 0 and the sum of all the numbers should be divisible by 3 to be divisible by 2, 3, and 5.
- Check for the 0 in the array and print not possible if it's not present in the array.
- Sort the array in descending order.
- Find the remainder for sum % 3.
- If the remainder is not 1, then delete all the digits from the end whose remainder for digit % 3 is equal to the above remainder.
- If there are no digits with the same remainder as above, then subtract 3 from the above remainder and delete the last two digits whose remainder is the same as above.
- Print all the digits from the array.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void findLargestDivibleNumber(int n, vector<int>& v){ int flag = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (v[i] == 0) { flag = 1; } sum += v[i]; } if (!flag) { cout << "Not possible" << endl; }else { sort(v.begin(), v.end(), greater<int>()); if (v[0] == 0) { cout << "0" << endl; }else { int flag = 0; int remainder = sum % 3; if (remainder != 0) { for (int i = n - 1; i >= 0; i--) { if (v[i] % 3 == remainder) { v.erase(v.begin() + i); flag = 1; break; } } if (flag == 0) { remainder = 3 - remainder; int count = 0; for (int i = n - 1; i >= 0; i--) { if (v[i] % 3 == remainder) { v.erase(v.begin() + i); count++; if (count >= 2) { break; } } } } } if (*v.begin() == 0) { cout << "0" << endl; }else { for (int i : v) { cout << i; } } } } } int main() { int n = 9; vector<int> v{ 4, 5, 0, 3, 2, 4, 5, 6, 7 }; findLargestDivibleNumber(n, v); return 0; }
Output
If you run the above code, then you will get the following result.
765544320
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Largest N digit number divisible by given three numbers in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Find largest number smaller than N with same set of digits in C++
- Multiply the given number by 2 such that it is divisible by 10
- Find N digits number which is divisible by D in C++
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Find the largest number that can be formed with the given digits in C++
- Check if N is divisible by a number which is composed of the digits from the set {A, B} in Python
- Find the largest multiple of 3 from array of digits - Set 2 in C++
- Activity : Ask all the students in your class to write a 3-digit number. Choose any student from the room at random. What is the probability that the number written by her/him is divisible by 3 ? Remember that a number is divisible by 3 , if the sum of its digits is divisible by 3 .
- Check if a large number is divisible by 2, 3 and 5 or not in C++
- A number is divisible by both 5 and 12. By which other number will that number be always divisible?
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++
- Prove that one and only one out of $n, (n + 2)$ and $(n + 4)$ is divisible by 3, where $n$ is any positive integer.
- N digit numbers divisible by 5 formed from the M digits in C++

Advertisements