Longest sub array in rotating array









up vote
1
down vote

favorite












Is there any way to find the longest subarray of 1's in log(n) time?



example:



  1. 110011111000 - then the output is 5 (from pos 5 to 10)


  2. 1110011101 - the output here is 3 but after rotation 1 the array becomes 111100111 and the output is now 4.


  3. 001111 - the output here is 4 from pos 3 to 6 but after rotation it becomes 3 from pos 4 to 6


Note: I found the longest subarray length in O(n) before rotation. How can I improve the solution after rotation if I have the past results?










share|improve this question























  • Finding the longest subarray of 1 before any rotation is O(n). You can improve the check after rotation to O(1) if you have the previous results
    – David Winder
    Nov 11 at 10:17










  • @DavidWinder can you put up the algorithm please..... thats exactly what i want to do
    – Mike3096
    Nov 11 at 10:22










  • Out for lunch - will post detailed answer later
    – David Winder
    Nov 11 at 10:31










  • how many time you will rotate the array?
    – nightfury1204
    Nov 11 at 10:35










  • @nightfury1204 no. of roations = length of array
    – Mike3096
    Nov 11 at 10:44














up vote
1
down vote

favorite












Is there any way to find the longest subarray of 1's in log(n) time?



example:



  1. 110011111000 - then the output is 5 (from pos 5 to 10)


  2. 1110011101 - the output here is 3 but after rotation 1 the array becomes 111100111 and the output is now 4.


  3. 001111 - the output here is 4 from pos 3 to 6 but after rotation it becomes 3 from pos 4 to 6


Note: I found the longest subarray length in O(n) before rotation. How can I improve the solution after rotation if I have the past results?










share|improve this question























  • Finding the longest subarray of 1 before any rotation is O(n). You can improve the check after rotation to O(1) if you have the previous results
    – David Winder
    Nov 11 at 10:17










  • @DavidWinder can you put up the algorithm please..... thats exactly what i want to do
    – Mike3096
    Nov 11 at 10:22










  • Out for lunch - will post detailed answer later
    – David Winder
    Nov 11 at 10:31










  • how many time you will rotate the array?
    – nightfury1204
    Nov 11 at 10:35










  • @nightfury1204 no. of roations = length of array
    – Mike3096
    Nov 11 at 10:44












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Is there any way to find the longest subarray of 1's in log(n) time?



example:



  1. 110011111000 - then the output is 5 (from pos 5 to 10)


  2. 1110011101 - the output here is 3 but after rotation 1 the array becomes 111100111 and the output is now 4.


  3. 001111 - the output here is 4 from pos 3 to 6 but after rotation it becomes 3 from pos 4 to 6


Note: I found the longest subarray length in O(n) before rotation. How can I improve the solution after rotation if I have the past results?










share|improve this question















Is there any way to find the longest subarray of 1's in log(n) time?



example:



  1. 110011111000 - then the output is 5 (from pos 5 to 10)


  2. 1110011101 - the output here is 3 but after rotation 1 the array becomes 111100111 and the output is now 4.


  3. 001111 - the output here is 4 from pos 3 to 6 but after rotation it becomes 3 from pos 4 to 6


Note: I found the longest subarray length in O(n) before rotation. How can I improve the solution after rotation if I have the past results?







arrays algorithm rotation subsequence






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:17









David Winder

2,8602723




2,8602723










asked Nov 11 at 10:09









Mike3096

85




85











  • Finding the longest subarray of 1 before any rotation is O(n). You can improve the check after rotation to O(1) if you have the previous results
    – David Winder
    Nov 11 at 10:17










  • @DavidWinder can you put up the algorithm please..... thats exactly what i want to do
    – Mike3096
    Nov 11 at 10:22










  • Out for lunch - will post detailed answer later
    – David Winder
    Nov 11 at 10:31










  • how many time you will rotate the array?
    – nightfury1204
    Nov 11 at 10:35










  • @nightfury1204 no. of roations = length of array
    – Mike3096
    Nov 11 at 10:44
















  • Finding the longest subarray of 1 before any rotation is O(n). You can improve the check after rotation to O(1) if you have the previous results
    – David Winder
    Nov 11 at 10:17










  • @DavidWinder can you put up the algorithm please..... thats exactly what i want to do
    – Mike3096
    Nov 11 at 10:22










  • Out for lunch - will post detailed answer later
    – David Winder
    Nov 11 at 10:31










  • how many time you will rotate the array?
    – nightfury1204
    Nov 11 at 10:35










  • @nightfury1204 no. of roations = length of array
    – Mike3096
    Nov 11 at 10:44















