-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoDimArr.c
40 lines (36 loc) · 892 Bytes
/
twoDimArr.c
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
//Apply Binary Search on 2D NxM array (A) having numbers stored in non-deceasing order under row-major scanning.
//Hint: k-th element = A[k/M][k % M]
#include <stdio.h>
#include<stdlib.h>
int main(void) {
// your code goes here
int high,low,mid,n,m,i,j,x,flag=0;
printf("enter size of array\n");
scanf("%d %d",&n,&m);
int arr[n][m];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&arr[i][j]);
printf("enter element to be searched in the list\n");
scanf("%d",&x);
low=0;
high=(n*m)-1;
// k-th element = A[k/M][k % M]
while(high>=low){
mid=(low+high)/2;
if(arr[mid/m][mid % m] == x)
{
flag=1;
break;
}
else if(arr[mid/m][mid % m]<x) // since the array is sorted
low=mid+1;
else
high=mid-1;
}
if(flag==0)
printf("the element could not be found\n");
else
printf("the row index is %d and column index is %d\n",mid/m,mid%m);
return 0;
}