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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:16:46+00:00 2026-05-26T22:16:46+00:00

is there a good algorithm for checking whether there are 5 same elements in

  • 0

is there a good algorithm for checking whether there are 5 same elements in a row or a column or diagonally given a square matrix, say 6×6?

there is ofcourse the naive algorithm of iterating through every spot and then for each point in the matrix, iterate through that row, col and then the diagonal. I am wondering if there is a better way of doing it.

  • 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-26T22:16:47+00:00Added an answer on May 26, 2026 at 10:16 pm

    You can check whether there are k same elements in a matrix of integers in a single pass.

    Suppose that n is the size of the matrix and m is the largest element. We have n column, n row and 1 diagonal.
    Foreach column, row or diagonal we have at most n distinct element.

    Now we can create a histogram containing (n + n + 1) * (2 * m + 1) element. Representing
    the rows, columns and the diagonal each of them containing at most n distinct element.

    size = (n + n + 1) * (2 * m + 1)
    histogram = zeros(size, Int)
    

    Now the tricky part is how to update this histogram ?

    Consider this function in pseudo-code:

    updateHistogram(i, j, element)
    
        if (element < 0)
            element = m - element;
    
        rowIndex        = i * m + element
        columnIndex     = n * m + j * m + element
        diagonalIndex   = 2 * n * m + element
    
        histogram[rowIndex] = histogram[rowIndex] + 1
        histogram[columnIndex] = histogram[columnIndex] + 1
    
        if (i = j)
            histogram[diagonalIndex] = histogram[diagonalIndex] + 1
    

    Now all you have to do is to iterate throw the histogram and check whether there is an element > k

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

Sidebar

Related Questions

Is there a good algorithm to calculate the cartesian product of three seq s
I'm looking for a good algorithm to get all the elements in one array
Is there a good algorithm to split a randomly generated number into three buckets,
I am developing an android messaging application. Is there a good spam filtering algorithm
Are there any good resources for the layout of elements in a form? Possibly
Hi I would like to know if there is a good algorithm to the
my question is: is there a good (common) algorithm to create numbers, which match
I want to write a program that processes images. If there are good algorithm
Is there a good algorithm for this? after an amount of searching around I
Is there good implementation of Hungarian algorithm in standard python libraries?

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.