Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninja !! Let’s dive into solving this easy yet interesting question involving matrix and its diagonals. We would cover all the aspects of the problem, from solution walkthrough to time and space complexity analysis. At the end of this blog, you will have in-depth knowledge of working with matrics.
You are given a square matrix of odd dimensions. Your task is to print the squares of both diagonal elements. Go through the examples to understand more.
Step 1:Initialise the matrix in the main function.
Step 2: Call the helper function, passing the matrix and its dimensions.
Step 3: Use a nested for loop to access each element and check whether its row index and
column index are the same or not for the “diagonal one”.
Step 4: If it's the same print, it's square.
Step 5: For “diagonal two”, check if the sum of the row and column indices equals the number of columns minus one.
Step 6: If they are the same, print its square.
C++ Code
#include <bits/stdc++.h>
using namespace std;
//Helper function to calculate and print squares of diagonal elements
void helper(vector<vector<int>>&inputMatrix,int n)
{
cout<<"Diagonal One : ";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
cout<<pow(inputMatrix[i][j],2)<<" ";
}
}
}
cout<<endl<<"Diagonal Two : ";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i+j==n-1)
cout<<pow(inputMatrix[i][j],2)<<" ";
}
}
}
int main()
{
int n;
//Taking side input from user
cout<<"Enter the side length: ";
cin>>n;
vector<vector<int>>inputMatrix(n);
cout<<"Enter the matrix elements:\n";
//Taking matrix elements input
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int x;
cin>>x;
inputMatrix[i].push_back(x);
}
}
//Calling the helper function to calculate and print the elements
helper(inputMatrix,n);
return 0;
}
You can also try this code with Online C++ Compiler
import java.util.*;
public class Main
{
//helper function to print the square of diagonal elements
public static void helper(ArrayList<ArrayList<Integer>>inputMatrix,int n)
{
//Code to print elements of Diagonal one
System.out.print("Diagonal One : ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
System.out.print(inputMatrix.get(i).get(j)*inputMatrix.get(i).get(j) +" ");
}
}
}
//Code to print elements of Diagonal two
System.out.print("\nDiagonal Two : ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i+j==n-1)
System.out.print(inputMatrix.get(i).get(j)*inputMatrix.get(i).get(j) +" ");
}
}
}
public static void main(String[] args) {
//Taking side input from user
System.out.println("Enter the side length : ");
Scanner obj=new Scanner(System.in);
int n=obj.nextInt();
//Initialising 2D matrix
ArrayList<ArrayList<Integer>>inputMatrix=new ArrayList<>(n);
//Taking matrix elements as input from user
for(int i=0;i<n;i++)
{
ArrayList<Integer>temp=new ArrayList();
for(int j=0;j<n;j++)
{
int x=obj.nextInt();
temp.add(x);
}
inputMatrix.add(temp);
}
helper(inputMatrix,n);
}
}
You can also try this code with Online Java Compiler
Step 1: Initialise the matrix in the main function.
Step 2: Call the helper function, passing the matrix and its dimensions.
Step 3: For “diagonal one”, the logic is that we would use elements whose
Both ‘i’ and ‘j’ values are the same. Use a for loop to achieve this functionality.
Step 4: Print the squares of each element.
Step 5: For the “diagonal two” the logic is inputMatrix[i][row-i-1] to capture each element.
Step 6: Print the squares of each element.
C++ Code
#include <bits/stdc++.h>
using namespace std;
//Helper function to calculate and print squares of diagonal elements
void helper(vector<vector<int>>&inputMatrix,int n)
{
cout<<"Diagonal One : ";
//Here output is found using single for loop
for(int i=0;i<n;i++)
{
cout<<inputMatrix[i][i]*inputMatrix[i][i]<<" ";
}
cout<<endl<<"Diagonal Two : ";
//For loop for 2nd Diagonal elements square
for(int i=0;i<n;i++)
{
cout<<inputMatrix[i][n-i-1]*inputMatrix[i][n-i-1]<<" ";
}
}
int main()
{
int n;
//Taking side input from user
cout<<"Enter the side length: ";
cin>>n;
vector<vector<int>>inputMatrix(n);
cout<<"Enter the matrix elements:\n";
//Taking matrix elements input
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int x;
cin>>x;
inputMatrix[i].push_back(x);
}
}
//Calling the helper function to calculate and print the elements
helper(inputMatrix,n);
return 0;
}
You can also try this code with Online C++ Compiler
import java.util.*;
public class Main
{
//helper function to print the square of diagonal elements
public static void helper(ArrayList<ArrayList<Integer>>inputMatrix,int n)
{
//Code to print elements of Diagonal one
System.out.print("Diagonal One : ");
for(int i=0;i<n;i++)
{
System.out.print(inputMatrix.get(i).get(i)*inputMatrix.get(i).get(i)+" ");
}
//Code to print elements of Diagonal two
System.out.print("\nDiagonal Two : ");
for(int i=0;i<n;i++)
{
System.out.print(inputMatrix.get(i).get(n-i-1)*inputMatrix.get(i).get(n-i-1)+" ");
}
}
public static void main(String[] args) {
//Taking side input from user
System.out.println("Enter the side length : ");
Scanner obj=new Scanner(System.in);
int n=obj.nextInt();
//Initialising 2D matrix
ArrayList<ArrayList<Integer>>inputMatrix=new ArrayList<>(n);
//Taking matrix elements as input from user
for(int i=0;i<n;i++)
{
ArrayList<Integer>temp=new ArrayList();
for(int j=0;j<n;j++)
{
int x=obj.nextInt();
temp.add(x);
}
inputMatrix.add(temp);
}
helper(inputMatrix,n);
}
}
You can also try this code with Online Java Compiler
A matrix is an array of arrays. You can consider it as a grid.
What is the time complexity to traverse a 2D matrix?
The time taken is O(n*n).
What is a vector?
A vector is a data structure which allows dynamic-sized arrays, i.e. array size can change at runtime.
What is import?
This is a keyword in java which is used to import pre-written libraries.
What is an object?
An object is an instance of a class.
Conclusion
This blog taught you how to deal with 2-D matrices and access their diagonals. You can use this knowledge to solve other questions based on matrix diagonals. You also learned how to analyse time and space complexity.