Find the Submatrix which holds the given co-ordinate
Last Updated : 19 Sep, 2022
Given a matrix mat of N*N (N is a perfect square|) and two points x and y, the task is to return all the elements of the submatrix in which the element A[x][y] lies.
Note: The matrix is divided into N equal submatrix each of size K*K (where K is the square root of N)
Approach: The problem can be solved based on the following observation:
An element at index (x, y) in a square matrix of perfect square length, lies in submatrix[n*(x/n), (n*(y/n)], where each value shows the positioning with respect to other submatrices. So the idea is to just print that submatrix.
Follow the steps mentioned below to implement the idea:
Find square root on N.
Store the submatrix where the coordinate (x, y) lies in a new matrix.
Return the new array.
Below is the implementation of the above approach.
C++
// C++ code to implement the approach#include<bits/stdc++.h>usingnamespacestd;// Function to print submatrixvector<vector<int>>getSubGrid(intN,vector<vector<int>>&matrix,intx,inty){intn=sqrt(N);vector<vector<int>>subGrid(n,vector<int>(n));for(inti=0;i<n;i++){for(intj=0;j<n;j++){subGrid[i][j]=matrix[n*(x/n)+(j/n)+i][n*(y/n)+j%n];}}// Return the submatrixreturnsubGrid;}// Driver Codeintmain(){intN=9;vector<vector<int>>matrix={{1,2,3,9,8,7,1,2,1},{4,5,6,1,2,3,7,9,8},{7,8,9,2,2,0,1,5,7},{0,2,9,1,2,3,4,5,3},{9,8,7,4,5,6,7,4,1},{1,4,7,7,8,9,9,8,7},{5,6,1,9,8,7,5,2,3},{4,5,1,6,5,4,9,7,4},{8,7,9,3,2,1,9,4,9}};intx=4;inty=4;// Function callvector<vector<int>>subGrid=getSubGrid(N,matrix,x,y);for(inti=0;i<subGrid.size();i++){cout<<"[";intj=0;for(;j<subGrid.size()-1;j++){cout<<subGrid[i][j]<<", ";}cout<<subGrid[i][j]<<"] ";}return0;}// This code is contributed by Rohit Pradhan
Java
// Java code to implement the approachimportjava.io.*;importjava.util.*;classGFG{// Function to print submatrixstaticint[][]getSubGrid(intN,int[][]matrix,intx,inty){intn=(int)Math.sqrt(N);intsubGrid[][]=newint[n][n];for(inti=0;i<n;i++){for(intj=0;j<n;j++){subGrid[i][j]=matrix[n*(x/n)+(j/n)+i][n*(y/n)+j%n];}}// Return the submatrixreturnsubGrid;}// Driver codepublicstaticvoidmain(String[]args){intN=9;intmatrix[][]={{1,2,3,9,8,7,1,2,1},{4,5,6,1,2,3,7,9,8},{7,8,9,2,2,0,1,5,7},{0,2,9,1,2,3,4,5,3},{9,8,7,4,5,6,7,4,1},{1,4,7,7,8,9,9,8,7},{5,6,1,9,8,7,5,2,3},{4,5,1,6,5,4,9,7,4},{8,7,9,3,2,1,9,4,9}};intx=4;inty=4;// Function callintsubGrid[][]=getSubGrid(N,matrix,x,y);System.out.println(Arrays.deepToString(subGrid));}}
Python3
# python3 code to implement the approachimportmath# Function to print submatrixdefgetSubGrid(N,matrix,x,y):n=int(math.sqrt(N))subGrid=[[0for_inrange(n)]for_inrange(n)]foriinrange(0,n):forjinrange(0,n):subGrid[i][j]=matrix[n*(x//n)+(j//n)+i][n*(y//n)+j%n]# Return the submatrixreturnsubGrid# Driver Codeif__name__=="__main__":N=9matrix=[[1,2,3,9,8,7,1,2,1],[4,5,6,1,2,3,7,9,8],[7,8,9,2,2,0,1,5,7],[0,2,9,1,2,3,4,5,3],[9,8,7,4,5,6,7,4,1],[1,4,7,7,8,9,9,8,7],[5,6,1,9,8,7,5,2,3],[4,5,1,6,5,4,9,7,4],[8,7,9,3,2,1,9,4,9]]x=4y=4# Function callsubGrid=getSubGrid(N,matrix,x,y)foriinrange(0,len(subGrid)):print("[",end="")j=0while(j<len(subGrid)-1):print(subGrid[i][j],end=", ")j+=1print(subGrid[i][j],end="] ")# This code is contributed by rakeshsahni
C#
// C# code to implement the approachusingSystem;classGFG{// Function to print submatrixstaticint[,]getSubGrid(intN,int[,]matrix,intx,inty){intn=(int)Math.Sqrt(N);int[,]subGrid=newint[n,n];for(inti=0;i<n;i++){for(intj=0;j<n;j++){subGrid[i,j]=matrix[n*(x/n)+(j/n)+i,n*(y/n)+j%n];}}// Return the submatrixreturnsubGrid;}// Driver codepublicstaticvoidMain(string[]args){intN=9;int[,]matrix={{1,2,3,9,8,7,1,2,1},{4,5,6,1,2,3,7,9,8},{7,8,9,2,2,0,1,5,7},{0,2,9,1,2,3,4,5,3},{9,8,7,4,5,6,7,4,1},{1,4,7,7,8,9,9,8,7},{5,6,1,9,8,7,5,2,3},{4,5,1,6,5,4,9,7,4},{8,7,9,3,2,1,9,4,9}};intx=4;inty=4;// Function callint[,]subGrid=getSubGrid(N,matrix,x,y);Console.Write("[");for(inti=0;i<subGrid.GetLength(0);i++){Console.Write("[");for(intj=0;j<subGrid.GetLength(1);j++)Console.Write(subGrid[i,j]+" ,");Console.Write("] ");}Console.Write("]");}}// This code is contributed by AnkThon
JavaScript
<script>// javascript code to implement the approach// Function to print submatrixfunctiongetSubGrid(N,matrix,x,y){varn=parseInt(Math.sqrt(N));varsubGrid=Array(n).fill(0).map(x=>Array(n).fill(0));for(vari=0;i<n;i++){for(varj=0;j<n;j++){subGrid[i][j]=matrix[n*parseInt(x/n)+parseInt(j/n)+i][n*parseInt(y/n)+j%n];}}// Return the submatrixreturnsubGrid;}// Driver codevarN=9;varmatrix=[[1,2,3,9,8,7,1,2,1],[4,5,6,1,2,3,7,9,8],[7,8,9,2,2,0,1,5,7],[0,2,9,1,2,3,4,5,3],[9,8,7,4,5,6,7,4,1],[1,4,7,7,8,9,9,8,7],[5,6,1,9,8,7,5,2,3],[4,5,1,6,5,4,9,7,4],[8,7,9,3,2,1,9,4,9]];varx=4;vary=4;// Function callvarsubGrid=getSubGrid(N,matrix,x,y);document.write("[");for(leti=0;i<subGrid.length;i++){document.write("[");for(letj=0;j<subGrid[i].length;j++){document.write(subGrid[i][j]+", ");}document.write("]");}document.write("]");// This code contributed by shikhasingrajput </script>