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

  • Home
  • SEARCH
  • 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 6895967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:30+00:00 2026-05-27T06:58:30+00:00

I want to reverse an array (or any other data structure) but because this

  • 0

I want to reverse an array (or any other data structure) but because this operation is going to be done on the array for n times , im looking for the best solution possible, I have the sorted array , which is gotten in O(nlgn) time , i start looking for first element in the sorted array , inside the unsorted array ( which is equal to finding the smallest key in the unsorted array ) then I reverse the array from the beginning to the index of that value , then i do the same for the rest , find the second smallest value’s index , and reverse the array again , from the second index to the end of the array and so on :
for example , consider this array :

*‫2 6 (*1*) 5 4 *3‬  // array is reversed from its 0th index to the 3rd index (0 based)     
1 *5* 4 3 6 (*2*)  // array is reversed from its 1st index (0 based ) to the 5th index   
1 2 *6* *3* 4 5    // and ...  
1 2 3 *6* *4* 5  
1 2 3 4 *6* *5*  
1 2 3 4 5 6 

well i have to sort the array in order to have the values im looking for in the unsorted array , it’ll take o(nlgn) time , and doing the algorithm above , will take o(n^2) ,any idea to make it more quick , to be done in o(nlgn) time ? so the question is reversing a sub array of the array in the least time Order , cause it’s done for many times in large sized arrays. ( I can get the indices i and j ( which are the first and last index of the sub array ) in O (n) time cause i have the sorted array and i’ll just look up the numbers in the unsorted array ) so im looking for the best time order for reversing an array from it’s ith index to it’s jth index .
thanks in advance

  • 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-27T06:58:30+00:00Added an answer on May 27, 2026 at 6:58 am

    Here comes an O(n) solution (i think, reading your description was hard). It’s a data structure wich allows 1) reversing a sub-array in O(1), 2) Getting a value from the array in O(r), where r is the number of reversings that is done, 3) find the index of an element in O(n), where n is the length of the list.

    Just store our array as usual, and have a list of ReversedRange(imin, imax) elements. Reversing part of the array is as easy as inserting another element in this list.

    Whenever you need to get a value from the modified array at index i, you look through all the ReversedRange for which imin <= i <= imax, and calculate the index j which corresponds to the original array index. You need to check r reversings, so it is O(r).

    To get the index i of a value v, look through the original array and find the index j. Done in O(n) time. Do the same traversing of the ReversedRanges, only in the oppsite direction to calculate i. Done in O(r) time, total O(n+r) which is O(n).

    Example

    Consider the following list: 0 1 2 3 4 5 6 7 8 9. Now say we reverse the list form indexes 1 through 6, and then from 0 through 5. So we have:

    I    0 1 2 3 4 5 6 7 8 9
           |         |
    II   0 6 5 4 3 2 1 7 8 9
         |         |
    III  2 3 4 5 6 0 1 7 8 9
    

    No let us map the index i = 2 to the original array. From the graph we see that we should end up with III(2) = 4

    1) since i = 2 is in [0, 5] we calculate
        i <- 5 - (i - 0) = 5 - 2 = 3
    2) since i = 3 is in [1, 6] we calculate
        i <- 6 - (i - 1) = 6 - 2 = 4
    3) there are no more ranges, we are done with result 4!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not talking about array_flip() , i want to reverse an array without a
I want to use pointers to reverse a char array in C++. I was
So I have this function: function toAlpha($data){ $alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); $alpha_flip = array_flip($alphabet); if($data
Alright, Im going about this, with what is probably a really complicated solution, but
I want to reverse engineer (import into diagram form) the database definition dump of
I want to reverse each individual word of a String in Java (not the
I want to have a function that will return the reverse of a list
We have a reverse proxy here, running Apache in version 2.2.x Essentially I want
I have one list that I want to take a slice of, reverse that
Want the function to sort the table by HP but if duplicate HPs then

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.