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

The Archive Base Latest Questions

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

Consider the following scenario . I have an array of numbers: [ 1,2,3,4 ]

  • 0

Consider the following scenario.

I have an array of numbers:

 [ 1,2,3,4 ]

If this array was joined i would have the number 1234.

I want to swap the numbers around to achieve the clossest higher number.

1234 will become 1243, which will become 1324, which will become 1342 and so forth..

What algorithm would i need use to make this changes in an array?

Ideally i would like to use the algorithm in this manner:
( lets say Array has this algorithm as a function called walkthrough )

 [ 1,2,3,4].walkthrough() # gives [ 1, 2, 4, 3 ]
 [ 1,2,4,3].walkthrough() # gives [ 1, 3, 2, 4 ]

the list of numbers continues:

1234
1243
1324
1342
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241

  • 1 1 Answer
  • 2 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-12T10:40:28+00:00Added an answer on May 12, 2026 at 10:40 am

    This gives you next permutation:

    bool Increase(int[] values) {
       // locate the last item which is smaller than the following item
       int pos = values.Length - 2;
       while (pos >= 0 && values[pos] > values[pos + 1]) pos--;
       // if not found we are done
       if (pos == -1) return false;
       // locate the item next higher in value
       int pos2 = values.Length - 1;
       while (values[pos2] < values[pos]) pos2--;
       // put the higher value in that position
       int temp = values[pos];
       values[pos] = values[pos2];
       values[pos2] = temp;
       // reverse the values to the right
       Array.Reverse(values, pos + 1, values.Length - pos - 1);
       return true;
    }
    

    Edit:
    Changed Array.Sort to Array.Reverse. The items are always in descending order and should be in ascending order, so they give the same result.

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

Sidebar

Related Questions

This might be a novice question. :). Consider the following scenario. Suppose we have
consider following scenario input string = WIPR.NS i have to replace this with WIPR2.NS
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following scenario We have a simple database that involves two entities: user
From couple of days i am thinking of a following scenario Consider I have
I have a question regarding a race condition scenario. The question: Consider the following
Consider this scenario: I've an XML file called person.xml with the following data in
Consider the following scenario. We have a C++ function with a static local variable:
Let's consider the following scenario. I have the following page where all rendered elements
consider the following scenario: I have a storyboard-based app. I add a ViewController object

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.