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:
110011111000
- then the output is 5 (from pos 5 to 10)1110011101
- the output here is 3 but after rotation 1 the array becomes111100111
and the output is now 4.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
|
show 1 more comment
up vote
1
down vote
favorite
Is there any way to find the longest subarray of 1's in log(n) time?
example:
110011111000
- then the output is 5 (from pos 5 to 10)1110011101
- the output here is 3 but after rotation 1 the array becomes111100111
and the output is now 4.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
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
|
show 1 more comment
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:
110011111000
- then the output is 5 (from pos 5 to 10)1110011101
- the output here is 3 but after rotation 1 the array becomes111100111
and the output is now 4.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
Is there any way to find the longest subarray of 1's in log(n) time?
example:
110011111000
- then the output is 5 (from pos 5 to 10)1110011101
- the output here is 3 but after rotation 1 the array becomes111100111
and the output is now 4.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
arrays algorithm rotation subsequence
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
|
show 1 more comment
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
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can follow those steps:
- find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.
- Start rotating and update those parameters.
- 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:
- Nothing change
- Bigger subarray created
- 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!
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 this011101101
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
add a comment |
up vote
0
down vote
No, because you have to know every letter value to count 1s, which is O(n) at least.
add a comment |
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:
- find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.
- Start rotating and update those parameters.
- 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:
- Nothing change
- Bigger subarray created
- 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!
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 this011101101
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
add a comment |
up vote
1
down vote
accepted
You can follow those steps:
- find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.
- Start rotating and update those parameters.
- 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:
- Nothing change
- Bigger subarray created
- 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!
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 this011101101
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
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can follow those steps:
- find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.
- Start rotating and update those parameters.
- 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:
- Nothing change
- Bigger subarray created
- 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!
You can follow those steps:
- find the longest subarray of 1 (in O(n)). During that loop find the first and last instance of 0.
- Start rotating and update those parameters.
- 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:
- Nothing change
- Bigger subarray created
- 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!
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 this011101101
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
add a comment |
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 this011101101
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
add a comment |
up vote
0
down vote
No, because you have to know every letter value to count 1s, which is O(n) at least.
add a comment |
up vote
0
down vote
No, because you have to know every letter value to count 1s, which is O(n) at least.
add a comment |
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.
No, because you have to know every letter value to count 1s, which is O(n) at least.
answered Nov 11 at 10:18
Erez.S
32511
32511
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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