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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:40:11+00:00 2026-05-28T18:40:11+00:00

here’s my dilema. I have for example this string: 1 2 3 4 5

  • 0

here’s my dilema.

I have for example this string:

“1 2 3 4 5 ….. 100” , but it can be any length (usually much, much bigger one, talking about 4 digit numbers).

What I need to do: re-arrange the elements of the string based on a known possition.
E.g: the position in question is 70. I need to have the following output:
“70 71 ….100 1 2 3 …69”

Conditions:

  1. I know the key: e.g the above “70”. Can be also 500, 5000, depends on the string length.
  2. I can’t use any string buffers or string management functions.
  3. There’s little to none memory available.
  4. There are two 1 byte buffers available.
  5. Operation should be done in with the least possible steps (time critical).

I’ve been trying to find a good algorithm that would not depend on the position of key in the string. Basically shifting left/right depending on what half I am still makes it for a lot of reads/writes and I don’t want that.

Thanks a lot!

  • 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-28T18:40:12+00:00Added an answer on May 28, 2026 at 6:40 pm

    this code fits your decription, but you’ve been vague enough that I doubt it does what you want…

    #include <string>
    #include <iostream>
    #include <cmath>
    #include <cassert>
    #include <stdint.h>
    
    int main() {
        int n = 15;
        std::string s = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  ";
    
        uint16_t index;
        switch((int)log10(n)) {
        case 0: // 0 - 9 
            // -2 to account for lack of "0" in the list which would have occupied 2 chars..
            index = (2 * (n)) + -2;
            break;
        case 1: // 10 - 99
            index = (3 * (n % 10)) + (10 * 2 - 2);
            break;
        case 2: // 100 - 999
            index = (4 * (n % 100)) + (90 * 3) + (10 * 2 - 2);
            break;
        case 3: // 1000 - 9999
            index = (5 * (n % 1000)) + (900 * 4) + (90 * 3) + (10 * 2 - 2);
            break;
        }
    
        std::string::iterator first  = s.begin();
        std::string::iterator middle = s.begin() + index;
        std::string::iterator last   = s.end();
    
        std::string::iterator next = middle;
        while(first != next) {
    
            char temp1;
            temp1 = *first;
            *first = *next;
            *next = temp1;
    
            ++first;
            ++next;
    
            if (next == last) {
                next = middle;
            } else if (first == middle) {
                middle = next;
            }
        }
    
        std::cout << s << std::endl;
    }
    

    Produces the following output:

    $ ./test 
    15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14
    

    But should work just the same for sequences all the way up to 9999

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

Sidebar

Related Questions

Here is what I am trying to achieve in PHP: I have this string:
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is an example: I have the generic type called Account. I wish to
Here's the situation, i want to have a user that can enter time on
Here is an example: I have a file 1.js, which has some functions. I
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes

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.