Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmLargest number with the given set of N digits that is...

Largest number with the given set of N digits that is divisible by 2, 3 and 5

Given a set of ā€˜Nā€™ digits. The task is to find the maximum integer that we can make from these digits. The resultant number must be divisible by 2, 3, and 5.Ā 
Note: It is not necessary to use all the digits from the set. Also, leading zeroes are not allowed.
Examples:
Ā 

Input: N = 11, setOfDigits = {3, 4, 5, 4, 5, 3, 5, 3, 4, 4, 0}Ā 
Output: 5554443330Ā 
After arranging all the elements in a non-increasing order as 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 0. The sum of all the digit is 40. Thus when we found out that the remainder of 40, when divided by 3, is 1. Then weā€™ll start traversing from the end to the start and if we encounter any digit with the same remainder, which we got 4 at the position 7 will be erased it. Now the sum is 36 which is divisible 3 and the new largest number will be 5554443330, which is divisible by 2, 3, and 5.
Input: N = 1, setOfDigits = {0}Ā 
Output: 0

Ā 

Approach: Below is the step by step algorithm to solve this problem:Ā 
Ā 

  1. Initialize the set of digits in a vector.
  2. Any number is divisible by 2, 3 and 5 only if the sum of digits is divisible by 3 and the last digit is 0.
  3. Check if 0 is not present in the vector, then it is not possible to create a number because it will not be divisible by 5.
  4. Sort the vector in a non-increasing manner if the first element is 0 after that, then print 0.
  5. Find the modulus of sum of all the digits by 3 and if itā€™s 1 then delete the first element with the same remainder while traversing from the end.
  6. If there is no element with the same remainder, then delete two elements which has a remainder as 3 ā€“ y.
  7. Print all the remaining digits of a vector as a single integer.

