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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:17:53+00:00 2026-06-14T17:17:53+00:00

The objective is to convert an array like a3b5c2 to aaabbbbbcc in-place. I have

  • 0

The objective is to convert an array like a3b5c2 to aaabbbbbcc in-place.
I have a solution:

  • Assuming that the array is of infinite size,
  • I parse the array from the end.
  • look for a number (say n)
  • depending on how many digits I get, I look for the next character.
  • When I get it, I move the elements from current position through end of array by n-1 positions.
  • Fill the open positions with the encountered character.

The complexity of this solution will be O(n^2). Is there a solution with complexity less than O(n^2)?

  • 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-14T17:17:55+00:00Added an answer on June 14, 2026 at 5:17 pm

    If you parse the array once, you can know where the last element will be by summing all the numerical values.

    Parse it once and find its final size.

    Once you do that, start filling it from the “end” (from where its final value will be): 2 times c, then 5 times b…

    This is a O(n) in-place solution.

    EDIT:

    As srbh.kmr said in comments, this won’t work if the array has a series of badly-placed characters repeated only one time. For instance, if we have the array a1b1c1d1e7, the answer above will erase the last letters.

    The only number that causes issues is 1, and we can treat it in O(n):

    Before treating the array as explained above, eliminate those ones. Starting from the beginning, parse the array and each time a 1 is found, erase it and move the remaining letters forward (not the whole remaining array, just the next characters). If several 1s are found in the array, the hole between the first and second parts of the array will get bigger. For the example array above, the steps look like this:

    a1b1c1d1e7
    // First parse gives length = 1+1+1+1+7
    
    // Repair ones
    a b1c1d1e7
    ab 1c1d1e7
    ab  c1d1e7
    abc  1d1e7
    abc   d1e7
    abcd   1e7
    abcd    e7
    abcde    7
    abcde7
    

    Then, apply the algorithm above. If no number is found after a character, just copy the character to its position at the end of the array:

    // Fill final array
    abcde7    x
              ^ 11th position
    abcde     e
    abcde    ee
    abcde   eee
    abcde  eeee
    abcde eeeee
    abcdeeeeeee
    abcdeeeeeee  // Here we overwrite the first "e"
    abcdeeeeeee  // Then we see there are lone letters (4 times), so we leave them.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Objective-C code that I'd like to convert to Objective-C++. To changed
How to convert string to array in objective C. i.e,I have a string, NSString
Possible Duplicate: Convert NSArray to NSString in Objective-C I have an array of an
I have used the Convert to Objective C ARC option in Xcode 4.3 to
I currently have an array, created from a database, an example of which looks
I have the following JSON string, which was an Objective C array then encoded
I have a real basic command-line program, in Objective-C, that searches for user inputed
How do you convert an unsigned char array to an NSData in objective c?
I have 2 NSArray (Mutable, actually) that I am trying to convert to a
I have an Objective-C view controller class, from which I am trying to call

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.