Finding the longest subarray of 1 before any rotation is O(n). You can improve the check after rotation to O(1) if you have the previous results
– David Winder
Nov 11 at 10:17




Finding the longest subarray of 1 before any rotation is O(n). You can improve the check after rotation to O(1) if you have the previous results
– David Winder
Nov 11 at 10:17












@DavidWinder can you put up the algorithm please..... thats exactly what i want to do
– Mike3096
Nov 11 at 10:22




@DavidWinder can you put up the algorithm please..... thats exactly what i want to do
– Mike3096
Nov 11 at 10:22












Out for lunch - will post detailed answer later
– David Winder
Nov 11 at 10:31




Out for lunch - will post detailed answer later
– David Winder
Nov 11 at 10:31












how many time you will rotate the array?
– nightfury1204
Nov 11 at 10:35




how many time you will rotate the array?
– nightfury1204
Nov 11 at 10:35












@nightfury1204 no. of roations = length of array
– Mike3096
Nov 11 at 10:44




@nightfury1204 no. of roations = length of array
– Mike3096
Nov 11 at 10:44












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can follow those steps:



  1. find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.

  2. Start rotating and update those parameters.

  3. If the last index is 0 search for the previous 1 to keep updated the parameters (at complexity this will not go over O(n) total.

I will assume you know how to get max subarray index and count in O(n). In addition to that, you will need to find the second largest subarray (can be done in the same loop).



Let extract 2 more params - first and last zeros (Simple code - I can attach it if you need )



When you rotate the array there are 3 option:



  1. Nothing change

  2. Bigger subarray created

  3. Current biggest subarray breaks

In the first and second cases - you only need to update the params - O(1) - you can know this is the case according your params. In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time)



For example, consider you have array: 1110011101 (as your example) and you have max = 3 and maxIndex = 5. After running the getZeroIndexs function you also know that firstZeroIndex = 3 and lastZeroIndex = 8.



How our var will look like after rotate?



max = 3
maxIndex = 6
firstZeroIndex = 4 // this will increase as long as the lastZeroIndex different then the array size
lastZeroIndex = 9 //this will go up till array.size - when it those you need to loop again till you find last


In this case, the first index move to 4 whats make him bigger then max -> max = 4 and maxIndex = 0.



Now your array is : 1111001110 so lastZeroIndex = 9 as the array size so next rotation will yield:



max = 4
maxIndex = 1
firstZeroIndex = 0
lastZeroIndex = ? // here you need to loop from the end of your array till you get 0 -> O(k) in total complexity, all the run together will be O(n)


Hope it clear, if not feel free to ask!






share|improve this answer




















  • That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
    – Mike3096
    Nov 11 at 12:16










  • In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
    – nightfury1204
    Nov 11 at 12:20










  • @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
    – David Winder
    Nov 11 at 12:47










  • @DavidWinder thanks. got it
    – nightfury1204
    Nov 11 at 13:04

















up vote
0
down vote













No, because you have to know every letter value to count 1s, which is O(n) at least.