Below is the implementation of the above approach:Ā 
Ā 

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long
Ā 
// Function to find the largest
// integer with the given set
int findLargest(int n, vector<int>& v)
{
Ā 
Ā Ā Ā Ā int flag = 0;
Ā Ā Ā Ā ll sum = 0;
Ā 
Ā Ā Ā Ā // find sum of all the digits
Ā Ā Ā Ā // look if any 0 is present or not
Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1;
Ā Ā Ā Ā Ā Ā Ā Ā sum += v[i];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // if 0 is not present, the resultant number
Ā Ā Ā Ā // won't be divisible by 5
Ā Ā Ā Ā if (!flag)
Ā Ā Ā Ā Ā Ā Ā Ā cout << "Not possible" << endl;
Ā 
Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā // sort all the elements in a non-decreasing manner
Ā Ā Ā Ā Ā Ā Ā Ā sort(v.begin(), v.end(), greater<int>());
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if there is just one element 0
Ā Ā Ā Ā Ā Ā Ā Ā if (v[0] == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "0" << endl;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int flag = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find the remainder of the sum
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // of digits when divided by 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y = sum % 3;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // there can a remainder as 1 or 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (y != 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // traverse from the end of the digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // first element which has the same remainder
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // remove it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.erase(v.begin() + i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if there is no element which
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // has a same remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (flag == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // subtract it by 3 ( could be one or two)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = 3 - y;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int cnt = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // delete two minimal digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // which has a remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.erase(v.begin() + i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cnt >= 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (*v.begin() == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "0" << endl;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print all the digits as a single integer
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i : v) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
// Driver code
int main()
{
Ā Ā Ā Ā // initialize the number of set of digits
Ā Ā Ā Ā int n = 11;
Ā 
Ā Ā Ā Ā // initialize all the set of digits in a vector
Ā Ā Ā Ā vector<int> v{ 3, 9, 9, 6, 4, 3, 6, 4, 9, 6, 0 };
Ā 
Ā Ā Ā Ā findLargest(n, v);
Ā 
Ā Ā Ā Ā return 0;
}


Java




// Java implementation of above approach
import java.util.*;
Ā 
class GFG {
Ā 
Ā Ā Ā Ā // Function to find the largest
Ā Ā Ā Ā // integer with the given set
Ā Ā Ā Ā static int findLargest(int n, Vector<Integer> v)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int flag = 0;
Ā Ā Ā Ā Ā Ā Ā Ā long sum = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // find sum of all the digits
Ā Ā Ā Ā Ā Ā Ā Ā // look if any 0 is present or not
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v.get(i) == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += v.get(i);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if 0 is not present, the resultant number
Ā Ā Ā Ā Ā Ā Ā Ā // won't be divisible by 5
Ā Ā Ā Ā Ā Ā Ā Ā if (flag != 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Not possible");
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // sort all the elements in a non-decreasing manner
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Collections.sort(v, Collections.reverseOrder());
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if there is just one element 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v.get(0) == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("0");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int flags = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find the remainder of the sum
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // of digits when divided by 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y = (int)(sum % 3);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // there can a remainder as 1 or 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (y != 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // traverse from the end of the digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // first element which has the same remainder
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // remove it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v.get(i) % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.remove(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flags = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if there is no element which
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // has a same remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (flags == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // subtract it by 3 ( could be one or two)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = 3 - y;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int cnt = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // delete two minimal digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // which has a remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v.get(i) % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.remove(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cnt >= 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v.get(0) == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("0");
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print all the digits as a single integer
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (Integer i : v) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return Integer.MIN_VALUE;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver code
Ā Ā Ā Ā public static void main(String[] args)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // initialize the number of set of digits
Ā Ā Ā Ā Ā Ā Ā Ā int arr[] = { 3, 9, 9, 6, 4, 3, 6, 4, 9, 6, 0 };
Ā Ā Ā Ā Ā Ā Ā Ā int n = 11;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Vector<Integer> v = new Vector<Integer>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // initialize all the set of digits in a vector
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.add(i, arr[i]);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā findLargest(n, v);
Ā Ā Ā Ā }
}
Ā 
// This code contributed by Rajput-Ji


Python3




# Python 3 implementation of above approach
Ā 
# Function to find the largest
# integer with the given set
def findLargest(n, v):
Ā Ā Ā Ā flag = 0
Ā Ā Ā Ā sum = 0
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # find sum of all the digits
Ā Ā Ā Ā # look if any 0 is present or not
Ā Ā Ā Ā for i in range(n):
Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] == 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1
Ā Ā Ā Ā Ā Ā Ā Ā sum += v[i]
Ā 
Ā Ā Ā Ā # if 0 is not present, the resultant number
Ā Ā Ā Ā # won't be divisible by 5
Ā Ā Ā Ā if (flag == 0):
Ā Ā Ā Ā Ā Ā Ā Ā print("Not possible")
Ā 
Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # sort all the elements in a
Ā Ā Ā Ā Ā Ā Ā Ā # non-decreasing manner
Ā Ā Ā Ā Ā Ā Ā Ā v.sort(reverse = True)
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # if there is just one element 0
Ā Ā Ā Ā Ā Ā Ā Ā if (v[0] == 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print("0")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 0
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # find the remainder of the sum
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # of digits when divided by 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = sum % 3
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # there can a remainder as 1 or 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (y != 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # traverse from the end of the digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i = n - 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while(i >= 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # first element which has the same
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # remainder, remove it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] % 3 == y):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.remove(v[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i -= 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # if there is no element which
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # has a same remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (flag == 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # subtract it by 3 ( could be one or two)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = 3 - y
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt = 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i = n - 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā while(i >= 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # delete two minimal digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # which has a remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] % 3 == y):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.remove(v[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt += 1
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cnt >= 2):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā i -= 1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
Ā Ā Ā Ā 
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # print all the digits as a single integer
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for i in (v):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print(i, end = "")
Ā Ā Ā Ā Ā Ā Ā Ā Ā 
# Driver code
if __name__ == '__main__':
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # initialize the number of set of digits
Ā Ā Ā Ā n = 11
Ā 
Ā Ā Ā Ā # initialize all the set of
Ā Ā Ā Ā # digits in a vector
Ā Ā Ā Ā v = [3, 9, 9, 6, 4, 3,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 6, 4, 9, 6, 0]
Ā 
Ā Ā Ā Ā findLargest(n, v)
Ā Ā Ā Ā Ā 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the above approach
using System;
using System.Collections;
Ā 
class GFG {
Ā 
Ā Ā Ā Ā // Function to find the largest
Ā Ā Ā Ā // integer with the given set
Ā Ā Ā Ā static int findLargest(int n, ArrayList v)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā int flag = 0;
Ā Ā Ā Ā Ā Ā Ā Ā long sum = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // find sum of all the digits
Ā Ā Ā Ā Ā Ā Ā Ā // look if any 0 is present or not
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((int)v[i] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sum += (int)v[i];
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if 0 is not present, the resultant number
Ā Ā Ā Ā Ā Ā Ā Ā // won't be divisible by 5
Ā Ā Ā Ā Ā Ā Ā Ā if (flag != 1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Not possible");
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // sort all the elements in a non-decreasing manner
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.Sort();
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.Reverse();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if there is just one element 0
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((int)v[0] == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("0");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int flags = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find the remainder of the sum
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // of digits when divided by 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y = (int)(sum % 3);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // there can a remainder as 1 or 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (y != 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // traverse from the end of the digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // first element which has the same remainder
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // remove it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((int)v[i] % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.RemoveAt(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flags = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if there is no element which
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // has a same remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (flags == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // subtract it by 3 ( could be one or two)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = 3 - y;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int cnt = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // delete two minimal digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // which has a remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((int)v[i] % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.RemoveAt(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cnt >= 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((int)v[0] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("0");
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print all the digits as a single integer
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < v.Count; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(v[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return int.MinValue;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // Driver code
Ā Ā Ā Ā static void Main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // initialize the number of set of digits
Ā Ā Ā Ā Ā Ā Ā Ā int[] arr = { 3, 9, 9, 6, 4, 3, 6, 4, 9, 6, 0 };
Ā Ā Ā Ā Ā Ā Ā Ā int n = 11;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā ArrayList v = new ArrayList();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // initialize all the set of digits in a vector
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < n; i++)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.Add(arr[i]);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā findLargest(n, v);
Ā Ā Ā Ā }
}
Ā 
// This code contributed by mits


Javascript




<script>
Ā 
// JavaScript implementation of above approach
Ā 
Ā 
// Function to find the largest
// integer with the given set
function findLargest(n,v)
{
Ā 
Ā Ā Ā Ā let flag = 0;
Ā Ā Ā Ā let sum = 0;
Ā 
Ā Ā Ā Ā // find sum of all the digits
Ā Ā Ā Ā // look if any 0 is present or not
Ā Ā Ā Ā for (let i = 0; i <n; i++) {
Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1;
Ā Ā Ā Ā Ā Ā Ā Ā sum += v[i];
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // if 0 is not present, the resultant number
Ā Ā Ā Ā // won't be divisible by 5
Ā Ā Ā Ā if (!flag)
Ā Ā Ā Ā Ā Ā Ā Ā document.write("Not possible","</br>");
Ā 
Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā // sort all the elements in a non-decreasing manner
Ā Ā Ā Ā Ā Ā Ā Ā v.sort((a,b)=>b-a);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if there is just one element 0
Ā Ā Ā Ā Ā Ā Ā Ā if (v[0] == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write("0","</br>");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let flag = 0;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // find the remainder of the sum
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // of digits when divided by 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let y = sum % 3;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // there can a remainder as 1 or 2
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (y != 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // traverse from the end of the digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // first element which has the same remainder
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // remove it
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v = v.splice(i,1);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā flag = 1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // if there is no element which
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // has a same remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (flag == 0) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // subtract it by 3 ( could be one or two)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā y = 3 - y;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let cnt = 0;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i = n - 1; i >= 0; i--) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // delete two minimal digits
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // which has a remainder as y
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[i] % 3 == y) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā v.splice(i,1);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cnt++;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (cnt >= 2)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (v[0] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write("0","</br>");
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // print all the digits as a single integer
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for (let i of v) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}
Ā 
// Driver code
Ā 
// initialize the number of set of digits
let n = 11;
Ā 
// initialize all the set of digits in a vector
let v = [ 3, 9, 9, 6, 4, 3, 6, 4, 9, 6, 0 ];
Ā 
findLargest(n, v);
Ā 
// This code is contributed by shinjanpatra
</script>


Output:Ā 

999666330

Ā 

Time Complexity: O(n log n)

Space Complexity: O(1)

Alternate Solution :Ā 
Below is an implementation by Keegan Fisher, Seattle, WAĀ 
Ā 

C++




#include <bits/stdc++.h>
using namespace std;
Ā 
// return a String representing the largest value that is
// both a combination of the values from the parameter array
// "vals" and divisible by 2, 3, and 5.
string findLargest(int vals[], int N)
{
Ā Ā Ā 
Ā Ā Ā Ā // sort the array in ascending order
Ā Ā Ā Ā sort(vals, vals + N);
Ā Ā Ā Ā string sb;
Ā 
Ā Ā Ā Ā // index of the lowest value divisible by 3 in "vals"
Ā Ā Ā Ā // if not present, no possible value
Ā Ā Ā Ā int index_div_3 = -1;
Ā 
Ā Ā Ā Ā // if a zero is not found, no possible value
Ā Ā Ā Ā bool zero = false;
Ā 
Ā Ā Ā Ā // find minimum multiple of 3 and check for 0
Ā Ā Ā Ā for (int i = 0; i < N; i++)
Ā Ā Ā Ā {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // break when finding the first multiple of 3
Ā Ā Ā Ā Ā Ā Ā Ā // which is minimal due to sort in asc order
Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] % 3 == 0 && vals[i] != 0)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zero = true;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // if no multiple of 3 or no zero, then no value
Ā Ā Ā Ā if (index_div_3 == -1 || !zero)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā return sb;
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // construct the output StringBuilder
Ā Ā Ā Ā // adding the values to the string from highest
Ā Ā Ā Ā // to lowest, not adding the val[index_div_3]
Ā Ā Ā Ā // until reaching the 0s in the array, at which
Ā Ā Ā Ā // point add the multiple of 3 followed by
Ā Ā Ā Ā // any 0s in the array
Ā Ā Ā Ā for (int i = N - 1; i >= 0; i--)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā if (i != index_div_3)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0 && index_div_3 != -1)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb = sb + to_string(vals[index_div_3]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb = sb + to_string(vals[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb = sb + to_string(vals[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā Ā Ā Ā return sb;
}
Ā Ā Ā Ā Ā 
int main()
{
Ā Ā Ā Ā int vals[] = { 0, 0, 0, 2, 3, 4, 2, 7, 6, 9 };
Ā Ā Ā Ā int N = sizeof(vals) / sizeof(vals[0]);
Ā Ā Ā Ā cout << "Output = " << findLargest(vals, N) << endl;
Ā 
Ā Ā Ā Ā return 0;
}
Ā 
// This code is contributed by divyeshrabadiya07


Java




import java.util.Arrays;
Ā 
class GFG {
Ā Ā Ā Ā // Driver
Ā Ā Ā Ā public static void main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int[] vals = { 0, 0, 0, 2, 3, 4, 2, 7, 6, 9 };
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("Output = " + findLargest(vals));
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // return a String representing the largest value that is
Ā Ā Ā Ā // both a combination of the values from the parameter array
Ā Ā Ā Ā // "vals" and divisible by 2, 3, and 5.
Ā Ā Ā Ā public static String findLargest(int[] vals)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // sort the array in ascending order
Ā Ā Ā Ā Ā Ā Ā Ā Arrays.sort(vals);
Ā Ā Ā Ā Ā Ā Ā Ā StringBuilder sb = new StringBuilder();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // index of the lowest value divisible by 3 in "vals"
Ā Ā Ā Ā Ā Ā Ā Ā // if not present, no possible value
Ā Ā Ā Ā Ā Ā Ā Ā int index_div_3 = -1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if a zero is not found, no possible value
Ā Ā Ā Ā Ā Ā Ā Ā boolean zero = false;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // find minimum multiple of 3 and check for 0
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < vals.length; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // break when finding the first multiple of 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // which is minimal due to sort in asc order
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] % 3 == 0 && vals[i] != 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zero = true;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if no multiple of 3 or no zero, then no value
Ā Ā Ā Ā Ā Ā Ā Ā if (index_div_3 == -1 || !zero) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return sb.toString();
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // construct the output StringBuilder
Ā Ā Ā Ā Ā Ā Ā Ā // adding the values to the string from highest
Ā Ā Ā Ā Ā Ā Ā Ā // to lowest, not adding the val[index_div_3]
Ā Ā Ā Ā Ā Ā Ā Ā // until reaching the 0s in the array, at which
Ā Ā Ā Ā Ā Ā Ā Ā // point add the multiple of 3 followed by
Ā Ā Ā Ā Ā Ā Ā Ā // any 0s in the array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = vals.length - 1; i >= 0; i--) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i != index_div_3) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0 && index_div_3 != -1) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.append(vals[index_div_3]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.append(vals[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.append(vals[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return sb.toString();
Ā Ā Ā Ā }
}


Python3




# Return a String representing the largest
# value that is both a combination of the
# values from the parameter array
# "vals" and divisible by 2, 3, and 5.
def findLargest (vals):
Ā 
Ā Ā Ā Ā # Sort the array in ascending order
Ā Ā Ā Ā vals.sort()
Ā Ā Ā Ā sb = ""
Ā 
Ā Ā Ā Ā # Index of the lowest value divisible
Ā Ā Ā Ā # by 3 in "vals" if not present, no
Ā Ā Ā Ā # possible value
Ā Ā Ā Ā index_div_3 = -1
Ā 
Ā Ā Ā Ā # If a zero is not found, no possible value
Ā Ā Ā Ā zero = False
Ā 
Ā Ā Ā Ā # Find minimum multiple of 3 and check for 0
Ā Ā Ā Ā for i in range(len(vals)):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā # Break when finding the first
Ā Ā Ā Ā Ā Ā Ā Ā # multiple of 3 which is minimal
Ā Ā Ā Ā Ā Ā Ā Ā # due to sort in asc order
Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] % 3 == 0 and vals[i] != 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zero = True
Ā 
Ā Ā Ā Ā # If no multiple of 3 or no zero, then no value
Ā Ā Ā Ā if (index_div_3 == -1 or zero == False):
Ā Ā Ā Ā Ā Ā Ā Ā return str(sb)
Ā 
Ā Ā Ā Ā # Construct the output String by
Ā Ā Ā Ā # adding the values to the string from highest
Ā Ā Ā Ā # to lowest, not adding the val[index_div_3]
Ā Ā Ā Ā # until reaching the 0s in the array, at which
Ā Ā Ā Ā # point add the multiple of 3 followed by
Ā Ā Ā Ā # any 0s in the array
Ā Ā Ā Ā for i in range(len(vals) - 1, -1, -1):
Ā Ā Ā Ā Ā Ā Ā Ā if (i != index_div_3):
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0 and index_div_3 != -1):
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb += str(vals[index_div_3])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb += str(vals[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb += str(vals[i])
Ā 
Ā Ā Ā Ā return str(sb)
Ā 
# Driver code
if __name__ == '__main__':
Ā 
Ā Ā Ā Ā vals = [ 0, 0, 0, 2, 3, 4, 2, 7, 6, 9 ]
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā print("Output =", findLargest(vals))
Ā 
# This code is contributed by himanshu77


C#




using System;
using System.Text;
class GFG
{
Ā Ā Ā Ā // Driver
Ā Ā Ā Ā public static void Main()
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā int[] vals = { 0, 0, 0, 2, 3, 4, 2, 7, 6, 9 };
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine("Output = " + findLargest(vals));
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // return a String representing the largest value that is
Ā Ā Ā Ā // both a combination of the values from the parameter array
Ā Ā Ā Ā // "vals" and divisible by 2, 3, and 5.
Ā Ā Ā Ā public static string findLargest(int[] vals)
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā // sort the array in ascending order
Ā Ā Ā Ā Ā Ā Ā Ā Array.Sort(vals);
Ā Ā Ā Ā Ā Ā Ā Ā StringBuilder sb = new StringBuilder();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // index of the lowest value divisible by 3 in "vals"
Ā Ā Ā Ā Ā Ā Ā Ā // if not present, no possible value
Ā Ā Ā Ā Ā Ā Ā Ā int index_div_3 = -1;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if a zero is not found, no possible value
Ā Ā Ā Ā Ā Ā Ā Ā bool zero = false;
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // find minimum multiple of 3 and check for 0
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < vals.Length; i++) {
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // break when finding the first multiple of 3
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // which is minimal due to sort in asc order
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] % 3 == 0 && vals[i] != 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = i;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zero = true;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // if no multiple of 3 or no zero, then no value
Ā Ā Ā Ā Ā Ā Ā Ā if (index_div_3 == -1 || !zero) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return sb.ToString();
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // construct the output StringBuilder
Ā Ā Ā Ā Ā Ā Ā Ā // adding the values to the string from highest
Ā Ā Ā Ā Ā Ā Ā Ā // to lowest, not adding the val[index_div_3]
Ā Ā Ā Ā Ā Ā Ā Ā // until reaching the 0s in the array, at which
Ā Ā Ā Ā Ā Ā Ā Ā // point add the multiple of 3 followed by
Ā Ā Ā Ā Ā Ā Ā Ā // any 0s in the array
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = vals.Length - 1; i >= 0; i--) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (i != index_div_3) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0 && index_div_3 != -1) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.Append(vals[index_div_3]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.Append(vals[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = -1;
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.Append(vals[i]);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā return sb.ToString();
Ā Ā Ā Ā }
}
Ā 
// This code is contributed by SoumikMondal


Javascript




<script>
// Return a String representing the largest
// value that is both a combination of the
// values from the parameter array
// "vals" and divisible by 2, 3, and 5.
function findLargest (vals){
Ā 
Ā Ā Ā Ā // Sort the array in ascending order
Ā Ā Ā Ā vals.sort()
Ā Ā Ā Ā let sb = ""
Ā 
Ā Ā Ā Ā // Index of the lowest value divisible
Ā Ā Ā Ā // by 3 in "vals" if not present, no
Ā Ā Ā Ā // possible value
Ā Ā Ā Ā let index_div_3 = -1
Ā 
Ā Ā Ā Ā // If a zero is not found, no possible value
Ā Ā Ā Ā let zero = false
Ā 
Ā Ā Ā Ā // Find minimum multiple of 3 and check for 0
Ā Ā Ā Ā for(let i=0;i<vals.length;i++){
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Break when finding the first
Ā Ā Ā Ā Ā Ā Ā Ā // multiple of 3 which is minimal
Ā Ā Ā Ā Ā Ā Ā Ā // due to sort in asc order
Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] % 3 == 0 && vals[i] != 0){
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = i
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā break
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0)
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zero = true
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // If no multiple of 3 or no zero, then no value
Ā Ā Ā Ā if (index_div_3 == -1 || zero == false)
Ā Ā Ā Ā Ā Ā Ā Ā return sb
Ā 
Ā Ā Ā Ā // Construct the output String by
Ā Ā Ā Ā // adding the values to the string from highest
Ā Ā Ā Ā // to lowest, not adding the val[index_div_3]
Ā Ā Ā Ā // until reaching the 0s in the array, at which
Ā Ā Ā Ā // point add the multiple of 3 followed by
Ā Ā Ā Ā // any 0s in the array
Ā Ā Ā Ā for(let i=vals.length - 1;i>=0;i--){
Ā Ā Ā Ā Ā Ā Ā Ā if (i != index_div_3){
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (vals[i] == 0 && index_div_3 != -1){
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb += parseInt(vals[index_div_3])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb += parseInt(vals[i])
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā index_div_3 = -1
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā else
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb += parseInt(vals[i])
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā return sb
}
Ā 
// Driver code
let vals = [ 0, 0, 0, 2, 3, 4, 2, 7, 6, 9 ]
document.write("Output =", findLargest(vals))
Ā 
// This code is contributed by shinjanpatra
Ā 
</script>


Output:Ā 

Output = 9764223000

Ā 

Time Complexity: O(n log n)

Space Complexity: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments