fork download
  1. # your code goes here
  2.  
  3. A = [ [1, 3, 5, 7],
  4. [10, 11, 16, 20],
  5. [23, 30, 34, 50] ]
  6. B = 1
  7.  
  8. rows = len(A)
  9. cols = len(A[0])
  10.  
  11. total_elements = rows * cols
  12. left = 0
  13. right = total_elements - 1
  14.  
  15. while left<=right:
  16. mid = round(((left + right) / 2))
  17. row_num = int(mid / cols)
  18. col_num = int((mid % cols))
  19. print(str(left)+" "+str(right)+" "+str(mid)+" "+str(row_num)+" "+str(col_num))
  20. if A[row_num][col_num] == B:
  21. print(1)
  22. break
  23. if A[row_num][col_num] < B:
  24. left = mid + 1
  25. else:
  26. right = mid - 1
  27.  
  28. print(0)
Success #stdin #stdout 0.02s 9168KB
stdin
Standard input is empty
stdout
0 11 6 1 2
0 5 2 0 2
0 1 0 0 0
1
0