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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:27:58+00:00 2026-05-28T01:27:58+00:00

This isn’t a complicated problem, but I can’t for whatever reason think of a

  • 0

This isn’t a complicated problem, but I can’t for whatever reason think of a simple way to do this with the modulus operator. Basically I have a collection of N items and I want to display them in a grid.

I can display a maximum of 3 entries across and infinite vertically; they are not fixed width…So If I have 2 items they get displayed like that [1][2]. If I have 4 items they get displayed stacked like this:

[1][2]
[3][4]

If I have 5 items it should look like this:

[ 1 ][ 2]
[3][4][5]

Seven items is slightly more complicated:

[ 1 ][ 2]
[ 3 ][ 4]
[5][6][7]

This is one of those things where if I slept on it, it would be brain dead obvious in the morning, but all I can think about doing involves complicated loops and state variables. There has to be an easier way.

I’m doing this in C# but I doubt the language matters.

  • 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-28T01:27:58+00:00Added an answer on May 28, 2026 at 1:27 am

    By maximizing the number of rows that have three items, you can minimize the total number of rows. Thus six items would be grouped as two rows of 3 rather than three rows of 2:

    [1][2][3]
    [4][5][6]
    

    and ten items would be grouped as two rows of 2 and two rows of 3 rather than five rows of 2:

    [ 1 ][ 2 ]
    [ 3 ][ 4 ]
    [5][6][7 ]
    [8][9][10]
    

    If you want rows with two items first, then you keep peeling off two items until the remaining items are divisible by 3. As you go through the loop, you need to keep track of the number of remaining items using an index or whatnot.

    In your loop to populate each row, you can check these conditions:

    //logic within loop iteration
    if (remaining % 3 == 0) //take remaining in threes; break the loop
    else if (remaining >= 4) //take two items, leaving two or more remaining
    else //take remaining items, which will be two or three; break the loop
    

    If we walk through the example of 10 items, the process would go as follows:

    1. 10 items remaining. 10 % 3 != 0. Since 10 > 4, take two items.
    2. 8 items remaining. 8 % 3 != 0. Since 8 > 4, take two items.
    3. 6 items remaining. 6 % 3 = 0. Take those 6 items in groups of three.

    To go to your example of 7 items:

    1. 7 items remaining. 7 % 3 != 0. Since 7 > 4, take two items.
    2. 5 items remaining. 5 % 3 != 0. Since 5 > 4, take two items.
    3. 3 items remaining. 3 % 3 = 0. Take those 3 items as a group.

    And here’s the result for 4 items:

    1. 4 items remaining. 4 % 3 != 0. Since remaining = 4, take two items.
    2. 2 items remaining. 2 % 3 != 0. 2 < 4. Fall to else condition, take remaining items.

    I think that’ll work. At least, at 12:30 a.m. it seems like it should work.

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

Sidebar

Related Questions

This isn't that complicated of a question, but I can't wrap my head around
This isn't a code question for once, but it definitely has me confused. Basically,
This isn't that big of a deal but I was wondering if it can
This isn't a javascript question but I've created a fiddle so I can demonstrate
This isn't a problem as such, but it's bugging me and I would appreciate
This isn't a very simple question, but hopefully someone has run across it. I
This isn't about a specific problem I'm having, but I have to clear something
This isn't a huge problem, but ideally i'd like to sort it out if
This isn't a direct question, but more a question on how I can improve
This isn't strictly a programming question, but I'm asking it here because it's certainly

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.