You are on page 1of 33

<!DOCTYPE html> <!-- saved from url=(0096)http://stackoverflow.

com/questions/16605991/number-ofsubarrays-divisible-by-k/16606634#16606634 --> <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>arrays - Number of subarrays divisible by k - Stack Overflow</title> <link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/fav icon.ico"> <link rel="apple-touch-icon image_src" href="http://cdn.sstatic.net/stackove rflow/img/apple-touch-icon.png"> <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="http://stackoverflow.com/opensearch.xml"> <script type="text/javascript" async="" src="./arrays - Number of subarrays divisible by k - Stack Overflow_files/adzerk1_2_4_43,adzerk2_2_17_45,adzerk3_2_4 _44"></script><script type="text/javascript" async="" src="./arrays - Number of subarrays divisible by k - Stack Overflow_files/ga.js"></script><script type="te xt/javascript" async="" src="./arrays - Number of subarrays divisible by k - Sta ck Overflow_files/quant.js"></script><script type="text/javascript" src="./array s - Number of subarrays divisible by k - Stack Overflow_files/jquery.min.js"></s cript> <script type="text/javascript" src="./arrays - Number of subarrays divisible by k - Stack Overflow_files/stub.js"></script> <link rel="stylesheet" type="text/css" href="./arrays - Number of subarrays divisible by k - Stack Overflow_files/all.css"> <link rel="canonical" href="http://stackoverflow.com/questions/16605991/numb er-of-subarrays-divisible-by-k"> <link rel="alternate" type="application/atom+xml" title="Feed for question & #39;Number of subarrays divisible by k&#39;" href="http://stackoverflow.com/feed s/question/16605991"> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.using("postValidation", function () { StackExchange.postValidation.initOnBlurAndSubmit($('#post-form') , 2, 'answer'); }); StackExchange.question.init({showAnswerHelp:true,totalCommentCount:0 ,shownCommentCount:0,highlightColor:'#F4A83D',backgroundColor:'#FFF',questionId: 16605991}); styleCode(); StackExchange.realtime.subscribeToQuestion('1', '16605991'); }); </script> <script type="text/javascript"> StackExchange.init({"stackAuthUrl":"https://stackauth.com","serverTime": 1372754496,"styleCode":true,"enableUserHovercards":true,"site":{"name":"Stack Ov erflow","description":"Q&A for professional and enthusiast programmers","isNotic esTabEnabled":true,"recaptchaPublicKey":"6LdchgIAAAAAAJwGpIzRQSOFaO0pU6s44Xt8aTw c","enableSocialMediaInSharePopup":true},"user":{"fkey":"9336b12e86af343415687d9 e813eec5f","isAnonymous":true}});

StackExchange.using.setCacheBreakers({"js/prettify-full.js":"6c261bebf56 a","js/moderator.js":"68a5f45d34d6","js/full-anon.js":"761e0ff892e6","js/full.js ":"b16edc73d2d1","js/wmd.js":"2f79c03846d5","js/third-party/jquery.autocomplete. min.js":"e5f01e97f7c3","js/mobile.js":"e8e23ad37820","js/help.js":"6e6623243cf6" ,"js/tageditor.js":"450c9e8426fc","js/tageditornew.js":"b6c68ad4c7dd","js/inline -tag-editing.js":"8e84e8a137f7","js/revisions.js":"7273bb714bba","js/review.js": "59e9b73580be","js/tagsuggestions.js":"aa48ef6154df","js/post-validation.js":"bb 996020492a","js/explore-qlist.js":"1c5bbd79b562"}); </script> <script type="text/javascript"> StackExchange.using("gps", function() { StackExchange.gps.init(true); }); </script> <script type="text/javascript"> StackExchange.ready(function () { $('#nav-tour').click(function () { StackExchange.using("gps", function() { StackExchange.gps.track("aboutpage.click", { aboutclick_ location: "headermain" }, true); }); }); }); </script> <script async="" type="text/javascript" src="./arrays - Number of subarrays divi sible by k - Stack Overflow_files/full-anon.js"></script><script async="" type=" text/javascript" src="./arrays - Number of subarrays divisible by k - Stack Over flow_files/post-validation.js"></script><script async="" type="text/javascript" src="./arrays - Number of subarrays divisible by k - Stack Overflow_files/pretti fy-full.js"></script><script type="text/javascript" src="./arrays - Number of su barrays divisible by k - Stack Overflow_files/adFeedback.js"></script><link rel= "stylesheet" href="http://static.adzerk.net/Extensions/adFeedback.css"><script t ype="text/javascript">//cdn test - don't delete! </script><link href="./arrays - Number of subarrays divisible by k - Stack Overf low_files/sidebar.min.css" type="text/css" rel="stylesheet"></head> <body class="question-page"><div style="position: absolute;"><object id="_GPL_sw f" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://downloa d.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="1" height="1"><param name="movie" value="http://contentcache-a.akamaihd.net/items/ it/swf/f.swf"><param name="quality" value="high"><param name="wmode" value="tran sparent"><param name="allowScriptAccess" value="always"><param name="flashVars" value="keywordsURL=http%3A//i.tracksrv.com/kwd%3Fc%3DOjo6c3RhY2tvdmVyZmxvdy5jb20 6ei0xMDIxLTE3MzQ5MQ%253D%253D%26cb%3D_GPL.items.a652c.displayKeywords&amp;keywor ds=stack%20overflow%7Ca%20question%20and%20answer%20site%20for%20professional%20 and%20enthusiast%20programmers%7Cit%27s%7Cfree%7Cno%20registration%20required%7C had%20the%20following%20question%20in%20an%20interview%20and%7Cin%20spite%20of%2 0the%20fact%20that%7Cgave%20a%20working%20implementation%7Cefficient%20enough%7C a%20slice%20of%20array%20a%7Cany%20pair%20of%20integers%7Csuch%20that%7Cdivisibl e%20by%7Cif%20the%20number%7Cthe%20function%7Cwas%20asked%20to%20write%7Chad%20t o%20return%20the%20number%20of%20slices%20divisible%20by%7Cthe%20expected%20time %20complexity%20was%7Cmax%7Cand%20space%20complexity%20was%7Cmy%20solution%20was %20the%20simplest%7Cone%20loop%20inside%20another%20and%20check%20every%20slice% 7Chave%20been%20thinking%20but%7Creally%7Cfigure%20out%20how%20can%7Cmay%20be%20 a%20variant%20of%20the%7Cknow%20how%20to%20count%20every%20subarray%7Cedit%7Cele ments%20in%20array%20could%20be%20negatives%7Chere%7Can%20example%7Cfunction%20m ust%20return%7Cbecause%20there%20are%7Csubarrays%20which%20sums%20are%20divisibl e%20by%7Cat%7Caccepted%7Cas%20you%20are%20only%20interested%20in%20numbers%20div isible%20by%7Cyou%20can%7Call%20computations%20modulo%7Cconsider%20the%20cumulat

ive%20sum%20array%7Cthen%7Ca%20slice%20divisible%20by%7Ciff%7Cremember%20we%7Cso %20you%20just%20have%20to%20count%20for%20each%20possible%20value%20of%7Chow%20m any%20times%7Cappears%20in%7Csome%20pseudocode%7Cnew%20array%7Cto%20n-1%7Cans%7C to%20k-1%20ans%7Conce%20you%20know%20that%20they%20are%7Ccells%20in%7Cthat%20hav e%20value%7Cyou%20want%20to%20count%20the%20number%20of%20slices%20the%20start%2 0in%20a%20cell%20with%20value%7Cand%20ends%20in%20a%20cell%20with%20value%7Cthis %20number%7Cx-1%7Cto%20solve%20edge%20problems%7Cwe%20add%20one%20cell%20with%20 value%7Canswered%7C366%7Csorry%7Cforgot%20to%20say%20that%20elements%20in%20arra y%20could%20be%20negatives%7Cchange%20anything%7Cstill%20valid%7Cthank%20you%20v ery%20much%20for%20your%20answer%7Cknow%20if%7Cget%7Cwork%7Cprove%20with%20for%2 0example%20array%7Cand%20the%20result%7Cwhen%7Cshould%20be%7Ccould%20you%20expla in%20a%20little%20bit%20more%20your%20answer%7Cindeed%20the%20last%20line%7Ccoun ts%20only%20splices%20of%20size%7Chave%20modified%7Cwork%20with"><!--[if !IE]> < --> <object id="_GPL_swf" data="http://contentcache-a.akamaihd.net/items/it/swf/ f.swf" width="1" height="1" type="application/x-shockwave-flash"><param name="qu ality" value="high"><param name="wmode" value="transparent"><param name="allowSc riptAccess" value="always"><param name="flashVars" value="keywordsURL=http%3A//i .tracksrv.com/kwd%3Fc%3DOjo6c3RhY2tvdmVyZmxvdy5jb206ei0xMDIxLTE3MzQ5MQ%253D%253D %26cb%3D_GPL.items.a652c.displayKeywords&amp;keywords=stack%20overflow%7Ca%20que stion%20and%20answer%20site%20for%20professional%20and%20enthusiast%20programmer s%7Cit%27s%7Cfree%7Cno%20registration%20required%7Chad%20the%20following%20quest ion%20in%20an%20interview%20and%7Cin%20spite%20of%20the%20fact%20that%7Cgave%20a %20working%20implementation%7Cefficient%20enough%7Ca%20slice%20of%20array%20a%7C any%20pair%20of%20integers%7Csuch%20that%7Cdivisible%20by%7Cif%20the%20number%7C the%20function%7Cwas%20asked%20to%20write%7Chad%20to%20return%20the%20number%20o f%20slices%20divisible%20by%7Cthe%20expected%20time%20complexity%20was%7Cmax%7Ca nd%20space%20complexity%20was%7Cmy%20solution%20was%20the%20simplest%7Cone%20loo p%20inside%20another%20and%20check%20every%20slice%7Chave%20been%20thinking%20bu t%7Creally%7Cfigure%20out%20how%20can%7Cmay%20be%20a%20variant%20of%20the%7Cknow %20how%20to%20count%20every%20subarray%7Cedit%7Celements%20in%20array%20could%20 be%20negatives%7Chere%7Can%20example%7Cfunction%20must%20return%7Cbecause%20ther e%20are%7Csubarrays%20which%20sums%20are%20divisible%20by%7Cat%7Caccepted%7Cas%2 0you%20are%20only%20interested%20in%20numbers%20divisible%20by%7Cyou%20can%7Call %20computations%20modulo%7Cconsider%20the%20cumulative%20sum%20array%7Cthen%7Ca% 20slice%20divisible%20by%7Ciff%7Cremember%20we%7Cso%20you%20just%20have%20to%20c ount%20for%20each%20possible%20value%20of%7Chow%20many%20times%7Cappears%20in%7C some%20pseudocode%7Cnew%20array%7Cto%20n-1%7Cans%7Cto%20k-1%20ans%7Conce%20you%2 0know%20that%20they%20are%7Ccells%20in%7Cthat%20have%20value%7Cyou%20want%20to%2 0count%20the%20number%20of%20slices%20the%20start%20in%20a%20cell%20with%20value %7Cand%20ends%20in%20a%20cell%20with%20value%7Cthis%20number%7Cx-1%7Cto%20solve% 20edge%20problems%7Cwe%20add%20one%20cell%20with%20value%7Canswered%7C366%7Csorr y%7Cforgot%20to%20say%20that%20elements%20in%20array%20could%20be%20negatives%7C change%20anything%7Cstill%20valid%7Cthank%20you%20very%20much%20for%20your%20ans wer%7Cknow%20if%7Cget%7Cwork%7Cprove%20with%20for%20example%20array%7Cand%20the% 20result%7Cwhen%7Cshould%20be%7Ccould%20you%20explain%20a%20little%20bit%20more% 20your%20answer%7Cindeed%20the%20last%20line%7Ccounts%20only%20splices%20of%20si ze%7Chave%20modified%7Cwork%20with"></object> <!----> <!--[endif]----> </object> </div><div style="position: absolute; top: 0px; left: 0px; width: 1px; height: 1 px; z-index: 2147483647;" id="_GPL_e6a00_parent_div"><object type="application/x -shockwave-flash" id="_GPL_e6a00_swf" data="http://contentcache-a.akamaihd.net/i tems/e6a00/storage.swf?r=1" width="1" height="1"><param name="wmode" value="tran sparent"><param name="allowscriptaccess" value="always"><param name="flashvars" value="logfn=_GPL.items.e6a00.log&amp;onload=_GPL.items.e6a00.onload&amp;onerror =_GPL.items.e6a00.onerror&amp;LSOName=gpl"></object></div> <noscript>&lt;div id="noscript-padding"&gt;&lt;/div&gt;</noscript> <div id="notify-container"></div> <div id="overlay-header"></div> <div id="custom-header"></div> <div class="container">

<div id="header" class="headeranon"> <div id="portalLink"> <a class="genu" onclick="StackExchange.ready(function(){genuwine .click();});return false;">Stack Exchange</a> </div> <div id="topbar"> <div id="hlinks"> <span id="hlinks-user"></span> <span id="hlinks-nav"> <a href="http://stackoverflow.com/ users/login?returnurl=%2fquestions%2f16605991%2fnumber-of-subarrays-divisible-by -k%2f16606634">sign up</a> <span class="lsep">|</span> <a href="http://stackoverflow.com/users/login?returnurl=%2fq uestions%2f16605991%2fnumber-of-subarrays-divisible-by-k%2f16606634">log in</a> <span class="lsep">|</span> <a href="http://careers.stackoverflow.com/">careers 2.0</a> <span class="lsep">|</span> </span> <span id="hlinks-custom"></span> </div> <div id="hsearch"> <form id="search" action="http://stackoverflow.com/search" m ethod="get" autocomplete="off"> <div> <input autocomplete="off" name="q" class="textbox" p laceholder="search" tabindex="1" type="text" maxlength="240" size="28" value=""> </div> </form> </div> </div> <br class="cbt"> <div id="hlogo"> <a href="http://stackoverflow.com/"> Stack Overflow </a> </div> <div id="hmenus"> <div class="nav mainnavs mainnavsanon"> <ul> <li class="youarehere"><a id="nav-questions" href="h ttp://stackoverflow.com/questions">Questions</a></li> <li><a id="nav-tags" href="http://stackoverflow.com/ tags">Tags</a></li> <li><a id="nav-tour" href="http://stackoverflow.com/ about">Tour</a></li> <li><a id="nav-users" href="http://stackoverflow.com /users">Users</a></li> </ul> </div> <div class="nav askquestion"> <ul> <li> <a id="nav-askquestion" href="http://stackoverflow.c om/questions/ask">Ask Question</a> </li> </ul>

</div> </div> </div>

<div id="content"> <div itemscope="" itemtype="http://schema.org/Article"> <link itemprop="image" href="http://cdn.sstatic.net/stackoverflow/img/apple-touc h-icon.png"> <!--googleoff: snippet--> <div id="herobox-mini"> <div id="hero-content"> <span id="controls"> <a href="http://stackoverflow.com/about" id="tell-me-more" class="bu tton">Tell me more</a> <span id="close"><a title="click to dismiss"></a></span> </span> <div id="blurb"> <span id="site-name">Stack Overflow</span> is a question and answer site for professional and enthusiast programmers. It's 100% free, no registra tion required. </div> </div> <script> $('#tell-me-more').click(function () { var clickSource = $("body").attr("class") + '-mini'; if ($("body").hasClass("questions-page")) { clickSource = 'questionpagemini'; } if ($("body").hasClass("home-page")) { clickSource = 'homepagemini'; } StackExchange.using("gps", function () { StackExchange.gps.track("aboutpage.click", { aboutclick_location : clickSource } , true); }); }); $('#herobox-mini #close').click(function () { StackExchange.using("gps", function () { StackExchange.gps.track("hero.action", { hero_action_type: "clos e" }, true); }); $.cookie("hero", "none", { path: "/" }); var $hero = $("#herobox-mini"); $hero.slideUp('fast', function () { $hero.remove(); }); return false; }); </script> </div> <!--googleon: snippet--> <div id="question-header"> <h1 itemprop="name"><a href="http://stackoverflow.com/questions/16605991/num ber-of-subarrays-divisible-by-k" class="question-hyperlink">Number of subarrays divisible by k</a></h1>

</div> <div id="mainbar">

