Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:19:19+00:00 2026-06-11T20:19:19+00:00

E.g.: Array: 4,3,0,1,5 {Assume all digits are >=0. Also each element in array correspond

  • 0

E.g.: Array: 4,3,0,1,5 {Assume all digits are >=0. Also each element in array correspond to a digit. i.e. each element on the array is between 0 and 9. }

In the above array, the largest number is: 5430 {using digits 5, 4, 3 and 0 from the array}

My Approach:

For divisibility by 3, we need the sum of digits to be divisible by 3.
So,

  1. Step-1: Remove all the zeroes from the array.
  2. Step-2: These zeroes will come at the end. {Since they dont affect the sum and we have to find the largest number}
  3. Step-3: Find the subset of the elements of array (excluding zeroes) such that the number of digits is MAXIMUM and also that the sum of digits is MAXIMUM and the sum is divisible by 3.
  4. STEP-4: The required digit consists of the digits in the above found set in decreasing order.

So, the main step is STEP-3 i.e. How to find the subset such that it contains MAXIMUM possible number of elements such that their sum is MAX and is divisible by 3 .

I was thinking, maybe Step-3 could be done by GREEDY CHOICE of taking all the elements and keep on removing the smallest element in the set till the sum is divisible by 3.

But i am not convinced that this GREEDY choice will work.

Please tell if my approach is correct.
If it is, then please suggest as to how to do Step-3 ?

Also, please suggest any other possible/efficient algorithm.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:19:21+00:00Added an answer on June 11, 2026 at 8:19 pm

    Observation: If you can get a number that is divisible by 3, you need to remove at most 2 numbers, to maintain optimal solution.

    A simple O(n^2) solution will be to check all possibilities to remove 1 number, and if none is valid, check all pairs (There are O(n^2) of those).


    EDIT:

    O(n) solution: Create 3 buckets – bucket1, bucket2, bucket0. Each will denote the modulus 3 value of the numbers. Ignore bucket0 in the next algorithm.

    Let the sum of the array be sum.

    If sum % 3 ==0: we are done.
    else if sum % 3 == 1:
      if there is a number in bucket1 - chose the minimal
      else: take 2 minimals from bucket 2
    else if sum % 3 == 2
      if there is a number in bucket2 - chose the minimal
      else: take 2 minimals from bucket1  
    

    Note: You don’t actually need the bucket, to achieve O(1) space – you need only the 2 minimal values from bucket1 and bucket2, since it is the only number we actually used from these buckets.

    Example:

    arr = { 3, 4, 0, 1, 5 }
    bucket0 = {3,0} ; bucket1 = {4,1} bucket2 = { 5 }
    sum = 13 ; sum %3 = 1
    bucket1 is not empty - chose minimal from it (1), and remove it from the array.
    result array = { 3, 4, 0, 5 } 
    proceed to STEP 4 "as planned"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume that we have multiple arrays of integers. You can consider each array as
Assume that the array has integers between 1 and 1,000,000. I know some popular
How can I convert the following array: (Assume I don't know how many depth
Assume a master array which is already sorted in ASCENDING order: $values = array('value1',
Assume that int array arrayName is a member of class className, How can I
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
Let's assume there is a static .Net method static void RegisterDelegate(Action<array<Object^>^>^ handler); defined in
I am playing around with multidimensional array of unequal second dimension size. Lets assume
Instead of loading all annotations that are in my array, I would only like
An array of N elements is given indexed from 1 to N. All elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.