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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:34:05+00:00 2026-06-06T09:34:05+00:00

Consider a 2000 x 2000 2D bool array. 100,000 elements are set to true,

  • 0

Consider a 2000 x 2000 2D bool array. 100,000 elements are set to true, the rest to false.

Given a cell (x1,y1) we need to find the nearest cell (x2,y2) (by manhattan distance: abs(x1-x2) + abs(y1-y2)) that is false.

One way to do that would be to:

for (int dist = 0; true; dist++)
    for ((x2,y2) in all cells dist away from (x1,y1))
        if (!array[x2,y2])
            return (x2,y2);

In the worst case we would have to iterate through 100,000 cells before finding the free one.

Is there a data structure we could use rather than a 2D array that would allow us to perform this search quicker?

  • 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-06T09:34:07+00:00Added an answer on June 6, 2026 at 9:34 am

    If the data is constant and you have many queries on it:

    You might want to use a k-d tree, and look for the nearest neighbor. Insert (i,j) for each element such that arr[i][j] = false. The standard k-d tree uses euclidean distance but I think one can modify it to use manhattan distances instead..

    If the data is used for one query:

    You will need at least Omega(n*m) ops to read the data and insert it into any data structure – so no point in doing that – the suggested solution will outperform only the build up of any data structure.

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

Sidebar

Related Questions

please consider this table: PK_Id Number Year Month Value ------------------------------------------------------------------------- 1 1 2000 5
Consider this simple SVG file : <svg xmlns=http://www.w3.org/2000/svg xmlns:x=http://www.w3.org/1999/xlink viewBox=0 0 353 150> <defs>
What do I need to consider before I switch a bunch of fields from
Consider the following set up: - SomeFilter extends Filter - Filter implements Runnable -
Consider this in C#: int[] a = new int[1024]; All integers will be set
Consider the following data set in a book table (group_id, title, check_out_date): > 1
I support SQL Servers 2000 thru 2008R2 (all Standard Edition). Question 1 Please consider
Consider that we are given an isometric grid (consider something like Diablo) of tiles.
Consider the following Python snippet: for ix in [0.02, 0.2, 2, 20, 200, 2000]:
Consider that I have a complex class structure where many elements inherit from other

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.