Check if each row and column of N*N Grid contains all numbers from 1 to N
Last Updated :
11 Feb, 2022
Given a square matrix arr[][] of size N * N, the task is to check whether each row and column of a matrix contains all the numbers from 1 to N or not.
Examples:
Input: arr[][] = { {1, 2, 3},
{3, 1, 2},
{2, 3, 1} }
Output: true
Explanation: Every row and column contains number 1 to N, i.e 1 to 3
Input: arr[][] = { {1, 1, 1},
{1, 2, 3},
{1, 2, 3} }
Output: false
Approach: The task can be solved using a set data structure (set stores unique elements). Iterate over the matrix, store the elements of each row and each column inside a set, and check whether the size of the set is equal to N or not.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int check(vector<vector< int > >& arr)
{
int N = arr.size();
set< int > row, col;
int flag = 1;
for ( int i = 0; i < N; i++) {
row.clear();
col.clear();
for ( int j = 0; j < N; j++) {
col.insert(arr[j][i]);
row.insert(arr[i][j]);
}
if (col.size() != N
|| row.size() != N)
flag = 0;
}
return flag;
}
int main()
{
int N = 3;
vector<vector< int > > arr{ { 1, 2, 3 },
{ 3, 1, 2 },
{ 2, 3, 1 } };
cout << (!check(arr) ? "false" : "true" );
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int check( int [][] arr)
{
int N = arr.length;
Set<Integer> row = new HashSet<Integer>();
Set<Integer> col = new HashSet<Integer>();
int flag = 1 ;
for ( int i = 0 ; i < N; i++) {
row.clear();
col.clear();
for ( int j = 0 ; j < N; j++) {
col.add(arr[j][i]);
row.add(arr[i][j]);
}
if (col.size() != N
|| row.size() != N)
flag = 0 ;
}
return flag;
}
public static void main (String[] args) {
int N = 3 ;
int [][] arr = { { 1 , 2 , 3 },
{ 3 , 1 , 2 },
{ 2 , 3 , 1 } };
if (check(arr) == 1 ){
System.out.println( "true" );
}
else {
System.out.println( "false" );
}
}
}
|
Python3
def check (arr):
N = len (arr)
row = set ()
col = set ();
flag = 1 ;
for i in range (N):
row = set ()
col = set ();
for j in range (N):
col.add(arr[j][i]);
row.add(arr[i][j]);
if ( len (col) ! = N or len (row) ! = N):
flag = 0 ;
return flag;
N = 3 ;
arr = [[ 1 , 2 , 3 ], [ 3 , 1 , 2 ], [ 2 , 3 , 1 ]];
print ( "false" ) if not check(arr) else print ( "true" );
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static int check( int [,] arr)
{
int N = 3;
HashSet< int > row = new HashSet< int >();
HashSet< int > col = new HashSet< int >();
int flag = 1;
for ( int i = 0; i < N; i++) {
row.Clear();
col.Clear();
for ( int j = 0; j < N; j++) {
col.Add(arr[j,i]);
row.Add(arr[i,j]);
}
if (col.Count != N
|| row.Count != N)
flag = 0;
}
return flag;
}
static public void Main (){
int [,] arr = { { 1, 2, 3 },
{ 3, 1, 2 },
{ 2, 3, 1 } };
if (check(arr) == 1){
Console.WriteLine( "true" );
}
else {
Console.WriteLine( "false" );
}
}
}
|
Javascript
<script>
const check = (arr) => {
let N = arr.length;
let row = new Set(), col = new Set();
let flag = 1;
for (let i = 0; i < N; i++) {
row.clear();
col.clear();
for (let j = 0; j < N; j++) {
col.add(arr[j][i]);
row.add(arr[i][j]);
}
if (col.size != N
|| row.size != N)
flag = 0;
}
return flag;
}
let N = 3;
let arr = [[1, 2, 3], [3, 1, 2], [2, 3, 1]];
(!check(arr) ? document.write( "false" ) : document.write( "true" ));
</script>
|
Time Complexity: O(N2 * logN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...