<div class="question" data-questionid="16605991" id="question"> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk1" ><a href="http://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giO jExNzgsImNyIjoyNTY1LCJkaSI6ImYyYjE5NDQxYjhkZDQxZGNhOTMwNDNjNjlmMmIwNWQwIiwiZG0iO jEsImZjIjo0MjE1LCJmbCI6MjQ0NCwia3ciOiJhcnJheXMsYWxnb3JpdGhtLHN1YnNldC1zdW0sY29ka WxpdHkiLCJudyI6MjIsInJmIjoiaHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tL3RhZ3MvY29kaWxpdHkva G90IiwicnYiOjAsInByIjoxNTY4LCJzdCI6ODI3Nywiem4iOjQzLCJ1ciI6Imh0dHA6Ly9jYXJlZXJzL nN0YWNrb3ZlcmZsb3cuY29tLz9ibWlkPTFiIn0&s=23_6m7TBuZEfLLMub8dFG59vm40" rel="nofol low" target="_blank" title=""><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_files/2565.png" title="" alt="" border="0" width="728" he ight="90"></a><div class="adzerk-vote-controls" style="display: none;"><div clas s="adzerk-vote-option adzerk-vote-up"><div class="adzerk-vote-icon"></div></div> <div class="adzerk-vote-option adzerk-vote-down"><div class="adzerk-vote-icon">< /div></div></div><div class="adzerk-vote-survey" style="display:none;"><form><sp an>No problem. We won't show you that ad again. Why didn't you like it?</span><u l><li><label><input type="radio" value="12" name="downvoteReason">Uninteresting< /label></li><li><label><input type="radio" value="13" name="downvoteReason">Misl eading</label></li><li><label><input type="radio" value="14" name="downvoteReaso n">Offensive</label></li><li><label><input type="radio" value="15" name="downvot eReason">Repetitive</label></li></ul><a href="http://stackoverflow.com/questions /16605991/number-of-subarrays-divisible-by-k/16606634#" class="adzerk-vote-cance l">Oops! I didn't mean to do this.</a></form></div><img height="0px" width="0px" border="0" src="./arrays - Number of subarrays divisible by k - Stack Overflow_ files/i.gif"></div> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="16605991"> <a class="vote-up-off" title="This question shows research effort; it is use ful and clear">up vote</a> <span class="vote-count-post ">3</span> <a class="vote-down-off" title="This question does not show any research eff ort; it is unclear or not useful">down vote</a> <a class="star-off" href="http://stackoverflow.com/questions/16605991/number -of-subarrays-divisible-by-k/16606634#" title="This is a favorite question (clic k again to undo)">favorite</a> <div class="favoritecount"><b></b></div> </div> </td> <td class="postcell"> <div> <div class="post-text" itemprop="description"> <p>I had the following question in an interview and, in spite of the fac t that I gave a working implementation, it wasn't efficient enough. </p>

<blockquote> <p>A slice of array A is any pair of integers (P, Q) such that 0 P Q &lt; N. A slice (P, Q) of array A is divisible by K if the number A[P] + A[P+1] + ... + A[Q-1] + A[Q] is divisible by K.</p> </blockquote> <p>The function I was asked to write, had to return the number of slices divisib le by K. The expected time complexity was O(max(N, K)) and space complexity was O(K).</p> <p>My solution was the simplest, one loop inside another and check every slice: O(n^2)</p> <p>I have been thinking but I really can't figure out how can I do it in O(max(N , K)). </p> <p>It may be a variant of the <a href="http://en.wikipedia.org/wiki/Subset_sum_p roblem" rel="nofollow">subset sum problem</a>, but I don't know how to count eve ry subarray. </p> <p>EDIT: Elements in array could be negatives. Here is an example:</p> <pre><code>A = {4, 5, 0, -2, -3, 1}, K = 5 Function must return 7, because there are 7 subarrays which sums are divisible b y 5 {4, 5, 0, -2, -3, 1} {5} {5, 0} {5, 0, -2, -3} {0} {0, -2, -3} {-2, -3} </code></pre> </div> <div class="post-taglist"> <a href="http://stackoverflow.com/questions/tagged/arrays" class="post-t ag" title="show questions tagged &#39;arrays&#39;" rel="tag">arrays</a> <a href= "http://stackoverflow.com/questions/tagged/algorithm" class="post-tag" title="sh ow questions tagged &#39;algorithm&#39;" rel="tag">algorithm</a> <a href="http:/ /stackoverflow.com/questions/tagged/subset-sum" class="post-tag" title="show que stions tagged &#39;subset-sum&#39;" rel="tag">subset-sum</a> <a href="http://sta ckoverflow.com/questions/tagged/codility" class="post-tag" title="show questions tagged &#39;codility&#39;" rel="tag">codility</a> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/q/16605991" title="shor

t permalink to this question" class="short-link" id="link-post-16605991">share</ a><span class="lsep">|</span><a href="http://stackoverflow.com/posts/16605991/ed it" class="suggest-edit-post" title="">improve this question</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/posts/16605991/rev isions" title="show all edits to this post">edited <span title="2013-05-24 05:20 :45Z" class="relativetime">May 24 at 5:20</span></a> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/774674/adi"><div class=""><i mg src="./arrays - Number of subarrays divisible by k - Stack Overflow_files/29b 2bb2b9e784b7fe19215329de18b43" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/774674/adi">adi</a><br> <span class="reputation-score" title="reputation score" dir="ltr">65 8</span><span title="3 silver badges"><span class="badge2"></span><span class="b adgecount">3</span></span><span title="22 bronze badges"><span class="badge3"></ span><span class="badgecount">22</span></span> </div> </div> </td> <td class="post-signature owner"> <div class="user-info "> <div class="user-action-time"> asked <span title="2013-05-17 09:43:09Z" cla ss="relativetime">May 17 at 9:43</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/2391137/tonimaister"><div cl ass=""><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_ files/4fde1c091d0b554379c278e9557d614c" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/2391137/tonimaister">tonimai ster</a><br> <span class="reputation-score" title="reputation score" dir="ltr">18 </span><span title="4 bronze badges"><span class="badge3"></span><span class="ba dgecount">4</span></span> </div> </div> </td> </tr> </tbody></table> </div> </td> </tr> <tr> <td class="votecell"></td>

<td> <div id="comments-16605991" class="comments dno"> <table> <tbody> <tr><td></td><td></td></tr> </tbody> </table> </div> </td> </tr> </div> </tbody></table>

<div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="subheader answers-subheader"> <h2> 4 Answers </h2> <div id="tabs"> <a href="http://stackoverflow.com/questions/16605991/number-of-s ubarrays-divisible-by-k?answertab=active#tab-top" title="Answers with the latest activity first">active</a> <a href="http://stackoverflow.com/questions/16605991/number-of-subarrays-divisib le-by-k?answertab=oldest#tab-top" title="Answers in the order they were provided ">oldest</a> <a class="youarehere" href="http://stackoverflow.com/questions/16605991/number-o f-subarrays-divisible-by-k?answertab=votes#tab-top" title="Answers with the high est score first">votes</a> </div> </div> </div>

<a name="16606634"></a> <div id="answer-16606634" class="answer accepted-answer" data-answerid="16606634 " style=""> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="16606634"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">3</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> <span class="vote-accepted-on load-accepted-answer-date" title="load

ing when this answer was accepted...">accepted</span> </div> </td>

<td class="answercell"> <div class="post-text"><p>As you are only interested in numbers divisible by K, you can do all computations modulo K. Consider the cumulative sum array S such that S[i] = S[0] + S[1] + ... + S[i]. T hen (P, Q) is a slice divisible by K iff S[P] = S[Q] (remember we do all computa tions modulo K). So you just have to count for each possible value of [0,...,K-1 ] how many times it appears in S.</p> <p>Here is some pseudocode:</p> <pre><code>B = new array(K) B[0]++ s = 0 for i = 0 to N-1 s = (s + A[i]) % K B[s]++ ans = 0 for i = 0 to K-1 ans += B[i]*(B[i]-1)/2 </code></pre> <p>Once you know that they are x cells in S that have value i, you want to count the number of slices the start in a cell with value i and ends in a cell with v alue i, this number is x(x-1)/2. To solve edge problems, we add one cell with va lue 0.</p> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/16606634" title="shor t permalink to this answer" class="short-link" id="link-post-16606634">share</a> <span class="lsep">|</span><a href="http://stackoverflow.com/posts/16606634/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/posts/16606634/rev isions" title="show all edits to this post">edited <span title="2013-05-17 14:09 :39Z" class="relativetime">May 17 at 14:09</span></a>

</div> <div class="user-gravatar32"> </div> <div class="user-details"> <br> </div> </div> </td>

<td align="right" class="post-signature">

<div class="user-info user-hover"> <div class="user-action-time"> answered <span title="2013-05-17 10:17:5 6Z" class="relativetime">May 17 at 10:17</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/1372399/thomash"><div class= ""><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_file s/8282947398571ff83ee0b3fa325ef49f" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/1372399/thomash">Thomash</a> <br> <span class="reputation-score" title="reputation score" dir="ltr">2, 366</span><span title="1 gold badge"><span class="badge1"></span><span class="ba dgecount">1</span></span><span title="6 silver badges"><span class="badge2"></sp an><span class="badgecount">6</span></span><span title="26 bronze badges"><span class="badge3"></span><span class="badgecount">26</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-16606634" class="comments"> <table> <tbody> <tr id="comment-23872517" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">sorry, I forgot to say that elements in array could be negatives</span> &nbsp;<a href="http://st ackoverflow.com/users/2391137/tonimaister" title="18 reputation" class="commentuser owner">tonimaister</a> <span class="comment-date" dir="ltr"><a class="comme

