Determine how many different arrays are possible












0















Suppose we have a boolean array of length X. The only rule is, TRUE must not occur twice in adjacent places. Especially the array with only false values is allowed. E.g. this is forbidden: [1,1,0,0,0] and these is allowed: [1,0,0,0,0], [0,0,0,0,0], [1,0,1,0,1] etc. How can I use dynamic programming to determine how many different valid arrays of length X there are?










share|improve this question

























  • So to clarify: you mean that a 1 is disallowed from occurring in two consecutive elements?

    – jamesdlin
    Nov 23 '18 at 21:28






  • 1





    what did you try so far?

    – Kurohige
    Nov 23 '18 at 21:29











  • @jamesdlin exactly

    – hearuige
    Nov 23 '18 at 21:31
















0















Suppose we have a boolean array of length X. The only rule is, TRUE must not occur twice in adjacent places. Especially the array with only false values is allowed. E.g. this is forbidden: [1,1,0,0,0] and these is allowed: [1,0,0,0,0], [0,0,0,0,0], [1,0,1,0,1] etc. How can I use dynamic programming to determine how many different valid arrays of length X there are?










share|improve this question

























  • So to clarify: you mean that a 1 is disallowed from occurring in two consecutive elements?

    – jamesdlin
    Nov 23 '18 at 21:28






  • 1





    what did you try so far?

    – Kurohige
    Nov 23 '18 at 21:29











  • @jamesdlin exactly

    – hearuige
    Nov 23 '18 at 21:31














0












0








0








Suppose we have a boolean array of length X. The only rule is, TRUE must not occur twice in adjacent places. Especially the array with only false values is allowed. E.g. this is forbidden: [1,1,0,0,0] and these is allowed: [1,0,0,0,0], [0,0,0,0,0], [1,0,1,0,1] etc. How can I use dynamic programming to determine how many different valid arrays of length X there are?










share|improve this question
















Suppose we have a boolean array of length X. The only rule is, TRUE must not occur twice in adjacent places. Especially the array with only false values is allowed. E.g. this is forbidden: [1,1,0,0,0] and these is allowed: [1,0,0,0,0], [0,0,0,0,0], [1,0,1,0,1] etc. How can I use dynamic programming to determine how many different valid arrays of length X there are?







algorithm combinatorics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:48









SomeWittyUsername

14.9k22967




14.9k22967










asked Nov 23 '18 at 21:26









hearuigehearuige

11




11













  • So to clarify: you mean that a 1 is disallowed from occurring in two consecutive elements?

    – jamesdlin
    Nov 23 '18 at 21:28






  • 1





    what did you try so far?

    – Kurohige
    Nov 23 '18 at 21:29











  • @jamesdlin exactly

    – hearuige
    Nov 23 '18 at 21:31



















  • So to clarify: you mean that a 1 is disallowed from occurring in two consecutive elements?

    – jamesdlin
    Nov 23 '18 at 21:28






  • 1





    what did you try so far?

    – Kurohige
    Nov 23 '18 at 21:29











  • @jamesdlin exactly

    – hearuige
    Nov 23 '18 at 21:31

















So to clarify: you mean that a 1 is disallowed from occurring in two consecutive elements?

– jamesdlin
Nov 23 '18 at 21:28





So to clarify: you mean that a 1 is disallowed from occurring in two consecutive elements?

– jamesdlin
Nov 23 '18 at 21:28




1




1





what did you try so far?

– Kurohige
Nov 23 '18 at 21:29





what did you try so far?

– Kurohige
Nov 23 '18 at 21:29













@jamesdlin exactly

– hearuige
Nov 23 '18 at 21:31





@jamesdlin exactly

– hearuige
Nov 23 '18 at 21:31












4 Answers
4






active

oldest

votes


















3














Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.



Then:





  • T0 = 0


  • F0 = 1


  • Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)


  • Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)

  • You want FX + TX.


So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.



(This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)






share|improve this answer
























  • Won't T0 = 1 as well, as there are no consecutive 1's in it?

    – saketk21
    Nov 23 '18 at 21:55











  • @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

    – ruakh
    Nov 23 '18 at 22:12











  • What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

    – saketk21
    Nov 23 '18 at 22:28













  • @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

    – ruakh
    Nov 23 '18 at 22:29











  • Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

    – saketk21
    Nov 23 '18 at 22:31





















0














I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.



Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases



1. the array has no 1's, only one case, and it is a valid array
2. the array has one 1's, all cases are valid
3. the array has two 1's, there are N - 1 cases which are not valid
4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid


...






share|improve this answer
























  • This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

    – saketk21
    Nov 23 '18 at 22:07











  • I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

    – CS Pei
    Nov 23 '18 at 22:38



















0














The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.






