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

  • Home
  • SEARCH
  • 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 8220625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:35:28+00:00 2026-06-07T13:35:28+00:00

Given N > 0 and M > 0 , I want to enumerate all

  • 0

Given N > 0 and M > 0, I want to enumerate all (x, y) pairs such that 1 <= x <= N and 1 <= y <= M in descending order of (x * y).
An example: given N = 3 and M = 2, the enumeration sequence should be:

1. (3, 2) -- 3 * 2 = 6
2. (2, 2) -- 2 * 2 = 4
3. (3, 1) -- 3 * 1 = 3
4. (2, 1) -- 2 * 1 = 2
5. (1, 2) -- 1 * 2 = 2
6. (1, 1) -- 1 * 1 = 1

The order of (2, 1) and (1, 2) could be swapped. One obvious way is to list them all, insert into a vector<pair<int, int> >, and call std::sort() with my own comparison function. However, since N and M may be large, and most of the time I only need the first few terms of the sequence, I hope there could be some smarter way that generates such a sequence instead of generate them all-and-sort, which requires as many as N*M array elements.

Update: I forgot to mention that although most of the time I only need the first few terms, the number of terms required is unknown before enumeration.

  • 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-07T13:35:32+00:00Added an answer on June 7, 2026 at 1:35 pm

    If you’re just looking to save on space while retaining the time as more or less equal, you can count on the fact that each successively smaller element must be adjacent (in the 2-D grid you alluded to) to one of the elements you’ve already encountered. (You can prove this with induction, it’s not particularly difficult. I’m going to assume for the rest of this that M>=N.)

    The basic algorithm looks something like:

    Start with a list (Enumerated Points) containing just the maximum element, M*N
    Create a max heap (Candidates) containing (M-1),N and M,(N-1).
    Repeat this:
        1.Pick the largest value (x,y) in Candidates and append it to Enumerated Points
        2.Add (x-1),y and x,(y-1) to Candidates if they are not there already
    

    You can repeat this as long as you want more elements in Enumerated Points. The max size of Candidates should be M+N so I think this is O(k log(M+N)) where k is the number of points you want.

    ADDENDUM:
    The matter of avoiding duplicate is not entirely difficult but is worth mentioning. I will assume in this algo that you lay your grid out so that numbers go down as you move down and right. Anyway, it goes like this:

    At the beginning of the algorithm create an array (Column Size) which has one element for each column. You should make this array contain the number of rows in each column which are part of the list of enumerated points.

    After you add a new element and update this array, you will check the size of the column on either side in order to decide if the grid squares to the immediate right and below of this new enumerated point are already in the candidates list.

    Check the size of the column to the left- if it’s larger than this one, you don’t need to add the element below your new enumerated point.

    Check the size of the column to the right- if it’s one less than the same size of this column, you don’t need to update the element to the right of this one.

    To make this obvious, let’s look at this partially completed chart for M=4, N=2:

    4  3  2  1
    *  *  *  2 |2
    *  3  2  1 |1
    

    The elements (4,2), (3,2), (2,2) and (4,1) are already in the list. (The first coordinate is M, the second is N.) The Column Size array is [2 1 1 0] since this is the number of items in each column that are in the Enumerated Points list. We are about to add (3,1) to the new list- We can look to the column size to the right and conclude that adding (2,1) isn’t needed because the size of the column for M=2 is larger than 1-1. The reasoning is pretty clear visually – we already added (2,1) when we added (2,2).

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

Sidebar

Related Questions

I want to enumerate all the loaded segments of a module given a module
I want to list/enumerate (at runtime) in my Java program all classes that implements
Given: alphabet = ['a','b','c',...,'z'] i want python to enumerate every combination (starting from 1
Given n enumerables of the same type that return distinct elements in ascending order,
Possible Duplicate: How to enumerate an enum? Suppose that i have an enumeration like
I want the given application (Windows Service) to act as a remoting server as
For any given type i want to know its default value. In C#, there
I want to insert given text to existing pdf at X,Y location provided. I
I want to draw an area from given points on a map using the
Given this string random_string= '4' i want to determine if its an integer, a

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.