
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- 8086 program to find the square root of a perfect square root number
- How to find square of a number?
- A square board has an area of $8\frac{73}{144}\ m^2$. How long is each side of the board?
- In the figure, a square dart board is shown. The length of a side of the larger square is 1.5 times the length of a side of the smaller square. If a dart is thrown and lands on the larger square. What is the probability that it will land in the interior of the smaller square?"\n
- C++ code to find rank of student from score table
- In the following APs, find the missing terms in the boxes:(i) $2, \square, 26$(ii) $\square, 13, \square, 3$(iii) $5, \square, \square, 9\frac{1}{2}$(iv) $-4, \square, \square, \square, \square, 6$(v) $\square, 38, \square, \square, \square, -22$
- The area of a square park is $64.516$ square metres. Find its side.
- In the following APs, find the missing terms in the boxes:$-4, \square, \square, \square, \square, 6$
- In the following APs, find the missing terms in the boxes:$\square, 38, \square, \square, \square, -22$
- Java program to find the area of a square
- 8086 program to find Square Root of a number
- 8085 program to find square root of a number
- Haskell Program to Find the Area of a Square
- Java Program to Find Area of Square
- Swift Program to Find Area of Square
