Max subarray length which sum not bigger than the main array length

Possible sub-arrays for [1, 2, 3, 4]

[1],
[2], [1, 2],
[3], [1, 3], [2, 3], [1, 2, 3],
[4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]

6 replies
    • Génesis
      Génesis says:

      The Max sub-array length which sum not bigger than the main array length is 1 in that case, it’s ok, so what did you get?

      [1], [1, 3], [1, 4], [1, 3, 4]
      [3], [3, 4]
      [4]

      The chosen sub-array is [3] with a length of 1

      • japanfever
        japanfever says:

        ha, ha [1, 3, 4] it’s a bug in your code!
        your code does not generate all possible subarrays, so the rest is wrong too
        where is [1, 3, 4] in your output?
        i’ve written a good algorithm that generates all the subarrays in all cases, only in 4 lines, but I do not put it here ’cause your blog does not format the source code well.always trim my code
        https://ja.wikipedia.org/wiki/冪集合

        • Génesis
          Génesis says:

          There aren’t bugs in my code 😛 I missed [1, 3, 4] in my previous comment. You can add a console.log(subarrays) to my JSFiddle link and see for yourself that I’m generating all the possibles subarrays 😉
          I think maybe I abused a lot using reduce. Do you have any link to your code? (if you copy plain text between code tags my blog should not trim it) By the way nice link to Power set on Wikipedia.

          • japanfever
            japanfever says:

            you like me because you’re a stubborn troll who does not recognize his mistakes. it’s funny. are you blind or do you really not see in the cover of your blogpost that [1, 3, 4] is missing?

            ok, i’m going to follow the game and i’m going to modify the line in jsfiddle to be able to see in the console what we all know … THE BUG IN YOUR CODE !! hahahaha

            http://jsfiddle.net/1ybrktg3/2/

            console output:
            0: Array [ 1 ]
            ​1: Array [ 1, 2 ]
            2: Array [ 1, 3 ]
            3: Array [ 1, 4 ]
            4: Array(3) [ 1, 2, 3 ]
            5: Array(3) [ 1, 2, 4 ]
            6: Array(4) [ 1, 2, 3, 4 ]
            7: Array [ 2 ]
            8: Array [ 2, 3 ]
            9: Array [ 2, 4 ]
            10: Array(3) [ 2, 3, 4 ]
            11: Array [ 3 ]
            12: Array [ 3, 4 ]
            13: Array [ 4 ]
            length: 14
            __proto__: Array []

            again……..where is [1, 3, 4] ?
            (you will say: I do see it!, I do see it! hahahaha)

            with the wiki link that i have set it’s easy to implement the correct algorithm, so i guess you’re kidding
            regards

          • Génesis
            Génesis says:

            Ok, I thought you were testing the input [1, 3, 4]. I think this is not good for your humbleness but you were right, I missed a case 🙂 I have fixed it and made it easier!

Comments are closed.