nt-link" href="http://stackoverflow.com/questions/16605991/number-of-subarrays-d ivisible-by-k/16606634#comment23872517_16606634"><span title="2013-05-17 10:32:3 7Z" class="relativetime-clean">May 17 at 10:32</span></a></span></div></td> </tr> <tr id="comment-23872558" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">It doesn't chan ge anything, my solution is still valid.</span> &nbsp;<a href="http://stackoverfl ow.com/users/1372399/thomash" title="2366 reputation" class="comment-user">Thoma sh</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="http:/ /stackoverflow.com/questions/16605991/number-of-subarrays-divisible-by-k/1660663 4#comment23872558_16606634"><span title="2013-05-17 10:34:04Z" class="relativeti me-clean">May 17 at 10:34</span></a></span></div></td> </tr> <tr id="comment-23874727" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Thank you very much for your answer, but I don't know if I don't get it of it just doesn't work . I prove with for example array {1, 2, 3} and k = 3 and the result is 1, when i t should be 3. Could you explain a little bit more your answer?</span> &nbsp;<a h ref="http://stackoverflow.com/users/2391137/tonimaister" title="18 reputation" c lass="comment-user owner">tonimaister</a> <span class="comment-date" dir="ltr">< a class="comment-link" href="http://stackoverflow.com/questions/16605991/numberof-subarrays-divisible-by-k/16606634#comment23874727_16606634"><span title="2013 -05-17 11:46:51Z" class="relativetime-clean">May 17 at 11:46</span></a></span><s pan class="edited-yes" title="this comment was edited"></span></div></td> </tr> <tr id="comment-23875080" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Indeed the last line <code>ans += B[i]*(B[i]+1)/2</code> counts only splices of size &gt; 1, I have modified it.</span> &nbsp;<a href="http://stackoverflow.com/users/1372399/th omash" title="2366 reputation" class="comment-user">Thomash</a> <span class="com ment-date" dir="ltr"><a class="comment-link" href="http://stackoverflow.com/ques tions/16605991/number-of-subarrays-divisible-by-k/16606634#comment23875080_16606 634"><span title="2013-05-17 11:56:45Z" class="relativetime-clean">May 17 at 11: 56</span></a></span></div></td> </tr> <tr id="comment-23879472" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">It doesn't work with <code>k = 6</code> and <code>A = {1, 2, 3}</code>. It returns 3, and it sh ould be one. Maybe it's a little mistake, but I can't fix it because I still don 't understand your reasoning. Why do you multiply <code>B[i] * (B[i]+1)</code> ? </span> &nbsp;<a href="http://stackoverflow.com/users/2391137/tonimaister" title= "18 reputation" class="comment-user owner">tonimaister</a> <span class="commentdate" dir="ltr"><a class="comment-link" href="http://stackoverflow.com/questions /16605991/number-of-subarrays-divisible-by-k/16606634#comment23879472_16606634"> <span title="2013-05-17 13:58:45Z" class="relativetime-clean">May 17 at 13:58</s pan></a></span></div></td> </tr> </tbody> </table> </div>

<a id="comments-link-16606634" class="comments-link" data-comments-count="2" title="expand to show all comments on this post">show <b>2</b> more comments</a > </td> </tr> </tbody></table> </div> <div class="everyonelovesstackoverflow adzerk-vote" id="adzerk3"><a href="http://engine.adzerk.net/r?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjI5MTEsImNoIjoxM Tc4LCJjciI6NTkyMywiZGkiOiJlMzQ4NDFiMGRmYzA0MmE1OGU0NzNiYTY0ZGQ5MTIzZSIsImRtIjoxL CJmYyI6MTAxODgsImZsIjo3NDcyLCJrdyI6ImFycmF5cyxhbGdvcml0aG0sc3Vic2V0LXN1bSxjb2Rpb Gl0eSIsIm53IjoyMiwicmYiOiJodHRwOi8vc3RhY2tvdmVyZmxvdy5jb20vdGFncy9jb2RpbGl0eS9ob 3QiLCJydiI6MCwicHIiOjE2MDQsInN0Ijo4Mjc3LCJ6biI6NDQsInVyIjoiaHR0cDovL2NhcmVlcnMuc 3RhY2tvdmVyZmxvdy5jb20vIn0&s=3Ypc7UitO0BQ6XeuLzjlxQPKOiM" rel="nofollow" target= "_blank" title=""><img src="./arrays - Number of subarrays divisible by k - Stac k Overflow_files/46a9844d6e504212a85bc72ddd7dd829.png" title="" alt="" border="0 " width="728" height="90"></a><div class="adzerk-vote-controls" style="display:n one;"><div class="adzerk-vote-option adzerk-vote-up"><div class="adzerk-vote-ico n"></div></div><div class="adzerk-vote-option adzerk-vote-down"><div class="adze rk-vote-icon"></div></div></div><div class="adzerk-vote-survey" style="display:n one;"><form><span>No problem. We won't show you that ad again. Why didn't you li ke it?</span><ul><li><label><input type="radio" value="12" name="downvoteReason" >Uninteresting</label></li><li><label><input type="radio" value="13" name="downv oteReason">Misleading</label></li><li><label><input type="radio" value="14" name ="downvoteReason">Offensive</label></li><li><label><input type="radio" value="15 " name="downvoteReason">Repetitive</label></li></ul><a href="http://stackoverflo w.com/questions/16605991/number-of-subarrays-divisible-by-k/16606634#" class="ad zerk-vote-cancel">Oops! I didn't mean to do this.</a></form></div><img height="0 px" width="0px" border="0" src="./arrays - Number of subarrays divisible by k Stack Overflow_files/i(1).gif"></div>

<a name="17307808"></a> <div id="answer-17307808" class="answer" data-answerid="17307808"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="17307808"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">1</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td>

<td class="answercell"> <div class="post-text"><pre><code>static void Main(string[] args) { int[] A = new int[] { 4, 5, 0, -2, -3, 1 }; int sum = 0; int i, j;

int count = 0; for (i = 0; i &lt; A.Length; i++) { for (j = 0; j &lt; A.Length; j++) { if (j + i &lt; 6) sum += A[j + i]; if ((sum % 5) == 0) count++; } sum = 0; } Console.WriteLine(count); Console.ReadLine(); } </code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/17307808" title="shor t permalink to this answer" class="short-link" id="link-post-17307808">share</a> <span class="lsep">|</span><a href="http://stackoverflow.com/posts/17307808/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td>

<td align="right" class="post-signature">

<div class="user-info "> <div class="user-action-time"> answered <span title="2013-06-25 21:24:5 7Z" class="relativetime">Jun 25 at 21:24</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/2521798/user2521798"><div cl ass=""><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_ files/3ee467a43002dc846b8db4c43d5a5058" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/2521798/user2521798">user252 1798</a><br> <span class="reputation-score" title="reputation score" dir="ltr">11

</span><span title="1 bronze badge"><span class="badge3"></span><span class="bad gecount">1</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-17307808" class="comments"> <table> <tbody> <tr id="comment-25101334" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">Suggestion: you 're iterating through the whole array in the inner loop, when you really only ha ve to go through part of it--since <code>j + i</code> is going to reach 6 partwa y through. Also, you're using <code>sum</code> in a comparison right after that even though it's not been set in that iteration. This solution needs some work s till.</span> &nbsp;<a href="http://stackoverflow.com/users/12802/catfood" title=" 2198 reputation" class="comment-user">catfood</a> <span class="comment-date" dir ="ltr"><a class="comment-link" href="http://stackoverflow.com/questions/16605991 /number-of-subarrays-divisible-by-k/16606634#comment25101334_17307808"><span tit le="2013-06-25 21:46:19Z" class="relativetime-clean">Jun 25 at 21:46</span></a>< /span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="16881714"></a> <div id="answer-16881714" class="answer" data-answerid="16881714"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="16881714"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">0</span> <a class="vote-down-off" title="This answer is not useful">down vote</a>

</div> </td>

