-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_in_2D_Array.java
More file actions
70 lines (68 loc) · 1.84 KB
/
Binary_Search_in_2D_Array.java
File metadata and controls
70 lines (68 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package technicals;
import java.util.*;
public class Binary {
static int [] search(int [][]matrix, int target,int c)
{
int row =0,column=c-1;//we will start the searching from r=0, column =max index,i.e. from the right hand side.
int [] result= {-1,-1};
while(row<matrix.length && column>=0)//matrix.length =3 here, mainly returns the length of the row.
{
if(matrix[row][column]==target)
{
result[0]=row;
result[1]=column;
return result;
}
else if (target<matrix[row][column])
column--; //when target is less than the current index value we remove the column as we know for every element
//in that column the target will be less so use of that column
else
row++; //for the case when target element greater than the current index value we move to the next row.
//As all the elements will be smaller than the index value as we start checking from the right most side
//So we move to the next row.
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of row and then column");
int r=sc.nextInt();
int c=sc.nextInt();
int[][] matrix=new int[r][c];
System.out.println("Enter the elements in the array");
for(int i =0;i<matrix.length;i++)//for each row
{
for(int j =0;j<matrix[i].length;j++)//for each array of row index i.e. for column
{
matrix[i][j]=sc.nextInt();
}
}
for(int i =0;i<matrix.length;i++)
{
System.out.println(Arrays.toString(matrix[i]));
}
System.out.println("enter the element to be searched");
int target=sc.nextInt();
System.out.println(Arrays.toString(search(matrix,target,c)));
}
}
/*Enter the size of row and then column
3
3
Enter the elements in the array
1
2
3
4
5
6
7
8
9
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
enter the element to be searched
8
[2, 1]
*/