Question

Given an array of non-negative integers. Find the largest multiple of 3 that can be formed from array elements.

For example, if the input array is {8, 1, 9}, the output should be “9 8 1″, and if the input array is {8, 1, 7, 6, 0}, output should be “8 7 6 0″.

Solution

A number is multiple of 3 if and only if the sum of digits of number is multiple of 3.

1. Sort the array in non-decreasing order.

2. Take three queues. One for storing elements which on dividing by 3 gives remainder as 0.The second queue stores digits which on dividing by 3 gives remainder as 1. The third queue stores digits which on dividing by 3 gives remainder as 2. Call them as queue0, queue1 and queue2

3. Find the sum of all the digits.

4. Three cases arise:
……4.1 The sum of digits is divisible by 3. Dequeue all the digits from the three queues. Sort them in non-increasing order. Output the array. ……4.2 The sum of digits produces remainder 1 when divided by 3.
Remove one item from queue1. If queue1 is empty, remove two items from queue2. If queue2 contains less than two items, the number is not possible. ……4.3 The sum of digits produces remainder 2 when divided by 3.
Remove one item from queue2. If queue2 is empty, remove two items from queue1. If queue1 contains less than two items, the number is not possible. 5. Finally empty all the queues into an auxiliary array. Sort the auxiliary array in non-increasing order. Output the auxiliary array.

Find the largest multiple of 3 解答的更多相关文章

  1. Largest Rectangle in Histogram 解答

    Question Given n non-negative integers representing the histogram's bar height where the width of ea ...

  2. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  3. LeetCode(四)

    Find Kth Largest Number public class Solution { public int findKthLargest(int[] nums, int k) { retur ...

  4. Code Signal_练习题_adjacentElementsProduct

    Given an array of integers, find the pair of adjacent elements that has the largest product and retu ...

  5. Kth Largest Element in an Array 解答

    Question Find the kth largest element in an unsorted array. Note that it is the kth largest element ...

  6. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  7. [LeetCode] Largest Divisible Subset 最大可整除的子集合

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. Leetcode 368. Largest Divisible Subset

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  9. 二分套二分 hrbeu.acm.1211Kth Largest

    Kth Largest TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Description There are two sequences A and ...

随机推荐

  1. poj1185:炮兵阵地(状压dp)

    也算是比较基础的状压dp了,跟做过的第二道比较又稍微复杂了一点 需要记录之前两行的状态.. 统计结果也稍有不同 另外还学习了一个得到一个整数二进制位 1 的个数的位运算方法 详见代码: #includ ...

  2. poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  3. ubuntu14.04折腾迅雷xware

    迅雷一直没有出linux版,wine不想去弄.linux下虽然也有各种bt软件,无奈我试用后却发现速度远比不上迅雷,甚至有些资源根本找不到.而有些迅雷的专用链接,更是没法下(原谅我2M的小水管,却喜欢 ...

  4. TPCC-UVA测试环境搭建与结果分析

    一.    准备 操作系统 :Linux, 内核版本2.6 需要软件:tpccuva-1.2.3, postgresql-8.1.15, gnuplot-4.2.5. tccuva是实现标准TPC-C ...

  5. 利用iptables实现基于端口的网络流量统计

    如何统计某个应用的网络流量(包括网络流入量和网络流出量)问题,可以转换成如何基于端口号进行网络流量统计的问题.大部分网络应用程序都是传输层及以上的协议,因此基于端口号(tcp, udp)统计网络流量基 ...

  6. [转载]C#开源项目(国外的还是很多)

    C#开源项目(国外的还是很多)一.Ajax框架Ajax.NET Professional (AjaxPro)是最先把AJAX技术在微软.NET环境下的实现的AJAX框架之一.它在客户端脚本之上创建代理 ...

  7. 做量化模型Matlab、R、Python、F#和C++到底选择哪一个?

    MATLAB是matrix&laboratory两个词的组合,意为矩阵工厂(矩阵实验室).是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它将数 ...

  8. Jquery:强大的选择器<一>

    今天回家之后,学习的是Jquery的选择器.选择器作为Jquery的优势之一,确实让我感觉到了它的强大.Jquery选择器分为基本选择器.层次选择器.过滤选择器和表单选择器,下面我一一介绍这四种选择器 ...

  9. Truncate Table user

    Truncate Table   百科名片 Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用. 目录 语法 参数 注释 示例 ...

  10. Oracle11g R2学习系列 之五回闪

    Oracle里面有一个回闪的操作,这个貌似sql server是没有的.要使用这个功能,需要用到两个时间内部函数 TIMESTAMP和TO_TIMESTAMP.其中,函数TO_TIMESTAMP的语法 ...