<td class="answercell"> <div class="post-text"> <pre><code> private int GetSubArraysCount(int[] A, int K) { int N = A.Length; int[] B = new int[K]; for (int i = 0; i &lt; B.Length; i++) { B[i] = 0; } B[0]++; int s = 0; for (int i = 0; i &lt; N; i++) { s = (s + A[i]) % K; while (s &lt; 0) { s += K; } B[s]++; } int ans = 0; for (int i = 0; i &lt;= K - 1; i++) { ans += B[i] * (B[i] - 1) / 2; } return ans; } </code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/16881714" title="shor t permalink to this answer" class="short-link" id="link-post-16881714">share</a> <span class="lsep">|</span><a href="http://stackoverflow.com/posts/16881714/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time">

<a href="http://stackoverflow.com/posts/16881714/rev isions" title="show all edits to this post">edited <span title="2013-06-02 11:18 :06Z" class="relativetime">Jun 2 at 11:18</span></a> </div> <div class="user-gravatar32"> </div> <div class="user-details"> <br> </div> </div> </td>

<td align="right" class="post-signature">

<div class="user-info "> <div class="user-action-time"> answered <span title="2013-06-02 10:01:3 0Z" class="relativetime">Jun 2 at 10:01</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/2444873/user2444873"><div cl ass=""><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_ files/fed0953adf5d8308b5d705a4d31e0d67" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/2444873/user2444873">user244 4873</a><br> <span class="reputation-score" title="reputation score" dir="ltr">1< /span><span title="1 bronze badge"><span class="badge3"></span><span class="badg ecount">1</span></span> </div> </div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-16881714" class="comments dno"> <table> <tbody> <tr><td></td><td></td></tr> </tbody>

</table> </div> </td> </tr> </tbody></table> </div> <a name="16615228"></a> <div id="answer-16615228" class="answer" data-answerid="16615228"> <table> <tbody><tr> <td class="votecell"> <div class="vote"> <input type="hidden" value="16615228"> <a class="vote-up-off" title="This answer is useful">up vote</a> <span class="vote-count-post ">-1</span> <a class="vote-down-off" title="This answer is not useful">down vote</a> </div> </td>

<td class="answercell"> <div class="post-text"><pre class="lang-java prettyprint prettyprinted" styl e=""><code><span class="kwd">public</span><span class="pln"> </span><span class= "kwd">class</span><span class="pln"> </span><span class="typ">SubArrayDivisible< /span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class= "kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span ><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</s pan><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">[]</span><span clas s="pln"> A </span><span class="pun">=</span><span class="pln"> </span><span clas s="pun">{</span><span class="lit">4</span><span class="pun">,</span><span class= "pln"> </span><span class="lit">5</span><span class="pun">,</span><span class="p ln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln "> </span><span class="pun">-</span><span class="lit">2</span><span class="pun"> ,</span><span class="pln"> </span><span class="pun">-</span><span class="lit">3< /span><span class="pun">,</span><span class="pln"> </span><span class="lit">1</s pan><span class="pun">};</span><span class="pln"> </span><span class="typ">SubArrayDivisible</span><span class="pln"> obj </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new </span><span class="pln"> </span><span class="typ">SubArrayDivisible</span><span class="pun">();</span><span class="pln"> obj</span><span class="pun">.</span><span class="pln">getSubArrays</span ><span class="pun">(</span><span class="pln">A</span><span class="pun">,</span>< span class="lit">5</span><span class="pun">);</span><span class="pln"> </span><span class="pun">}</span><span class="pln">

</span><span class="kwd">private</span><span class="pln"> </span><span class ="kwd">void</span><span class="pln"> getSubArrays</span><span class="pun">(</spa n><span class="kwd">int</span><span class="pun">[]</span><span class="pln"> A</s pan><span class="pun">,</span><span class="kwd">int</span><span class="pln"> K</ span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> count </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span c lass="pun">,</span><span class="pln">s</span><span class="pun">=</span><span cla ss="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span class ="kwd">int</span><span class="pln"> i</span><span class="pun">=</span><span clas s="lit">0</span><span class="pun">;</span><span class="pln">i</span><span class= "pun">&lt;</span><span class="pln">A</span><span class="pun">.</span><span class ="pln">length</span><span class="pun">;</span><span class="pln">i</span><span cl ass="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> </span><span c lass="lit">0</span><span class="pun">;</span><span class="pln"> </span><span class="kwd">for</span><span class="pun">(</span><span c lass="kwd">int</span><span class="pln"> j </span><span class="pun">=</span><span class="pln"> i</span><span class="pun">;</span><span class="pln">j</span><span class="pun">&lt;</span><span class="pln">A</span><span class="pun">.</span><span class="pln">length</span><span class="pun">;</span><span class="pln">j</span><s pan class="pun">++)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> s </span><span class="pun">=</span><span class="pln"> s</span><s pan class="pun">+</span><span class="pln">A</span><span class="pun">[</span><spa n class="pln">j</span><span class="pun">];</span><span class="pln"> </span><span class="kwd">if</span><span class="pun">((</span><sp an class="pln">s</span><span class="pun">%</span><span class="pln">K</span><span class="pun">)</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span cl ass="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</s pan><span class="pln">out</span><span class="pun">.</span><span class="pln">prin tln</span><span class="pun">(</span><span class="str">"Value of S "</span><span class="pun">+</span><span class="pln">s</span><span class="pun">);</span><span c lass="pln"> count</span><span class="pun">++;</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span cl ass="pln">out</span><span class="pun">.</span><span class="pln">println</span><s pan class="pun">(</span><span class="str">"Num of Sub-Array "</span><span class= "pun">+</span><span class="pln">count</span><span class="pun">);</span><span cla ss="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="pun">}</span></code></pre> </div> <table class="fw"> <tbody><tr> <td class="vt">

<div class="post-menu"><a href="http://stackoverflow.com/a/16615228" title="shor t permalink to this answer" class="short-link" id="link-post-16615228">share</a> <span class="lsep">|</span><a href="http://stackoverflow.com/posts/16615228/edit " class="suggest-edit-post" title="">improve this answer</a></div> </td> <td align="right" class="post-signature"> <div class="user-info "> <div class="user-action-time"> <a href="http://stackoverflow.com/posts/16615228/rev isions" title="show all edits to this post">edited <span title="2013-06-02 10:25 :08Z" class="relativetime">Jun 2 at 10:25</span></a> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/1410037/thomas"><div class=" "><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_files /c4e910f23fa3f49cded79db125234a3e" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/1410037/thomas">Thomas</a><b r> <span class="reputation-score" title="reputation score" dir="ltr">69 6</span><span title="5 silver badges"><span class="badge2"></span><span class="b adgecount">5</span></span><span title="22 bronze badges"><span class="badge3"></ span><span class="badgecount">22</span></span> </div> </div> </td>

<td align="right" class="post-signature">

<div class="user-info "> <div class="user-action-time"> answered <span title="2013-05-17 17:59:2 6Z" class="relativetime">May 17 at 17:59</span> </div> <div class="user-gravatar32"> <a href="http://stackoverflow.com/users/2395001/subhasish"><div clas s=""><img src="./arrays - Number of subarrays divisible by k - Stack Overflow_fi les/5415b8e55a562b3cc508124dd9b777ee" alt="" width="32" height="32"></div></a> </div> <div class="user-details"> <a href="http://stackoverflow.com/users/2395001/subhasish">Subhasish </a><br> <span class="reputation-score" title="reputation score" dir="ltr">1< /span> </div>

</div> </td> </tr> </tbody></table> </td> </tr>

<tr> <td class="votecell"></td> <td> <div id="comments-16615228" class="comments"> <table> <tbody> <tr id="comment-23889249" class="comment"> <td></td> <td class="comment-text"><div><span class="comment-copy">That's the solu tion I gave in the interview, but its complexity is O(n!) and they asked for O(m ax(k,n)). Thomas' solution is the correct one</span> &nbsp;<a href="http://stacko verflow.com/users/2391137/tonimaister" title="18 reputation" class="comment-user owner">tonimaister</a> <span class="comment-date" dir="ltr"><a class="comment-l ink" href="http://stackoverflow.com/questions/16605991/number-of-subarrays-divis ible-by-k/16606634#comment23889249_16615228"><span title="2013-05-17 19:06:34Z" class="relativetime-clean">May 17 at 19:06</span></a></span></div></td> </tr> </tbody> </table> </div> </td> </tr> </tbody></table> </div> <a name="new-answer"></a> <form id="post-form" action="http://stackoverflow.com/questions/16605991/ans wer/submit" method="post" class="post-form"> <input type="hidden" id="post-id" value="16605991"> <h2 class="space">Your Answer</h2>

<script type="text/javascript"> StackExchange.ready(function() { initTagRenderer("".split(" "), "".split(" ")); prepareEditor({ heartbeatType: 'answer', bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true });

}); </script> <div id="post-editor" class="post-editor"> <div style="position: relative;"> <div class="wmd-container"> <div id="wmd-button-bar" class="wmd-button-bar"><ul id="wmd-button-r ow" class="wmd-button-row"><li id="wmd-bold-button" class="wmd-button" style="le ft: 0px;"><span style="background-position: 0px -20px;"></span></li><li id="wmditalic-button" class="wmd-button" style="left: 25px;"><span style="background-po sition: -20px -20px;"></span></li><li id="wmd-spacer1" class="wmd-spacer" style= "left: 50px;"><span style="background-position: -40px -20px;"></span></li><li id ="wmd-link-button" class="wmd-button" style="left: 75px;"><span style="backgroun d-position: -40px -20px;"></span></li><li id="wmd-quote-button" class="wmd-butto n" style="left: 100px;"><span style="background-position: -60px -20px;"></span>< /li><li id="wmd-code-button" class="wmd-button" style="left: 125px;"><span style ="background-position: -80px -20px;"></span></li><li id="wmd-image-button" class ="wmd-button" style="left: 150px;"><span style="background-position: -100px -20p x;"></span></li><li id="wmd-spacer2" class="wmd-spacer" style="left: 175px;"><sp an style="background-position: -120px -20px;"></span></li><li id="wmd-olist-butt on" class="wmd-button" style="left: 200px;"><span style="background-position: -1 20px -20px;"></span></li><li id="wmd-ulist-button" class="wmd-button" style="lef t: 225px;"><span style="background-position: -140px -20px;"></span></li><li id=" wmd-heading-button" class="wmd-button" style="left: 250px;"><span style="backgro und-position: -160px -20px;"></span></li><li id="wmd-hr-button" class="wmd-butto n" style="left: 275px;"><span style="background-position: -180px -20px;"></span> </li><li id="wmd-spacer3" class="wmd-spacer" style="left: 300px;"><span style="b ackground-position: -200px -20px;"></span></li><li id="wmd-undo-button" class="w md-button" style="left: 325px;"><span style="background-position: -200px -20px;" ></span></li><li id="wmd-redo-button" class="wmd-button" style="left: 350px;"><s pan style="background-position: -220px -20px;"></span></li></ul></div> <textarea id="wmd-input" class="wmd-input" name="post-text" cols="92 " rows="15" tabindex="101" data-min-length=""></textarea> </div> </div> <div class="fl" style="margin-top: 8px; height:24px;">&nbsp;</div> <div id="draft-saved" class="draft-saved community-option fl" style="margintop: 8px; height:24px; display:none;">draft saved</div> <div id="draft-discarded" class="draft-discarded community-option fl" style= "margin-top: 8px; height:24px; display:none;">draft discarded</div>

<div id="wmd-preview" class="wmd-preview"></div> <div></div> <div class="edit-block"> <input id="fkey" name="fkey" type="hidden" value="9336b12e86af343415687d 9e813eec5f"> <input id="author" name="author" type="text"> </div> </div> <div style="position: relative;"> <div class="form-item new-post-login"> <div class="new-login-form">

<div class="new-login-left"> <h3>Sign up or <a id="login-link" href="http://stackover flow.com/users/login?returnurl=%2fquestions%2f16605991%2fnumber-of-subarrays-div isible-by-k%23new-answer">log in</a></h3> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.helpers.onClickDraftSave('#login-l ink'); }); </script> <div class="preferred-login google-login"> <p><span class="icon"></span><span>Sign up using Goo gle</span></p> </div> <div class="preferred-login facebook-login"> <p><span class="icon"></span><span>Sign up using Fac ebook</span></p> </div> <div class="preferred-login stackexchange-login"> <p><span class="icon"></span><span>Sign up using Sta ck Exchange</span></p> </div> </div> <input type="hidden" name="manual-openid" class="manual-open id"> <input type="hidden" name="use-facebook" class="use-facebook " value="false"> <input type="button" class="submit-openid" value="Submit" st yle="display:none"> <div class="new-login-right"> <h3>Post as a guest</h3> <div class="form-item"> <table> <tbody><tr> <script type="text/javascript"> StackExchange.ready(function () { StackExchange.helpers.bindHelpOverlayEvents($('.vm input')); }); </script> <td class="vm"> <div> <label for="display-name">Name</label> <input id="display-name" name="display-name" type="text" siz e="30" maxlength="30" value="" tabindex="105"> </div> <div> <label for="m-address">Email</label> <input id="m-address" name="m-address" type="text" size="30" maxlength="100" value="" tabindex="106"> <span class="edit-field-overlay">required, but not shown</sp an> </div> </td> </tr> </tbody></table> </div> </div> </div> </div>

