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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:36:38+00:00 2026-06-12T10:36:38+00:00

In this great book , we’re asked to design an algorithm to figure out

  • 0

In this great book, we’re asked to design an algorithm to figure out if someone has won in a game of tic-tac-toe.
The following solution is given, after which the author said:

Note that the runtime could be reduced to O(N) with the addition of
row and column count arrays (and two sums for the diagonals)

I tried hard, but I couldn’t figure out the meaning of that remark. How are these arrays and sums added ? Thanks !

enum Piece { Empty, Red, Blue };
enum Check { Row, Column, Diagonal, ReverseDiagonal }

Piece getIthColor(Piece[][] board, int index, int var, Check check) {
  if (check == Check.Row) return board[index][var];
  else if (check == Check.Column) return board[var][index];
  else if (check == Check.Diagonal) return board[var][var];
  else if (check == Check.ReverseDiagonal)      
    return board[board.length - 1 - var][var];      

  return Piece.Empty;
}

Piece getWinner(Piece[][] board, int fixed_index, Check check) {    
  Piece color = getIthColor(board, fixed_index, 0, check);
  if (color == Piece.Empty) return Piece.Empty;
  for (int var = 1; var < board.length; var++) {
    if (color != getIthColor(board, fixed_index, var, check)) {
      return Piece.Empty;
    }
  } 
  return color;
}

Piece hasWon(Piece[][] board) {
  int N = board.length;
  Piece winner = Piece.Empty;

  // Check rows and columns
  for (int i = 0; i < N; i++) {

    winner = getWinner(board, i, Check.Row);
    if (winner != Piece.Empty) {
      return winner;
    }       

    winner = getWinner(board, i, Check.Column);     
    if (winner != Piece.Empty) {
      return winner;
    }

  }     

  winner = getWinner(board, -1, Check.Diagonal);
  if (winner != Piece.Empty) {
    return winner;
  }     

  // Check diagonal     
  winner = getWinner(board, -1, Check.ReverseDiagonal);
  if (winner != Piece.Empty) {
    return winner;
  } 

  return Piece.Empty;
}
  • 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-12T10:36:39+00:00Added an answer on June 12, 2026 at 10:36 am

    Each time a new piece is placed in the board, you increase the appropriate row, column and (possibly ) diagonal counters by 1. You either have separate counters for the players, or use +1/-1.

    Now when you check the board, you only have to check whether any of this counters equals N, which can be done in O(N) (2N+2 counters).

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

Sidebar

Related Questions

I was reading up on singleton class design in C# on this great resource
I'm trying to learn java from bottom up, and I got this great book
We all know the great book about design patterns known as Gang of Four,
I am a bit confused here. This is from a great C book, and
So I found this great game of pong created by this guy called Ferrari,
So, I'm learning Objective C by this book. It is thought to be great
I've been reading Diaz's book Pro JavaScript Design Patterns. Great book. I myself am
I am reading a great book called Swing: A Beginner's guide. There is this
I found this great example of horizontal parallax http://www.perandersen.no/sandbox/parallax/ The only modification I would
I found this great class, all works well except... I spcify the output directory

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.