share|improve this answer




















    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',
    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%2f53247660%2flongest-sub-array-in-rotating-array%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You can follow those steps:



    1. find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.

    2. Start rotating and update those parameters.

    3. If the last index is 0 search for the previous 1 to keep updated the parameters (at complexity this will not go over O(n) total.

    I will assume you know how to get max subarray index and count in O(n). In addition to that, you will need to find the second largest subarray (can be done in the same loop).



    Let extract 2 more params - first and last zeros (Simple code - I can attach it if you need )



    When you rotate the array there are 3 option:



    1. Nothing change

    2. Bigger subarray created

    3. Current biggest subarray breaks

    In the first and second cases - you only need to update the params - O(1) - you can know this is the case according your params. In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time)



    For example, consider you have array: 1110011101 (as your example) and you have max = 3 and maxIndex = 5. After running the getZeroIndexs function you also know that firstZeroIndex = 3 and lastZeroIndex = 8.



    How our var will look like after rotate?



    max = 3
    maxIndex = 6
    firstZeroIndex = 4 // this will increase as long as the lastZeroIndex different then the array size
    lastZeroIndex = 9 //this will go up till array.size - when it those you need to loop again till you find last


    In this case, the first index move to 4 whats make him bigger then max -> max = 4 and maxIndex = 0.



    Now your array is : 1111001110 so lastZeroIndex = 9 as the array size so next rotation will yield:



    max = 4
    maxIndex = 1
    firstZeroIndex = 0
    lastZeroIndex = ? // here you need to loop from the end of your array till you get 0 -> O(k) in total complexity, all the run together will be O(n)


    Hope it clear, if not feel free to ask!






    share|improve this answer




















    • That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
      – Mike3096
      Nov 11 at 12:16










    • In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
      – nightfury1204
      Nov 11 at 12:20










    • @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
      – David Winder
      Nov 11 at 12:47










    • @DavidWinder thanks. got it
      – nightfury1204
      Nov 11 at 13:04














    up vote
    1
    down vote



    accepted










    You can follow those steps:



    1. find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.

    2. Start rotating and update those parameters.

    3. If the last index is 0 search for the previous 1 to keep updated the parameters (at complexity this will not go over O(n) total.

    I will assume you know how to get max subarray index and count in O(n). In addition to that, you will need to find the second largest subarray (can be done in the same loop).



    Let extract 2 more params - first and last zeros (Simple code - I can attach it if you need )



    When you rotate the array there are 3 option:



    1. Nothing change

    2. Bigger subarray created

    3. Current biggest subarray breaks

    In the first and second cases - you only need to update the params - O(1) - you can know this is the case according your params. In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time)



    For example, consider you have array: 1110011101 (as your example) and you have max = 3 and maxIndex = 5. After running the getZeroIndexs function you also know that firstZeroIndex = 3 and lastZeroIndex = 8.



    How our var will look like after rotate?



    max = 3
    maxIndex = 6
    firstZeroIndex = 4 // this will increase as long as the lastZeroIndex different then the array size
    lastZeroIndex = 9 //this will go up till array.size - when it those you need to loop again till you find last


    In this case, the first index move to 4 whats make him bigger then max -> max = 4 and maxIndex = 0.



    Now your array is : 1111001110 so lastZeroIndex = 9 as the array size so next rotation will yield:



    max = 4
    maxIndex = 1
    firstZeroIndex = 0
    lastZeroIndex = ? // here you need to loop from the end of your array till you get 0 -> O(k) in total complexity, all the run together will be O(n)


    Hope it clear, if not feel free to ask!






    share|improve this answer




















    • That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
      – Mike3096
      Nov 11 at 12:16










    • In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
      – nightfury1204
      Nov 11 at 12:20










    • @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
      – David Winder
      Nov 11 at 12:47










    • @DavidWinder thanks. got it
      – nightfury1204
      Nov 11 at 13:04












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You can follow those steps:



    1. find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.

    2. Start rotating and update those parameters.

    3. If the last index is 0 search for the previous 1 to keep updated the parameters (at complexity this will not go over O(n) total.

    I will assume you know how to get max subarray index and count in O(n). In addition to that, you will need to find the second largest subarray (can be done in the same loop).



    Let extract 2 more params - first and last zeros (Simple code - I can attach it if you need )



    When you rotate the array there are 3 option:



    1. Nothing change

    2. Bigger subarray created

    3. Current biggest subarray breaks

    In the first and second cases - you only need to update the params - O(1) - you can know this is the case according your params. In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time)



    For example, consider you have array: 1110011101 (as your example) and you have max = 3 and maxIndex = 5. After running the getZeroIndexs function you also know that firstZeroIndex = 3 and lastZeroIndex = 8.



    How our var will look like after rotate?



    max = 3
    maxIndex = 6
    firstZeroIndex = 4 // this will increase as long as the lastZeroIndex different then the array size
    lastZeroIndex = 9 //this will go up till array.size - when it those you need to loop again till you find last


    In this case, the first index move to 4 whats make him bigger then max -> max = 4 and maxIndex = 0.



    Now your array is : 1111001110 so lastZeroIndex = 9 as the array size so next rotation will yield:



    max = 4
    maxIndex = 1
    firstZeroIndex = 0
    lastZeroIndex = ? // here you need to loop from the end of your array till you get 0 -> O(k) in total complexity, all the run together will be O(n)


    Hope it clear, if not feel free to ask!






    share|improve this answer












    You can follow those steps:



    1. find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.

    2. Start rotating and update those parameters.

    3. If the last index is 0 search for the previous 1 to keep updated the parameters (at complexity this will not go over O(n) total.

    I will assume you know how to get max subarray index and count in O(n). In addition to that, you will need to find the second largest subarray (can be done in the same loop).



    Let extract 2 more params - first and last zeros (Simple code - I can attach it if you need )



    When you rotate the array there are 3 option:



    1. Nothing change

    2. Bigger subarray created

    3. Current biggest subarray breaks

    In the first and second cases - you only need to update the params - O(1) - you can know this is the case according your params. In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time)



    For example, consider you have array: 1110011101 (as your example) and you have max = 3 and maxIndex = 5. After running the getZeroIndexs function you also know that firstZeroIndex = 3 and lastZeroIndex = 8.



    How our var will look like after rotate?



    max = 3
    maxIndex = 6
    firstZeroIndex = 4 // this will increase as long as the lastZeroIndex different then the array size
    lastZeroIndex = 9 //this will go up till array.size - when it those you need to loop again till you find last


    In this case, the first index move to 4 whats make him bigger then max -> max = 4 and maxIndex = 0.



    Now your array is : 1111001110 so lastZeroIndex = 9 as the array size so next rotation will yield:



    max = 4
    maxIndex = 1
    firstZeroIndex = 0
    lastZeroIndex = ? // here you need to loop from the end of your array till you get 0 -> O(k) in total complexity, all the run together will be O(n)


    Hope it clear, if not feel free to ask!







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 11 at 12:03









    David Winder

    2,8602723




    2,8602723











    • That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
      – Mike3096
      Nov 11 at 12:16










    • In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
      – nightfury1204
      Nov 11 at 12:20










    • @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
      – David Winder
      Nov 11 at 12:47










    • @DavidWinder thanks. got it
      – nightfury1204
      Nov 11 at 13:04
















    • That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
      – Mike3096
      Nov 11 at 12:16










    • In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
      – nightfury1204
      Nov 11 at 12:20










    • @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
      – David Winder
      Nov 11 at 12:47










    • @DavidWinder thanks. got it
      – nightfury1204
      Nov 11 at 13:04















    That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
    – Mike3096
    Nov 11 at 12:16




    That really helped ......... i might ask for sudo code if required...... Thanks alot Sir
    – Mike3096
    Nov 11 at 12:16












    In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
    – nightfury1204
    Nov 11 at 12:20




    In the third, you will need to use the second longest subarray you find (notice that only 1 subarray can be break at a time) How can you keep track of second longest subarray in O(1), since in rotate operation break subarray. consider for this 011101101 after 6th rotation.
    – nightfury1204
    Nov 11 at 12:20












    @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
    – David Winder
    Nov 11 at 12:47




    @nightfury1204 You only need to keep the second array index -> increase it every iteration and move to 0 when at the end - > notice that when it breaks that mean the longest subarray is not broke (only one can be broken at each step) so you can just return the biggest - in case the biggest broke you can return the second
    – David Winder
    Nov 11 at 12:47












    @DavidWinder thanks. got it
    – nightfury1204
    Nov 11 at 13:04




    @DavidWinder thanks. got it
    – nightfury1204
    Nov 11 at 13:04












    up vote
    0
    down vote













    No, because you have to know every letter value to count 1s, which is O(n) at least.






    share|improve this answer
























      up vote
      0
      down vote













      No, because you have to know every letter value to count 1s, which is O(n) at least.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        No, because you have to know every letter value to count 1s, which is O(n) at least.






        share|improve this answer












        No, because you have to know every letter value to count 1s, which is O(n) at least.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 10:18









        Erez.S

        32511




        32511



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53247660%2flongest-sub-array-in-rotating-array%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto