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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:08:14+00:00 2026-06-09T00:08:14+00:00

I encountred this function without any comment. I wonder what is this function doing?

  • 0

I encountred this function without any comment. I wonder what is this function doing? Any help?

int flr(int n, char a[])
{
    #define A(i) a[((i) + k) % n]
    int l[n], ls = n, z[n], min = 0;

    for (int i = 0; i < n; i++)
    {
        l[i] = i;
        z[i] = 1;
    }

    for (int k = 0; ls >= 2; k++)
    {
        min = l[0];
        for (int i=0; i<ls; i++) min = A(l[i])<A(min) ? l[i] : min;
        for (int i=0; i<ls; i++) z[A(l[i])!=A(min) ? l[i] : (l[i]+k+1)%n] = 0;
        for (int ls_=ls, i=ls=0; i<ls_; i++) if (z[l[i]]) l[ls++] = l[i];
    }

    return ls == 1 ? l[0] : min;
}
  • 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-09T00:08:17+00:00Added an answer on June 9, 2026 at 12:08 am

    What a fun problem!

    Other posters are correct that it returns the index of a minimum, but it’s actually more interesting than that.

    If you treat the array as being circular (i.e. when you get past the end, go back to the beginning), the function returns the starting index of the minimum lexicographic subsequence.

    If only one element is minimal, that element is returned. If multiple elements are minimal, we compare the next element along from each minimal element.

    E.g. with an input of 10 and {0, 1, 2, 1, 1, 1, 0, 0, 1, 0}:

    • There are four minimal elements of 0, at indices 0, 6, 7 and 9
    • Of these two are followed by a 1 (the 0 and 7 elements), and two are followed by a 0 (the 6 and 9 elements). Remember that the array is circular.
    • 0 is smaller than 1, so we only consider the 0s at 6 and 9.
    • Of these the sequence of 3 elements starting at 6 is ‘001’ and the sequence from 9 is also ‘001’, so they’re still both equally minimal
    • Looking at the sequence of 4 elements, we have ‘0010’ from element 6 onwards and ‘0012’ from element 9 onwards. The sequence from 6 onwards is therefore smaller and 6 is returned. (I’ve checked that this is the case).

    Refactored and commented code follows:

    int findStartOfMinimumSubsequence(int length, char circular_array[])
    {
        #define AccessWithOffset(index) circular_array[(index + offset) % length]
        int indicesStillConsidered[length], count_left = length, indicator[length], minIndex = 0;
    
        for (int index = 0; index < length; index++)
        {
            indicesStillConsidered[index] = index;
            indicator[index] = 1;
        }
    
        // Keep increasing the offset between pairs of minima, until we have eliminated all of
        // them or only have one left.
        for (int offset = 0; count_left >= 2; offset++)
        {
            // Find the index of the minimal value for the next term in the sequence,
            // starting at each of the starting indicesStillConsidered
            minIndex = indicesStillConsidered[0];
            for (int i=0; i<count_left; i++) 
                minIndex = AccessWithOffset(indicesStillConsidered[i])<AccessWithOffset(minIndex) ? 
                    indicesStillConsidered[i] : 
                    minIndex;
    
            // Ensure that indicator is 0 for indices that have a non-minimal next in sequence
            // For minimal indicesStillConsidered[i], we make indicator 0 1+offset away from the index.
            // This prevents a subsequence of the current sequence being considered, which is just an efficiency saving.
            for (int i=0; i<count_left; i++){
                offsetIndexToSet = AccessWithOffset(indicesStillConsidered[i])!=AccessWithOffset(minIndex) ? 
                    indicesStillConsidered[i] : 
                    (indicesStillConsidered[i]+offset+1)%length;
                indicator[offsetIndexToSet] = 0;
            }
    
            // Copy the indices where indicator is true down to the start of the l array.
            // Indicator being true means the index is a minimum and hasn't yet been eliminated.
            for (int count_before=count_left, i=count_left=0; i<count_before; i++) 
                if (indicator[indicesStillConsidered[i]]) 
                    indicesStillConsidered[count_left++] = indicesStillConsidered[i];
        }
    
        return count_left == 1 ? indicesStillConsidered[0] : minIndex;
    }
    

    Sample uses

    Hard to say, really. Contrived example: from a circular list of letters, this would return the index of the shortest subsequence that appears earlier in a dictionary than any other subsequence of the same length (assuming all the letters are lower case).

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

Sidebar

Related Questions

I've spent a great long while googling this problem without any luck and I've
I have encounters this abap snippet that I have difficulty to comprehend. call function
I'am very new to google-apps-script. I encounter this error TypeError: Cannot find function getValue
so i have this script: <script type=text/javascript> $(window).load(function () { var imagePath = new
I saw this code somewhere without the PHP backend for it and have wondering
I encountered this error: Cannot implicitly convert type System.Web.UI.WebControls.LoginView to Login The error occured
I encountered this error: The type or namespace name 'WebControls' could not be found
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
Today I encountered this article about decimal expansion and I was instantaneously inspired to
I've encountered this issue on two different machines and can't figure out what the

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.