

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 multiple of 2, 3 and 5 in C++
In this problem, we are given an array arr[] of size N consisting of single digits only. Our task is to find the largest multiple of 2, 3 and 5.
Let's take an example to understand the problem,
Input : arr[] = {1, 0, 5, 2} Output : 510
Explanation −
The number 510 is divisible by all 2, 3, 5.
Solution Approach
A simple solution to the problem is by checking for basic divisibility of the number created.
So, if the number needs to be divisible by 2 and 5 i.e. it is divisible by 10. For creating a number divisible by 10, the array must have zero.
If it has a zero then, we will create the largest possible number with zero at the end which is divisible by 3.
The method is shown here. Largest Multiple of Three in C++
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; class Solution { public: string largestMultipleOfThree(vector<int>& digits) { vector<vector<int>> d(3); int sum = 0; for (int i = 0; i < digits.size(); i++) { int x = digits[i]; d[x % 3].push_back(digits[i]); sum += x; sum %= 3; } if (sum) { if (!d[sum].size()) { int rem = 3 - sum; if (d[rem].size() < 2) return ""; d[rem].pop_back(); d[rem].pop_back(); } else { d[sum].pop_back(); } } string ret = ""; for (int i = 0; i < 3; i++) { for (int j = 0; j < d[i].size(); j++) { ret += to_string(d[i][j]); } } sort(ret.begin(), ret.end(), greater<int>()); if (ret.size() && ret[0] == '0') return "0"; return ret; } }; int main(){ Solution ob; vector<int> v = {7, 2, 0, 8}; sort(v.begin(), v.end(), greater<int>()); if(v[v.size() - 1 ] != 0){ cout<<"Not Possible!"; } else{ cout<<"The largest number is "<<(ob.largestMultipleOfThree(v)); } }
Output
The largest number is 870
- Related Questions & Answers
- Find the largest multiple of 3 from array of digits - Set 2 in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Find sum of the series 1-2+3-4+5-6+7....in C++
- Replace Multiple of 3 and 5 With Fizz, Buzz in Python
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Program to find N-th term of series 0, 2,1, 3, 1, 5, 2, 7, 3...in C++
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Find Multiples of 2 or 3 or 5 less than or equal to N in C++
Advertisements