Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 859171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:38:37+00:00 2026-05-15T08:38:37+00:00

Suppose there is an array, we want to find everything in the odd index

  • 0

Suppose there is an array, we want to find everything in the odd index (index starting with 0), and move it to the end.
Everything in the even index move it to the beginning.
The relative order of all odd index items and all even index items are preserved.

i.e. if the array is

a1 b1 a2 b2 ...  an bn    

after the operation it becomes

a1 a2 a3 ... an b1 b2 ... bn

Can this be done in-place and in O(n) time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:38:38+00:00Added an answer on May 15, 2026 at 8:38 am

    It is possible, but it is very complicated! A simpler O(nlogn) and O(1) space solution might be better to code and in terms of cache.

    We will solve a problem different from yours, but your problem is trivial to solve once we solve that problem.

    Consider the array to be

    b1, a1, b2, a2, ..., bn, an
    

    and you have to convert this to

    a1, a2, ..., an, b1, b2, ..., bn
    

    Working with indices 1 to 2n,

    we see that this is given by

    i -> (n+1)*i (mod 2n+1).
    

    An O(nlogn) time O(1) space solution

    We can use divide and conquer as follows.

    First for some m close to n/2 convert

    b1, a1, ..., bn , an

    to

    a1,a2,...am, b1,b2, ..bm, a(m+1), ..., an, b(m+1), ... , bn
    

    by recursively applying to first 2m elements, and then the remaining.

    Now all we need to do this cyclic shift the middle array by m spots (this can be done in O(n) time and O(1) space)

    to give

    a1, a2, .., am , a(m+1), ..., an, b1, b2, ..., bm, b(m+1), ..., bn.
    

    Of course, as IVlad pointed out, this needs O(logn) stack space. We can get around that by doing the following:

    We have:

    b1 a1, b2 a2, .. bm am, b(m+1) a(m+1), ..., bn an
    

    Now swap pairs in the latter part of the array to give

    b1 a1, b2 a2, .. bm am, a(m+1) b(m+1), ..., an bn
    

    Now cyclic shift the elements at odd position: b1, b2, .., bm, a(m+1), a(m+2) ..., a(n).

    This gives something like

    a(m+1) a1, a(m+2) a2, ..., a(2m) am, a(2m+1) b(m+1),...,an b(n-m), b1 b(n-m+1),...,bm bn
    

    Now again swap the latter part of the array to give

    a(m+1) a1, a(m+2) a2, ..., a(2m) am, b(m+1) a(2m+1),...,b(n-m) an,b(n-m+1) b1,..., bn bm
    

    Now recursively solve the first part and second part to give

    [a1 a2 ... am][a(m+1) ... a(2m)]   [a(2m+1) ...an b1 b2 .. bm][b(m+1) ... bn]
    

    This works whether 2m >= n or not.

    So, this is O(nlogn) time and O(1) space algorithm.


    An O(n) time O(1) space solution.

    The ideas used are similar to the ideas used in the following paper:
    A simple in-place algorithm for Inshuffle.

    You would need to read that paper to understand the below. I suggest you also read: How to master in-place array modification algorithms?

    This is basically the inverse permutation of what is solved in the paper above.

    It is enough to solve this when 2n+1 is a power of 3 = (3^m say), as we can use divide and conquer after that (like the O(nlogn) solution).

    Now 2n+1 and n+1 are relatively prime, so working modulo 3^m, we see that n+1 must be some power of 2. (See that paper again to see why: basically any number modulo 3^m which is relative prime to 3^m is a power of 2, again modulo 3^m).

    Say n+1 = 2^k (we don’t know k yet and note this is modulo 3^m).

    A way to find out k, compute powers of n+1 modulo 3^m, till it becomes 1. This gives us k (and is O(n) time at most).

    Now we can see that the cycles of the permutation (see above paper/stackoverflow link for what that is) start at

    2^a*3^b

    where 0 <= a < k, and 0 <= b < m.

    So you start with each possible pair (a,b) and follow the cycles of the permutation, and this gives an O(n) time, in-place algorithm, as you touch each element no more than a constant number of times!

    This was a bit brief(!) and if you need more info, please let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose there is a fully populated array of data String[n][3] myData. I want to
suppose there is a string of characters in an array of d[20] . How
Suppose there is an image_url column in database. I want the user to choose
I want to find similar strings in an array/dictionary and rename them by adding
Suppose there is a file named as SUN.txt File contains : a,b,dd,ss, I want
For example suppose I have a Car object and I want my array to
Suppose I want to construct something like Array[#1^#2 == 3 &, {3, 3}] And
Suppose you have an array (m, m) and want to make it (n, n).
Suppose I have an int-array and I want to modify it. I know that
Suppose there are two scripts Requester.php and Provider.php, and Requester requires processing from Provider

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.