<script type="text/javascript"> StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', '% 2fquestions%2f16605991%2fnumber-of-subarrays-divisible-by-k%23new-answer'); } ); </script> <noscript> &lt;h3&gt;Post as a guest&lt;/h3&gt; &lt;div class="form-item"&gt; &lt;table&gt; &lt;tr&gt; &lt;script type="text/javascript"&gt; StackExchange.ready(function () { StackExchange.helpers.bindHelpOverlayEvents($('.vm input')); }); &lt;/script&gt; &lt;td class="vm"&gt; &lt;div&gt; &lt;label for="display-name"&gt;Name&lt;/label&gt; &lt;input id="display-name" name="display-name" type="text" size="30" maxlength="30" value="" tabindex="105"&gt; &lt;/div&gt; &lt;div&gt; &lt;label for="m-address"&gt;Email&lt;/label&gt; &lt;input id="m-address" name="m-address" type="text" size=" 30" maxlength="100" value="" tabindex="106"&gt; &lt;span class="edit-field-overlay"&gt;required, but not sho wn&lt;/span&gt; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; </noscript> </div> <div class="form-submit cbt"> <input id="submit-button" type="submit" value="Post Your Answer" tab index="110"> <a href="http://stackoverflow.com/questions/16605991/number-of-subar rays-divisible-by-k/16606634#" class="discard-answer dno">discard</a> <p class="privacy-policy-agreement"> By posting your answer, you agree to the <a href="http://stackexchange.com/legal/privacy-policy" target="_blank"> privacy policy</a> and <a href="http://stackexchange.com/legal/terms-of-service" target="_blank ">terms of service</a>. </p> <input type="hidden" name="legalLinksShown" value="1"> </div> </form>

<h2 class="bottom-notice"> Not the answer you're looking for? Browse other questions tagged <a href="http://stackoverflow.com/ques

tions/tagged/arrays" class="post-tag" title="show questions tagged &#39;arrays&# 39;" rel="tag">arrays</a> <a href="http://stackoverflow.com/questions/tagged/alg orithm" class="post-tag" title="show questions tagged &#39;algorithm&#39;" rel=" tag">algorithm</a> <a href="http://stackoverflow.com/questions/tagged/subset-sum " class="post-tag" title="show questions tagged &#39;subset-sum&#39;" rel="tag"> subset-sum</a> <a href="http://stackoverflow.com/questions/tagged/codility" clas s="post-tag" title="show questions tagged &#39;codility&#39;" rel="tag">codility </a> or <a href="http://stackoverflow.com/questions/ask">ask your own question</a>. </h2> </div> </div> <div id="sidebar" class="show-votes"> <div class="module question-stats"> <p class="label-key">tagged</p> <div class="tagged"><a href="http://stackoverflow.com/questions/tagged/a rrays" class="post-tag" title="show questions tagged &#39;arrays&#39;" rel="tag" >arrays</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x"></ span>&nbsp;<span class="item-multiplier-count">55477</span></span><br> <a href="http://stackoverflow.com/questions/tagged/algorithm" class="post-tag" t itle="show questions tagged &#39;algorithm&#39;" rel="tag">algorithm</a>&nbsp;<s pan class="item-multiplier"><span class="item-multiplier-x"></span>&nbsp;<span cl ass="item-multiplier-count">30772</span></span><br> <a href="http://stackoverflow.com/questions/tagged/subset-sum" class="post-tag" title="show questions tagged &#39;subset-sum&#39;" rel="tag">subset-sum</a>&nbsp ;<span class="item-multiplier"><span class="item-multiplier-x"></span>&nbsp;<span class="item-multiplier-count">75</span></span><br> <a href="http://stackoverflow.com/questions/tagged/codility" class="post-tag" ti tle="show questions tagged &#39;codility&#39;" rel="tag">codility</a>&nbsp;<span class="item-multiplier"><span class="item-multiplier-x"></span>&nbsp;<span class ="item-multiplier-count">21</span></span><br> </div> <table id="qinfo"> <tbody><tr> <td><p class="label-key">asked</p></td> <td style="padding-left:10px"><p class="label-key" title="2013-0 5-17 09:43:09Z"><b>1 month ago</b></p></td> </tr> <tr> <td><p class="label-key">viewed</p></td> <td style="padding-left:10px"> <p class="label-key"> <b>694 times</b> </p> </td> </tr> <tr> <td><p class="label-key">active</p></td> <td style="padding-left:10px"><p class="label-key"><b><a href="h ttp://stackoverflow.com/questions/16605991/number-of-subarrays-divisible-by-k/16 606634?lastactivity" class="lastactivity-link" title="2013-06-25 21:24:57Z">6 da ys ago</a></b></p></td> </tr> </tbody></table> </div> <div class="module community-bulletin" data-tracker="cb=1"> <h4>Community Bulletin</h4>

