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 8339957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:01:53+00:00 2026-06-09T05:01:53+00:00

Initial array: 2 23 34 27 89 14 26 30 60 k = 3

  • 0

Initial array: 2 23 34 27 89 14 26 30 60

k = 3

starting from index i = 1 we have to shift k elements so that they occur after the element 26 (i.e. after givenIndex = 6) in the array.

Final array: 2 89 14 26 23 34 27 30 60

We are NOT allowed to use extra space.

My approach:

count = 0;   
while(count < k)  
{  
  count++;  
  temp = arr[i];  
  shift all elements from (i+1) to givenIndex to their immediate left position;  
  arr[givenIndex] = temp;  
}

First iteration:
temp = 23
shift all elements from [i+1](i.e. index=2) to givenIndex(i.e. index=6) to left one by one
array after shifting: 2 34 27 89 14 26 26 30 60
arr[givenIndex] = temp
array after applying this operation: 2 34 27 89 14 26 23 30 60

Similarly
array after second iteration: 2 27 89 14 26 23 34 30 60
array after third iteration: 2 89 14 26 23 34 27 30 60

worst case complexity O(n*k) where n is the no. of elemnts in the array.

Can we solve the problem in O(n)

  • 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-06-09T05:01:55+00:00Added an answer on June 9, 2026 at 5:01 am

    This rotation can be done in linear time using a reverse() helper function. Assuming that reverse(x, y) reverses array[x]..array[y] in place.

    reverse(1, 3); // 27 34 23 .. .. ..
    reverse(4, 6); // .. .. .. 26 14 89
    reverse(1, 6); // 89 14 26 23 34 27
    

    Writing a linear-time reverse is simple and might even be available in your favorite language library (for example, C++ includes std::rotate and std::reverse).

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

Sidebar

Related Questions

i want to have a foreach loop where the initial array is changed inside
Ok, here's the deal. I have an array (inputted from a 400 MB file)
I have the very common problem of creating an index for an in-disk array
I have a 2-dimensional array in Ruby that I want to produce a working
A PHP array can have arrays for its elements. And those arrays can have
I have a character array that contains a phone number of the form: (xxx)xxx-xxxx
I have an array in PHP that is simply strings. These strings represent relationships
I have a 2D array such as: // construct initial **array int **arr; arr
I have an initial array: $arr0 = array( 0 => array( 'a' => 1,
Initial tests indicate that GDI+ (writing in VB.NET) is not fast enough for my

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.