Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
Given a binary matrix arr[][] of dimensions N * M , the task is to count the number of right-angled triangles that can be formed by joining the cells containing the value 1 such that the triangles must have any two of its sides parallel to the sides of the rectangle.
Examples:
Input: arr[][] = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}
Output: 4
Explanation: Right-angled triangles that can be formed are (a[1][1], a[1][0], a[0][1]), (a[1][1], a[2][1], a[1][0]), (a[1][1], a[1][2], a[0][1]) and (a[1][1], a[1][2], a[2][1])
Input: arr[][] = {{1, 0, 1, 0}, {0, 1, 1, 1}, {1, 0, 1, 0}, {0, 1, 0, 1}}
Output: 16
Approach: The idea is to traverse the given grid and store the count of 1s present in each row and each column in auxiliary arrays row[] and col[] respectively. Then for each cell in the grid arr[][], if arr[i][j] is 1, then the total number of right-angled triangles formed can be calculated by (row[i] – 1)*(col[j] – 1) for each cell. Follow the steps below to solve the problem:
- Initialize the arrays col[] and row[] with 0.
- Traverse the given grid and visit every cell arr[i][j].
- If arr[i][j] is 1, increment row[i] and col[j] by 1.
- After traversing the grid, initialize a variable ans with 0.
- Again traverse the whole grid and now, if arr[i][j] is 1 then update the count of the right-angled triangle by:
(row[i] – 1)*(col[j] – 1)
- After traversing, print the value of ans as the total number of right-angled triangles.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfTriangle(
vector<vector< int > >& a)
{
int N = a.size();
int M = a[0].size();
int rows[N] = { 0 };
int columns[M] = { 0 };
for ( int i = 0; i < N; ++i) {
for ( int j = 0; j < M; ++j) {
if (a[i][j] == 1) {
rows[i]++;
}
}
}
for ( int i = 0; i < M; ++i) {
for ( int j = 0; j < N; ++j) {
if (a[j][i] == 1) {
columns[i]++;
}
}
}
int answer = 0;
for ( int i = 0; i < N; ++i) {
for ( int j = 0; j < M; ++j) {
if (a[i][j] == 1) {
answer += (rows[i] - 1)
* (columns[j] - 1);
}
}
}
return answer;
}
int main()
{
vector<vector< int > > arr;
arr = { { 1, 0, 1, 0 },
{ 0, 1, 1, 1 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 } };
cout << numberOfTriangle(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int numberOfTriangle( int [][] a)
{
int N = a.length;
int M = a[ 0 ].length;
int []rows = new int [N];
int []columns = new int [M];
for ( int i = 0 ; i < N; ++i)
{
for ( int j = 0 ; j < M; ++j)
{
if (a[i][j] == 1 )
{
rows[i]++;
}
}
}
for ( int i = 0 ; i < M; ++i)
{
for ( int j = 0 ; j < N; ++j)
{
if (a[j][i] == 1 )
{
columns[i]++;
}
}
}
int answer = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; ++j)
{
if (a[i][j] == 1 )
{
answer += (rows[i] - 1 ) *
(columns[j] - 1 );
}
}
}
return answer;
}
public static void main(String[] args)
{
int [][]arr = {{ 1 , 0 , 1 , 0 },
{ 0 , 1 , 1 , 1 },
{ 1 , 0 , 1 , 0 },
{ 0 , 1 , 0 , 1 }};
System.out.print(numberOfTriangle(arr));
}
}
|
Python3
def numberOfTriangle(a):
N = len (a)
M = len (a[ 0 ])
rows = [ 0 ] * N
columns = [ 0 ] * M
for i in range (N):
for j in range (M):
if (a[i][j] = = 1 ):
rows[i] + = 1
for i in range (M):
for j in range (N):
if (a[j][i] = = 1 ):
columns[i] + = 1
answer = 0
for i in range (N):
for j in range (M):
if (a[i][j] = = 1 ):
answer + = ((rows[i] - 1 ) *
(columns[j] - 1 ))
return answer
if __name__ = = '__main__' :
arr = [ [ 1 , 0 , 1 , 0 ],
[ 0 , 1 , 1 , 1 ],
[ 1 , 0 , 1 , 0 ],
[ 0 , 1 , 0 , 1 ] ]
print (numberOfTriangle(arr))
|
C#
using System;
class GFG{
static int numberOfTriangle( int [,] a)
{
int N = a.GetLength(0);
int M = a.GetLength(1);
int []rows = new int [N];
int []columns = new int [M];
for ( int i = 0; i < N; ++i)
{
for ( int j = 0; j < M; ++j)
{
if (a[i, j] == 1)
{
rows[i]++;
}
}
}
for ( int i = 0; i < M; ++i)
{
for ( int j = 0; j < N; ++j)
{
if (a[j, i] == 1)
{
columns[i]++;
}
}
}
int answer = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; ++j)
{
if (a[i, j] == 1)
{
answer += (rows[i] - 1) *
(columns[j] - 1);
}
}
}
return answer;
}
public static void Main(String[] args)
{
int [,]arr = { { 1, 0, 1, 0 },
{ 0, 1, 1, 1 },
{ 1, 0, 1, 0 },
{ 0, 1, 0, 1 } };
Console.Write(numberOfTriangle(arr));
}
}
|
Javascript
<script>
function numberOfTriangle(a)
{
var N = a.length;
var M = a[0].length;
var rows = Array.from({length: N}, (_, i) => 0);
var columns = Array.from({length: M}, (_, i) => 0);
for (i = 0; i < N; ++i)
{
for (j = 0; j < M; ++j)
{
if (a[i][j] == 1)
{
rows[i]++;
}
}
}
for (i = 0; i < M; ++i)
{
for (j = 0; j < N; ++j)
{
if (a[j][i] == 1)
{
columns[i]++;
}
}
}
var answer = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < M; ++j)
{
if (a[i][j] == 1)
{
answer += (rows[i] - 1) *
(columns[j] - 1);
}
}
}
return answer;
}
var arr = [[1, 0, 1, 0],
[0, 1, 1, 1],
[1, 0, 1, 0],
[0, 1, 0, 1]];
document.write(numberOfTriangle(arr));
</script>
|
Time Complexity: O(N*M) where NxM is the dimensions of the given grid.
Space Complexity: O(N*M)
Last Updated :
11 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...