Create a resume that lands you SDE interviews at MAANG

Speaker

Anubhav Sinha

SDE-2 @

12 Jun, 2024 @ 01:30 PM

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.

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Approach 1

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;
}

Output:

Java Code:

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);
}
}

Output:

Time and Space Complexity

Time Complexity: O(N2)

Space Complexity: O(1)

Approach 2

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;
}

Output:

Java Code

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);
}
}

Output:

Time and Space Complexity Analysis

Let “n” be the one side dimension of the matrix.

Only “n” number of traversals are made for one diagonal.

Therefore total time taken is n+n, i.e. 2n

In big O notation, it is O(n)

No extra space is used for diagonal elements’ square calculation.

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.