<div class="related"> <div class="spacer"> <div class="bulletin-item-type"><a href="http://blog.stackoverflow.c om/?cb=1" class="event-date" target="_blank">blog</a></div> <div class="bulletin-item-content"> <a href="http://blog.stackoverflow.com/2013/07/say-hi-to-nine-of -our-newest-newbies/?cb=1" class="question-hyperlink" target="_blank">Say Hi to Nine of Our Newest Newbies</a> </div> <br class="cbt"> </div> </div> </div> <script type="text/javascript"> var scriptSrc = "http://engine.adzerk.net/z/8277/adzerk1_2_4 _43,adzerk2_2_17_45,adzerk3_2_4_44?keywords=arrays,algorithm,subset-sum,codility "; if (document.referrer) { if (/\?/.test(scriptSrc)) scriptSrc += "&"; else scriptSrc += "?"; scriptSrc += "xReferrer=" + document.referrer; } StackExchange.ready(function() { var z = document.createElement("script"); z.type = "text/javascript"; z.async = "true"; z.src = scriptSrc; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s); }); </script> <div id="hireme"> <a href="http://careers.stackoverflow.com/jobs?a=24" class ="top" target="_blank"></a> <ul class="jobs"> <li> <a href="http://careers.stack overflow.com/jobs/34545/worship-worthy-frontend-developer-wanted-idyllic-softwar e?a=EOwfwnC" target="_blank" title="Worship-worthy Frontend Developer Wanted! Ja vascript // Html // CSS at Idyllic Software. Click to learn more."> Worship-wort hy Frontend Developer Wanted! Javascript //<br> <span class="company">Idyllic Sof tware</span> <span class="location">Pune, India</span> </a> </li> <li> <a href=" http://careers.stackoverflow.com/jobs/33649/senior-backend-developer-badgeville? a=DKRNbaw" target="_blank" title="Senior Backend Developer at Badgeville. Click to learn more."> Senior Backend Developer<br> <span class="company">Badgeville</ span> <span class="location">Redwood City, CA</span> </a> </li> <li> <a href="ht tp://careers.stackoverflow.com/jobs/35816/senior-software-developer-building-sca lable-e-lenskart-com?a=GjCFFzG" target="_blank" title="Senior Software Developer - building scalable e-solutions at lenskart.com. Click to learn more."> Senior Software Developer - building scalable e-solutions<br> <span class="company">len skart.com</span> <span class="location">New Delhi, India / relocation</span> </a > </li> <li> <a href="http://careers.stackoverflow.com/jobs/32969/ruby-developer -idyllic-software?a=CX4Dz3i" target="_blank" title="Ruby Developer at Idyllic So ftware. Click to learn more."> Ruby Developer<br> <span class="company">Idyllic Software</span> <span class="location">Mumbai, India</span> </a> </li> <li> <a h ref="http://careers.stackoverflow.com/jobs/36059/web-developer-at-pointroll-gann ett-co?a=GBp9GMg" target="_blank" title="Web Developer at PointRoll at Gannett C o. Click to learn more."> Web Developer at PointRoll<br> <span class="company">G annett Co</span> <span class="location">Conshohocken, PA</span> </a> </li> <li> <a href="http://careers.stackoverflow.com/jobs/36379/expert-php-developer-idylli c-software?a=GYRE1Pi" target="_blank" title="Expert PHP Developer at Idyllic Sof

tware. Click to learn more."> Expert PHP Developer<br> <span class="company">Idy llic Software</span> <span class="location">Mumbai, India</span> </a> </li> </ul > <img alt="" class="impression" src="./arrays - Number of subarrays divisible b y k - Stack Overflow_files/EOwfwnC-DKRNbaw-GjCFFzG-CX4Dz3i-GBp9GMg-GYRE1Pi-24" s tyle="display:none"></div>

<div class="module sidebar-related"> <h4 id="h-related">Related</h4> <div class="related" data-tracker="rq=1"> <div class="spacer"> <a href="http://stackoverflow.com/q/2680548?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">65</div> </a> <a href="http://stackoverflow.com/questions/2680548/given-an-array-of-numbers-re turn-array-of-products-of-all-other-numbers-no-div?rq=1" class="question-hyperli nk">Given an array of numbers, return array of products of all other numbers (no division)</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/3492302?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted large">296</div> </a> <a href="http://stackoverflow.com/questions/3492302/easy-interview-question-gotharder-given-numbers-1-100-find-the-missing-numbe?rq=1" class="question-hyperlin k">Easy interview question got harder: given numbers 1..100, find the missing nu mber(s)</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/5963983?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">15</div> </a> <a href="http://stackoverflow.com/questions/5963983/find-the-minimum-number-of-e lements-required-so-that-their-sum-equals-or-exceeds?rq=1" class="question-hyper link">Find the minimum number of elements required so that their sum equals or e xceeds S</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/8099334?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes answered-accepted default">4</div> </a> <a href="http://stackoverflow.com/questions/8099334/maximum-subset-sum-with-twoarrays?rq=1" class="question-hyperlink">Maximum subset sum with two arrays</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/9152095?rq=1" title="Vote score (upvotes - d ownvotes)"> <div class="answer-votes default">2</div> </a> <a href="http://stackoverflow.com/questions/9152095/zero-sum-minimal-subarray?rq =1" class="question-hyperlink">Zero sum minimal subarray</a> </div> <div class="spacer">

<a href="http://stackoverflow.com/q/11772145?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">0</div> </a> <a href="http://stackoverflow.com/questions/11772145/number-of-subarrays-where-s um-of-numbers-is-divisible-by-k?rq=1" class="question-hyperlink">number of subar rays where sum of numbers is divisible by K</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/13089010?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes answered-accepted default">1</div> </a> <a href="http://stackoverflow.com/questions/13089010/find-longest-subarray-whose -sum-divisible-by-k?rq=1" class="question-hyperlink">Find longest subarray whose sum divisible by K</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/13093602?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes answered-accepted default">0</div> </a> <a href="http://stackoverflow.com/questions/13093602/finding-subarray-with-maxim um-sum-number-of-elements?rq=1" class="question-hyperlink">Finding subarray with maximum sum/number of elements</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/13950442?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes answered-accepted default">0</div> </a> <a href="http://stackoverflow.com/questions/13950442/subset-sum-recursively-to-g et-as-near-as-possible-to-a-given-number?rq=1" class="question-hyperlink">Subset sum recursively to get as near as possible to a given number</a> </div> <div class="spacer"> <a href="http://stackoverflow.com/q/14120729?rq=1" title="Vote score (upvotes downvotes)"> <div class="answer-votes default">13</div> </a> <a href="http://stackoverflow.com/questions/14120729/algorithm-to-split-an-array -into-p-subarrays-of-balanced-sum?rq=1" class="question-hyperlink">Algorithm to split an array into P subarrays of balanced sum</a> </div> </div> </div> </div> <div id="feed-link"> <div id="feed-link-text"><a href="http://stackoverflow.com/feeds/question/16 605991" title="feed of this question and its answers"><span class="feed-icon"></ span>question feed</a></div> </div> <script type="text/javascript"> StackExchange.ready(function(){$.get('/posts/16605991/ivc/7ea7');}); </script> <noscript> &lt;div&gt;&lt;img src="/posts/16605991/ivc/7ea7" class="dno" alt="" width=" 0" height="0"&gt;&lt;/div&gt;

