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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:10:23+00:00 2026-06-05T08:10:23+00:00

I have a method that receives a number n as parameter and calculate all

  • 0

I have a method that receives a number n as parameter and calculate all possible combinition containing exactly n 1 in a bit(0,1) array, the result returned is the positions of the number 1

for example suppose that our bit array has 3 elements and

 for n = 1
   the function gives : [0] - [1] - [2] (three possible positions)
 for n=2
   the result will be [0,1] -  [0,2] - [1,2] (three possible positions)

and for n=3
the result will be [0,1,2] (one possible position)

the function work correctly for an array with 3 elements, but gives wrong results for 4 elements

  n = 2 result : [0,1] - [0,2] - [0,3] -[1,2] - [1,3] - [1,2,3]

Can any one explain why it gives unexpected results for arraysize >= 4

Thanks in advance

    const int arraySize = 4;

    static private void Positions(int n, int start, ArrayList prepend, ArrayList results)
    {
        ArrayList tmp = new ArrayList(prepend);
        int end = arraySize - n;
        for (int i = start; i <= end; i++)
        {
            if (end < arraySize - 1)
            {
                prepend.Add(i);
                Positions(n - 1, i + 1, prepend, results);                    
                prepend = tmp;
            }
            else
                results.Add(new ArrayList(prepend) { i });
        }
    }

this is how i use the method

    static void Main(string[] args)
    {
        ArrayList results = new ArrayList();            
        Positions(2, 0, new ArrayList(), results);

        foreach (ArrayList array in results)
        {
            foreach (var elem in array)
                Console.Write(elem);
            Console.WriteLine();
        }

        Console.Read();
    }
  • 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-05T08:10:25+00:00Added an answer on June 5, 2026 at 8:10 am

    I think the issue is in this line:

    prepend = tmp;
    

    You are intending I think to restore the state of the prepend array to the original at the beginning of the method. However, you are setting a direct reference, so with every loop iteration you are modifying the original prepend array. If you copy the array here as well every time, it seems to work correctly:

    prepend = new ArrayList(tmp);
    

    All this copying of arrays, however, is not very efficient. You can try removing the entry you just added as well as another alternative:

    prepend.Add(i);
    Positions(n - 1, i + 1, prepend);
    prepend.Remove(i);
    

    Then technically you don’t even need the tmp copy any more.

    Edit: Also, for your if statement, I think you want something like

    if (n > 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this method that receives an ID number and downloads an HTML website
I have a method that receives a number in a NSString format. I wish
I have a method that receives something and it needs to determine the type
I have a method that accepts a generic delegate as a parameter, and inserts
I'm writing a method that receives a number l and returns a vector of
I have created a method called isInPoly that receives the x and y coordinates
I have a method that will receive a string , but before I can
I have method that returns Drawable , and if its Bitmap object is recycled
I have method that returned NSManagedObject and I don't know what kind of NSManagedObject
I have method that returns module path of given class name def findModulePath(path, className):

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.