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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:01:28+00:00 2026-05-26T05:01:28+00:00

Lets say we have a grid containing cells, and that we want to return

  • 0

Lets say we have a grid containing cells, and that we want to return two arrays where each contains all the cells in the diagonal with that cell, up-left to bottom-right and the opposite one. What would be the best way to go about it?

I tried the following in javascript (note that board is a one dimensional array that supposedly represents a square grid with a side length of boardSize. position is the cell which I am trying to find its diagonals.)

    var diagonal1 = [];
    var diagonal2 = [];
    for(var i = 0; i < board.length; i++) { 
        if (i == position) diagonal.move_index = diagonal.length
        if (Math.abs(position - i) % (boardSize + 1) == 0) {
            diagonal1.push(board[i]);
        }
        else if (Math.abs(position - i) % (boardSize - 1) == 0) { 
            diagonal2.push(board[i]);
        }
    }

but this is only doing the job for elements that lie in the main diagonal, not others. Any ideas?

Example:

if board = [1,2,3,4,5,6,7,8]
board:
        1 2 3 
        4 5 6 
        7 8 9 
then If I say that I want to find the diagonals for position = 4 
I should get:
diagonal1 = [4, 8] 
diagonal2 = [2, 4] 

and if I choose another position, lets say position = 5, then:
diagonal1 = [1, 3, 5] 
diagonal2 = [3, 5, 7]
  • 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-26T05:01:29+00:00Added an answer on May 26, 2026 at 5:01 am

    Does the order of the cells in the diagonals matter? If not, the algorithm in this pseudocode should suffice. If it does matter, you just need to reorder some chunks and play around with the loop variables…

    getDiagonals(x, y) {
    
        Set<Cell> diagonal1;
        Set<Cell> diagonal2;
    
        diagonal1.add(board[x][y]); //manually add center cell to both diagonals
        diagonal2.add(board[x][y]);
    
        //Sweep forwards, adding higher cells to diag1 and lower cells to diag2
        for(int i=1; x+i < board.size(); i++) { //board.size() for 'horizontal' size
            if(y-i >= 0) diagonal1.add(board[x+i][y-i]);
            if(y+i < board[0].size()) diagonal2.add(board[x+i][y+i]); //board[0].size for 'vertical' size
        }
    
        //Sweep backwards, adding higher cells to diag2 and lower cells to diag1
        for(int i=1; x-i >= 0; i++) {
            if(y-i >= 0) diagonal2.add(board[x-i][y-i]);
            if(y+i < board[0].size()) diagonal1.add(board[x-i][y+i]);
        }
    
        return (diagonal1, diagonal2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have 2 columns in my data Grid: Column A: Selected, and
Let's say I have some grid that looks like this _ _ _ _
Lets say I have multiple DataGrids throughout my winform app and I want to
Lets say I have a drop down list and a grid view on a
Lets say you have a GridLayout of JButtons in an NxN grid, in code
Lets say I have an image or two dimensional pattern similar to QRcode and
BACKGROUND: I have a grid of 36 buttons, lets say a 6 rows &
I have template for my ComboBox . taken from here . Lets say each
Lets say i have 50 element grid. If its wide list it is easy
As a trivial example lets say I have the following grid and I am

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.