</noscript><div style="display:none" id="prettify-lang"></div></div> </div> </div> <div id="footer" class="categories"> <div class="footerwrap"> <div id="footer-menu"> <div class="top-footer-links"> <a href="http://stackoverflow.com/about">about</a> <a href="http://stackoverflow.com/help">help</a> <a href="http://stackoverflow.com/help/badges">badges</a > <a href="http://blog.stackexchange.com/?blb=1">blog</a> <a href="http://chat.stackoverflow.com/">chat</a> <a href="http://data.stackexchange.com/">data</a> <a href="http://stackexchange.com/legal">legal</a> <a href="http://stackexchange.com/legal/privacy-policy">priv acy policy</a> <a href="http://stackexchange.com/about/hiring">jobs</a> <a href="http://engine.adzerk.net/redirect/0/2776/2751/0/4de 3c60f719c4dfcb1a57531c7050090/0">advertising info</a> <a onclick="StackExchange.switchMobile(&quot;on&quot;, &quot ;/questions/16605991/number-of-subarrays-divisible-by-k/16606634&quot;)">mobile< /a> <b><a href="http://stackoverflow.com/contact">contact us</a> </b> <b><a href="http://meta.stackoverflow.com/">feedback</a> </b> </div> <div id="footer-sites"> <table> <tbody><tr> <th colspan="3"> Technology </th> <th> Life / Arts </th> <th> Culture / Recreation </th> <th> Science </th> <th> Other </th> </tr> <tr> <td> <ol> <li><a href="http://stackoverflow.com/" title="professio nal and enthusiast programmers">Stack Overflow</a></li> <li><a href="http://serverfault.com/" title="professiona l system and network administrators">Server Fault</a></li> <li><a href="http://superuser.com/" title="computer enth usiasts and power users">Super User</a></li> <li><a href="http://webapps.stackexchange.com/" title="p

ower users of web applications">Web Applications</a></li> <li><a href="http://askubuntu.com/" title="Ubuntu users and developers">Ask Ubuntu</a></li> <li><a href="http://webmasters.stackexchange.com/" title ="pro webmasters">Webmasters</a></li> <li><a href="http://gamedev.stackexchange.com/" title="p rofessional and independent game developers">Game Development</a></li> <li><a href="http://tex.stackexchange.com/" title="users of TeX, LaTeX, ConTeXt, and related typesetting systems">TeX - LaTeX</a></li> </ol></td><td><ol> <li><a href="http://programmers.stackexchange.com/" titl e="professional programmers interested in conceptual questions about software de velopment">Programmers</a></li> <li><a href="http://unix.stackexchange.com/" title="user s of Linux, FreeBSD and other Un*x-like operating systems.">Unix &amp; Linux</a> </li> <li><a href="http://apple.stackexchange.com/" title="pow er users of Apple hardware and software">Ask Different (Apple)</a></li> <li><a href="http://wordpress.stackexchange.com/" title= "WordPress developers and administrators">WordPress Answers</a></li> <li><a href="http://gis.stackexchange.com/" title="carto graphers, geographers and GIS professionals">Geographic Information Systems</a>< /li> <li><a href="http://electronics.stackexchange.com/" titl e="electronics and electrical engineering professionals, students, and enthusias ts">Electrical Engineering</a></li> <li><a href="http://android.stackexchange.com/" title="e nthusiasts and power users of the Android operating system">Android Enthusiasts< /a></li> <li><a href="http://security.stackexchange.com/" title=" IT security professionals">IT Security</a></li> </ol></td><td><ol> <li><a href="http://dba.stackexchange.com/" title="datab ase professionals who wish to improve their database skills and learn from other s in the community">Database Administrators</a></li> <li><a href="http://drupal.stackexchange.com/" title="Dr upal developers and administrators">Drupal Answers</a></li> <li><a href="http://sharepoint.stackexchange.com/" title ="SharePoint enthusiasts">SharePoint</a></li> <li><a href="http://ux.stackexchange.com/" title="user e xperience researchers and experts">User Experience</a></li> <li><a href="http://mathematica.stackexchange.com/" titl e="users of Mathematica">Mathematica</a></li> <li> <a href="http://stackexchange.com/sites#technology" class="more"> more (13) </a> </li> </ol> </td> <td> <ol> <li><a href="http://photo.stackexchange.com/" title="pro fessional, enthusiast and amateur photographers">Photography</a></li> <li><a href="http://scifi.stackexchange.com/" title="sci ence fiction and fantasy enthusiasts">Science Fiction &amp; Fantasy</a></li> <li><a href="http://cooking.stackexchange.com/" title="p rofessional and amateur chefs">Seasoned Advice (cooking)</a></li>

<li><a href="http://diy.stackexchange.com/" title="contr actors and serious DIYers">Home Improvement</a></li> <li> <a href="http://stackexchange.com/sites#lifearts" cl ass="more"> more (13) </a> </li> </ol> </td> <td> <ol> <li><a href="http://english.stackexchange.com/" title="l inguists, etymologists, and serious English language enthusiasts">English Langua ge &amp; Usage</a></li> <li><a href="http://skeptics.stackexchange.com/" title=" scientific skepticism">Skeptics</a></li> <li><a href="http://judaism.stackexchange.com/" title="t hose who base their lives on Jewish law and tradition and anyone interested in l earning more">Mi Yodeya (Judaism)</a></li> <li><a href="http://travel.stackexchange.com/" title="ro ad warriors and seasoned travelers">Travel</a></li> <li><a href="http://christianity.stackexchange.com/" tit le="committed Christians, experts in Christianity and those interested in learni ng more">Christianity</a></li> <li><a href="http://gaming.stackexchange.com/" title="pa ssionate videogamers on all platforms">Arqade (gaming)</a></li> <li><a href="http://bicycles.stackexchange.com/" title=" people who build and repair bicycles, people who train cycling, or commute on bi cycles">Bicycles</a></li> <li><a href="http://rpg.stackexchange.com/" title="gamem asters and players of tabletop, paper-and-pencil role-playing games">Role-playin g Games</a></li> <li> <a href="http://stackexchange.com/sites#culturerecre ation" class="more"> more (21) </a> </li> </ol> </td> <td> <ol> <li><a href="http://math.stackexchange.com/" title="peop le studying math at any level and professionals in related fields">Mathematics</ a></li> <li><a href="http://stats.stackexchange.com/" title="sta tisticians, data analysts, data miners and data visualization experts">Cross Val idated (stats)</a></li> <li><a href="http://cstheory.stackexchange.com/" title=" theoretical computer scientists and researchers in related fields">Theoretical C omputer Science</a></li> <li><a href="http://physics.stackexchange.com/" title="a ctive researchers, academics and students of physics">Physics</a></li> <li><a href="http://mathoverflow.net/" title="mathematic ians">MathOverflow</a></li> <li>

<a href="http://stackexchange.com/sites#science" cla ss="more"> more (7) </a> </li> </ol> </td> <td> <ol> <li><a href="http://stackapps.com/" title="apps, scripts , and development with the Stack Exchange API">Stack Apps</a></li> <li><a href="http://meta.stackoverflow.com/" title="meta -discussion of the Stack Exchange family of Q&amp;A websites">Meta Stack Overflo w</a></li> <li><a href="http://area51.stackexchange.com/" title="pr oposing new sites in the Stack Exchange network">Area 51</a></li> <li><a href="http://careers.stackoverflow.com/">Stack Ov erflow Careers</a></li> </ol> </td> </tr> </tbody></table> </div> </div> <div id="copyright"> site design / logo 2013 stack exchange inc; user contributions licensed under <a href="http://creativecommon s.org/licenses/by-sa/3.0/" rel="license">cc-wiki</a> with <a href="http://blog.stackoverflow.com/2009/06/attributionrequired/" rel="license">attribution required</a> </div> <div id="footer-flair"> <a href="http://creativecommons.org/licenses/by-sa/3.0/" class=" cc-wiki-link"></a> </div> <div id="svnrev"> rev 2013.7.1.798 </div> </div> </div> <noscript> &lt;div id="noscript-warning"&gt;Stack Overflow works best with JavaScri pt enabled&lt;img src="http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif" al t="" class="dno"&gt;&lt;/div&gt; </noscript> <script type="text/javascript">var _gaq=_gaq||[];_gaq.push(['_setAccount','U A-5620270-1']); _gaq.push(['_setCustomVar', 1, 'tags', '|arrays|algorithm|subset-sum|cod ility|']); _gaq.push(['_trackPageview']); var _qevents = _qevents || []; (function(){ var s=document.getElementsByTagName('script')[0]; var ga=document.createElement('script'); ga.type='text/javascript'; ga.async=true; ga.src='http://www.google-analytics.com/ga.js';

s.parentNode.insertBefore(ga,s); var sc=document.createElement('script'); sc.type='text/javascript'; sc.async=true; sc.src='http://edge.quantserve.com/quant.js'; s.parentNode.insertBefore(sc,s); })(); </script> <script type="text/javascript"> _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); </script> </body></html>

You might also like