- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ code to find score of winning square on a square board

Suppose we have a square board of order n x n. Amal and Bimal are playing a game. During the game they write numbers on the board's squares by some unknown rules. Currently the board is showing elements after ending the game. To understand who has won, we need to count the number of winning squares. A particular square is winning we should do the following. Find the sum of all numbers on the squares that share this column and separately calculate the sum of all numbers on the squares that share this row. A square is a winning square if the sum of the column numbers is strictly greater than the sum of the row numbers.

So, if the input is like

5 | 7 | 8 | 4 |

9 | 5 | 3 | 2 |

1 | 6 | 6 | 4 |

9 | 5 | 7 | 3 |

Then the output will be 6, because

5 | 7 | 8 | 4 |

9 | 5 | 3 | 2 |

1 | 6 | 6 | 4 |

9 | 5 | 7 | 3 |

## Steps

To solve this, we will follow these steps −

t := 0 n := size of M for initialize i := 0, when i <= n - 1, update (increase i by 1), do: for initialize j := 0, when j <= n - 1, update (increase j by 1), do: s := 0 l := 0 for initialize k := 0, when k <= n - 1, update (increase k by 1), do: s := s + M[i, k] l := l + M[k, j] if l > s, then: (increase t by 1) return t

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(vector<vector<int>> M){ int t = 0; int n = M.size(); for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= n - 1; j++){ int s = 0; int l = 0; for (int k = 0; k <= n - 1; k++){ s += M[i][k]; l += M[k][j]; } if (l > s) t++; } return t; } int main(){ vector<vector<int>> matrix = { { 5, 7, 8, 4 }, { 9, 5, 3, 2 }, { 1, 6, 6, 4 }, { 9, 5, 7, 3 } }; cout << solve(matrix) << endl; }

## Input

{ { 5, 7, 8, 4 }, { 9, 5, 3, 2 }, { 1, 6, 6, 4 }, { 9, 5, 7, 3 } }

## Output

6

- Related Questions & Answers
- 8086 program to find the square root of a perfect square root number
- Java Program to Find Area of Square
- Java program to find the area of a square
- 8085 program to find square root of a number
- 8086 program to find Square Root of a number
- Program to print Square inside a Square in C
- Square and Square root in Arduino
- 8085 program to find square of a 8 bit number
- Difference between sum of square and square of sum in JavaScript
- Magic Square
- Babylonian method to find the square root
- Java program to find the square root of a given number
- C++ code to find rank of student from score table
- Area of a leaf inside a square?
- How to find Square root of complex numbers in Python?