share|improve this answer































    0














    You don't really need dynamic programming.
    For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.



    X=1: valid arrays: 2



    X=2: valid arrays: 3



    X=3: valid arrays: 5



    X=4: valid arrays: 8



    and so on...



    Demonstration:



    Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
    And that's exactly the definition of the Fibonacci array.



    All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.



    You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).






    share|improve this answer


























    • One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

      – saketk21
      Nov 23 '18 at 22:05











    • you right, I added the demonstration too.

      – Selindek
      Nov 23 '18 at 22:16











    • If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

      – saketk21
      Nov 23 '18 at 22:24











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453099%2fdetermine-how-many-different-arrays-are-possible%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.



    Then:





    • T0 = 0


    • F0 = 1


    • Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)


    • Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)

    • You want FX + TX.


    So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.



    (This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)






    share|improve this answer
























    • Won't T0 = 1 as well, as there are no consecutive 1's in it?

      – saketk21
      Nov 23 '18 at 21:55











    • @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

      – ruakh
      Nov 23 '18 at 22:12











    • What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

      – saketk21
      Nov 23 '18 at 22:28













    • @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

      – ruakh
      Nov 23 '18 at 22:29











    • Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

      – saketk21
      Nov 23 '18 at 22:31


















    3














    Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.



    Then:





    • T0 = 0


    • F0 = 1


    • Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)


    • Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)

    • You want FX + TX.


    So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.



    (This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)






    share|improve this answer
























    • Won't T0 = 1 as well, as there are no consecutive 1's in it?

      – saketk21
      Nov 23 '18 at 21:55











    • @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

      – ruakh
      Nov 23 '18 at 22:12











    • What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

      – saketk21
      Nov 23 '18 at 22:28













    • @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

      – ruakh
      Nov 23 '18 at 22:29











    • Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

      – saketk21
      Nov 23 '18 at 22:31
















    3












    3








    3







    Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.



    Then:





    • T0 = 0


    • F0 = 1


    • Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)


    • Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)

    • You want FX + TX.


    So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.



    (This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)






    share|improve this answer













    Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.



    Then:





    • T0 = 0


    • F0 = 1


    • Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)


    • Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)

    • You want FX + TX.


    So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.



    (This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 21:51









    ruakhruakh

    126k13205257




    126k13205257













    • Won't T0 = 1 as well, as there are no consecutive 1's in it?

      – saketk21
      Nov 23 '18 at 21:55











    • @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

      – ruakh
      Nov 23 '18 at 22:12











    • What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

      – saketk21
      Nov 23 '18 at 22:28













    • @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

      – ruakh
      Nov 23 '18 at 22:29











    • Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

      – saketk21
      Nov 23 '18 at 22:31





















    • Won't T0 = 1 as well, as there are no consecutive 1's in it?

      – saketk21
      Nov 23 '18 at 21:55











    • @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

      – ruakh
      Nov 23 '18 at 22:12











    • What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

      – saketk21
      Nov 23 '18 at 22:28













    • @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

      – ruakh
      Nov 23 '18 at 22:29











    • Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

      – saketk21
      Nov 23 '18 at 22:31



















    Won't T0 = 1 as well, as there are no consecutive 1's in it?

    – saketk21
    Nov 23 '18 at 21:55





    Won't T0 = 1 as well, as there are no consecutive 1's in it?

    – saketk21
    Nov 23 '18 at 21:55













    @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

    – ruakh
    Nov 23 '18 at 22:12





    @saketk21: I'm sorry, I don't understand your question. (What does your pronoun "it" refer to?)

    – ruakh
    Nov 23 '18 at 22:12













    What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

    – saketk21
    Nov 23 '18 at 22:28







    What I was confused about is the base case. Why T0 = 0 and F0 = 1? As they both are of length 0, then they both should be 0. If you consider length 1 as the base case, then both of them should be 1, as the possible arrays are 0 and 1, i.e. T1 = 1 and F1 = 1.

    – saketk21
    Nov 23 '18 at 22:28















    @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

    – ruakh
    Nov 23 '18 at 22:29





    @saketk21: Are you asking why the empty array doesn't end in 1? Or are you asking why I chose to define Tᵢ as only counting arrays that do end in 1?

    – ruakh
    Nov 23 '18 at 22:29













    Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

    – saketk21
    Nov 23 '18 at 22:31







    Your latest comment helped me figure it out. I misinterpreted F as the number of arrays with length i ending in 0, rather than not ending in 1, which means an empty array is also counted in F.

    – saketk21
    Nov 23 '18 at 22:31















    0














    I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.



    Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases



    1. the array has no 1's, only one case, and it is a valid array
    2. the array has one 1's, all cases are valid
    3. the array has two 1's, there are N - 1 cases which are not valid
    4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid


    ...






    share|improve this answer
























    • This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

      – saketk21
      Nov 23 '18 at 22:07











    • I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

      – CS Pei
      Nov 23 '18 at 22:38
















    0














    I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.



    Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases



    1. the array has no 1's, only one case, and it is a valid array
    2. the array has one 1's, all cases are valid
    3. the array has two 1's, there are N - 1 cases which are not valid
    4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid


    ...






    share|improve this answer
























    • This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

      – saketk21
      Nov 23 '18 at 22:07











    • I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

      – CS Pei
      Nov 23 '18 at 22:38














    0












    0








    0







    I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.



    Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases



    1. the array has no 1's, only one case, and it is a valid array
    2. the array has one 1's, all cases are valid
    3. the array has two 1's, there are N - 1 cases which are not valid
    4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid


    ...






    share|improve this answer













    I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.



    Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases



    1. the array has no 1's, only one case, and it is a valid array
    2. the array has one 1's, all cases are valid
    3. the array has two 1's, there are N - 1 cases which are not valid
    4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid


    ...







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 21:41









    CS PeiCS Pei

    8,4082039




    8,4082039













    • This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

      – saketk21
      Nov 23 '18 at 22:07











    • I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

      – CS Pei
      Nov 23 '18 at 22:38



















    • This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

      – saketk21
      Nov 23 '18 at 22:07











    • I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

      – CS Pei
      Nov 23 '18 at 22:38

















    This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

    – saketk21
    Nov 23 '18 at 22:07





    This won't work. For e.g. in case of array of length 5, 2 consecutive 1's which you consider are 11xxx, x11xx, xx11x and xxx11. However, x can still take value 0 or 1 (apart from the cases you handle in higher number of consecutive 1's).

    – saketk21
    Nov 23 '18 at 22:07













    I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

    – CS Pei
    Nov 23 '18 at 22:38





    I think it works. for example, N = 5. case 1 and 2 are all valid. case 3 will have 4 invalid cases. case 4 will have 4 * 3 / 2 = 6 invalid cases. case 5 4 * C(4,2) / 3! = 8 invalid cases. case 6 will have 1 invalid cases. so it is 2^5 - (1 + 4 + 6 + 8) = 13

    – CS Pei
    Nov 23 '18 at 22:38











    0














    The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.






    share|improve this answer




























      0














      The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.






      share|improve this answer


























        0












        0








        0







        The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.






        share|improve this answer













        The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 21:52









        Golam Rahman TusharGolam Rahman Tushar

        846




        846























            0














            You don't really need dynamic programming.
            For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.



            X=1: valid arrays: 2



            X=2: valid arrays: 3



            X=3: valid arrays: 5



            X=4: valid arrays: 8



            and so on...



            Demonstration:



            Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
            And that's exactly the definition of the Fibonacci array.



            All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.



            You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).






            share|improve this answer


























            • One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

              – saketk21
              Nov 23 '18 at 22:05











            • you right, I added the demonstration too.

              – Selindek
              Nov 23 '18 at 22:16











            • If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

              – saketk21
              Nov 23 '18 at 22:24
















            0














            You don't really need dynamic programming.
            For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.



            X=1: valid arrays: 2



            X=2: valid arrays: 3



            X=3: valid arrays: 5



            X=4: valid arrays: 8



            and so on...



            Demonstration:



            Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
            And that's exactly the definition of the Fibonacci array.



            All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.



            You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).






            share|improve this answer


























            • One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

              – saketk21
              Nov 23 '18 at 22:05











            • you right, I added the demonstration too.

              – Selindek
              Nov 23 '18 at 22:16











            • If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

              – saketk21
              Nov 23 '18 at 22:24














            0












            0








            0







            You don't really need dynamic programming.
            For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.



            X=1: valid arrays: 2



            X=2: valid arrays: 3



            X=3: valid arrays: 5



            X=4: valid arrays: 8



            and so on...



            Demonstration:



            Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
            And that's exactly the definition of the Fibonacci array.



            All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.



            You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).






            share|improve this answer















            You don't really need dynamic programming.
            For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.



            X=1: valid arrays: 2



            X=2: valid arrays: 3



            X=3: valid arrays: 5



            X=4: valid arrays: 8



            and so on...



            Demonstration:



            Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
            And that's exactly the definition of the Fibonacci array.



            All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.



            You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 22:14

























            answered Nov 23 '18 at 22:03









            SelindekSelindek

            1,195914




            1,195914













            • One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

              – saketk21
              Nov 23 '18 at 22:05











            • you right, I added the demonstration too.

              – Selindek
              Nov 23 '18 at 22:16











            • If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

              – saketk21
              Nov 23 '18 at 22:24



















            • One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

              – saketk21
              Nov 23 '18 at 22:05











            • you right, I added the demonstration too.

              – Selindek
              Nov 23 '18 at 22:16











            • If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

              – saketk21
              Nov 23 '18 at 22:24

















            One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

            – saketk21
            Nov 23 '18 at 22:05





            One must assume it is important to the OP to actually know how you came up with the answer rather than just know the answer.

            – saketk21
            Nov 23 '18 at 22:05













            you right, I added the demonstration too.

            – Selindek
            Nov 23 '18 at 22:16





            you right, I added the demonstration too.

            – Selindek
            Nov 23 '18 at 22:16













            If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

            – saketk21
            Nov 23 '18 at 22:24





            If you mean Binet's Formula for Nth fibonacci number, then it is only an approximation and not the correct result. Nth fibonacci number can be computed at best in O(log n)

            – saketk21
            Nov 23 '18 at 22:24


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453099%2fdetermine-how-many-different-arrays-are-possible%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin