Skip to content

Commit

Permalink
Matrix Ques
Browse files Browse the repository at this point in the history
  • Loading branch information
riyakushwaha committed Jan 13, 2021
1 parent 7e8193c commit 9a46fc1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
60 changes: 60 additions & 0 deletions SaddlePoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import java.util.Scanner;
/*
Name: Saddle Point
Source: PepCoding
Link: https://www.pepcoding.com/resources/online-java-foundation/2d-arrays/saddle-point-official/ojquestion
Statement: You are required to find the saddle point of the matrix which is defined as the value which is smallest
in it's row but largest in it's column.
*/
public class SaddlePoint {
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int [][] arr = new int [n][n];

for(int i =0; i<n; i++)
{
for(int j =0; j<n; j++)
{
arr[i][j]= scanner.nextInt();
}
}

int min, max, col;

for(int i =0; i<arr.length; i++)
{
min = max = arr[i][0];
col=0;
for(int j =0; j<arr.length; j++)
{
if(min>arr[i][j])
{
min = arr[i][j];
col =j;
}
}

for(int j =0; j<arr.length; j++)
{
if(max<arr[j][col])
{
max = arr[j][col];
}
}

if(max == min)
{
System.out.println(max);
return;
}

}

System.out.println("Invalid input");

}


}
50 changes: 50 additions & 0 deletions SearchInMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import java.util.Scanner;
/*
Name: Search In A Sorted Matrix
Source: PepCoding
Link: https://www.pepcoding.com/resources/online-java-foundation/2d-arrays/search-in-a-sorted-2d-array-official/ojquestion
Statement: You are required to find x in the matrix and print it's location int (row, col) format as discussed in
output format below.
*/
public class SearchInMatrix {
public static void main(String[] args) {
// write your code here
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();

int[][] arr = new int[n][n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = scanner.nextInt();
}
}

int data = scanner.nextInt();
int i = 0;
int j = n-1;
while(i<arr.length && j>=0)
{
if(arr[i][j]==data)
{
System.out.println(i);
System.out.println(j);
return;
}
else if(data> arr[i][j])
{
i++;
}
else
{
j--;
}

}

System.out.println("Not Found");



}
}

0 comments on commit 9a46fc1

Please sign in to comment.