PROBLEM LINK: Practice contest
Author: salik_anwer
Tester: prasoonjain006
Editorialist: salik_anwer
DIFFICULTY:
Cake walk-Easy
PREREQUISITES:
Array, Matrix
PROBLEM:
Given a square matrix (N × N). Determine if the leading diagonal is Strictly Increasing or not. If it is strictly increasing then give output 1, otherwise print 0.
EXPLANATION:
- The leading diagonal of a square matrix is given by traversing the entries of the given matrix from top-left corner to bottom-right corner.
- It will be strictly increasing if the next element of the diagonal is greater than the current element when moving top to bottom in the diagonal.
- The coordinates of the leading diagonal elements are always (i, i) for a matrix. So, we have to use a for loop to traverse the (i, i) index and incrementing the index with every iteration.
- Meanwhile we will check if the current index entry is less than the entry on the next index in the leading diagonal.
- If the above condition is true for every element in the leading diagonal, the matrix is strictly increasing, else, it is not.
TIME COMPLEXITY:
O(N²)
SOLUTION:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int matrix[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>matrix[i][j];
}
}
int chk;
for(int i=0;i<n-1;i++){
if(matrix[i+1][i+1]>matrix[i][i]){
chk=1;
}
else{
chk=0;
break;
}
}
cout<<chk<<endl;
}
return 0;
}
Thanks for reading and do share your own solutions here