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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:04:35+00:00 2026-05-11T05:04:35+00:00

suppose I have a vector: 0 1 2 3 4 5 [45,89,22,31,23,76] And a

  • 0

suppose I have a vector:

 0  1  2  3  4  5 [45,89,22,31,23,76] 

And a permutation of its indices:

[5,3,2,1,0,4] 

Is there an efficient way to resort it according to the permutation thus obtaining:

[76,31,22,89,45,23] 

Using at most O(1) additional space?

  • 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. 2026-05-11T05:04:35+00:00Added an answer on May 11, 2026 at 5:04 am

    Yes. Starting from the leftmost position, we put the element there in its correct position i by swapping it with the (other) misplaced element at that position i. This is where we need the O(1) additional space. We keep swapping pairs of elements around until the element in this position is correct. Only then do we proceed to the next position and do the same thing.

    Example:

    [5 3 2 1 0 4] initial state

    [4 3 2 1 0 5] swapped (5,4), 5 is now in the correct position, but 4 is still wrong

    [0 3 2 1 4 5] swapped (4,0), now both 4 and 0 are in the correct positions, move on to next position

    [0 1 2 3 4 5] swapped (3,1), now 1 and 3 are both in the correct positions, move on to next position

    [0 1 2 3 4 5] all elements are in the correct positions, end.

    Note:

    Since each swap operation puts at least one (of the two) elements in the correct position, we need no more than N such swaps altogether.

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

Sidebar

Related Questions

Suppose I have a vector std::vector<a> A; I can get access to its member-functions
using namespace std; vector<vector<T> > vec_collection(3); vec_collection[0]=vector<T>(12); vec_collection[1]=vector<T>(3); vec_collection[2]=vector<T>(14); Suppose I have an empty
Suppose I have a vector<int> myvec and I want to loop through all of
Suppose I have an R vector of unique elements such as x <- c(1,2,3,4,5)
Suppose I have VectorA and VectorB are two std::vector<SameType> , both initilized (i mean
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have an (m x n) matrix Q, and a row vector r,
suppose we have a vector<student> allstudent Now I would like to sort the students
Suppose I have a named vector, bar : bar=c() bar[1997-10-14]=1 bar[2001-10-14]=2 bar[2007-10-14]=1 How can
Suppose that I have a vector of key-value pairs that I want to put

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.