- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Find the largest multiple of 3 from array of digits - Set 2 in C++
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- Largest Multiple of Three in C++
- Find the area of the quadrilateral whose vertices, taken in order, are $(-4, -2), (-3, -5), (3, -2)$ and $(2, 3)$.
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- If a=3 and b=-1then find the value of$5 ab-2 a^{2}+5 b^{2}$
- Find sum of the series 1-2+3-4+5-6+7....in C++
- Find the area of the quadrilaterals, the coordinates of whose vertices are$(-4, -2), (-3, -5), (3, -2), (2, 3)$
- Find the largest angle of a triangle which are in the ratio $4:3:2$.
- Find the value of x:$ 2 x - frac{2}{5} = frac{3}{5} - x$
- Find the area of the triangle whose vertices are:(i) $(2, 3), (-1, 0), (2, -4)$(ii) $(-5, -1), (3, -5), (5, 2)$
- Show that the points $(-3, 2), (-5, -5), (2, -3)$ and $(4, 4)$ are the vertices of a rhombus. Find the area of this rhombus.
- Find the value of the following:$2 -frac{3}{5}$.
- Replace Multiple of 3 and 5 With Fizz, Buzz in Python
- Show that $A (-3, 2), B (-5, -5), C (2, -3)$ and $D (4, 4)$ are the vertices of a rhombus.

Advertisements