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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:04:23+00:00 2026-05-20T15:04:23+00:00

This is a trivial algorithmic question, I believe, but I don’t seem to be

  • 0

This is a trivial algorithmic question, I believe, but I don’t seem to be able to find an efficient and elegant solution.

We have 3 arrays of int (Aa, Ab, Ac) and 3 cursors (Ca, Cb, Cc) that indicate an index in the corresponding array. I want to identify and increment the cursor pointing to the smallest value. If this cursor is already at the end of the array, I will exclude it and increment the cursor pointing to the second smallest value. If there is only 1 cursor that is not at the end of the array, we increment this one.

The only solutions I can come up are complicated and/or not optimal. For example, I always end up with a huge if…else…

Does anyone see a neat solution to this problem ?

I am programming in C++ but feel free to discuss it in pseudo-code or any language you like.

Thank you

  • 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-20T15:04:23+00:00Added an answer on May 20, 2026 at 3:04 pm

    Pseudo-java code:

    int[] values = new int[3];
    values[0] = aa[ca];
    values[1] = ab[cb];
    values[2] = ac[cc];
    Arrays.sort(values);
    
    boolean done = false;
    for (int i = 0; i < 3 && !done; i++) {
        if (values[i] == aa[ca] && ca + 1 < aa.length) {
            ca++;
            done = true;
        }
        else if (values[i] == ab[cb] && cb + 1 < ab.length) {
            cb++;
            done = true;
        }
        else if (cc + 1 < ac.length) {
            cc++;
            done = true;
        }
    }
    if (!done) {
        System.out.println("cannot increment any index");
        stop = true;
    }
    

    Essentially, it does the following:

    1. initialize an array values with aa[ca], ab[cb] and ac[cc]

    2. sort values

    3. scan values and increment if possible (i.e. not already at the end of the array) the index of the corresponding value

    I know, sorting is at best O(n lg n), but I’m only sorting an array of 3 elements.

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

Sidebar

Related Questions

I think this is trivial, but I cannot find the answer :( I have
I know this seams a trivial question, but how can I disable the annoying
This seems like such a trivial problem, but I can't seem to pin how
This seems trivial, but I've never had to worry about it before and my
This is pretty trivial, but I noticed on SO that instead of an offset
Wondering if anyone can help me with this annoying but trivial (in terms of
Question regarding how to setup dbase relationships (newbie, this may be trivial) Followed the
This is a fairly trivial matter, but I'm curious to hear people's opinions on
I thought this was trivial but couldn't solve it out :( I want to
This seems like a noob question, but the simple answer is eluding me. I

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.