fork download
  1. //import static org.junit.Assert.*;
  2.  
  3. import java.util.Arrays;
  4.  
  5. //import org.junit.Test;
  6.  
  7. class Sol {
  8.  
  9. public static final class Coord {
  10. private final int col;
  11. private final int row;
  12.  
  13. public Coord(int _row, int _col) {
  14. col = _row;
  15. row = _col;
  16. }
  17. }
  18.  
  19. private static final void displayMatrix(int[][] matrix, Coord coord) {
  20. StringBuilder sb = new StringBuilder(matrix.length
  21. * (matrix[0].length * 5) + matrix.length);
  22. for (int r = 0; r < matrix.length; r++) {
  23. for (int c = 0; c < matrix[r].length; c++) {
  24. if (r == coord.row && c == coord.col) {
  25. sb.append(String.format("[%2d] ", matrix[r][c]));
  26. } else {
  27. sb.append(String.format(" %2d ", matrix[r][c]));
  28. }
  29. }
  30. sb.append("\n");
  31. }
  32. System.out.println(sb);
  33. }
  34.  
  35. public static Coord findElt(int[][] matrix, int elt) {
  36. for (int r = 0; r < matrix.length; r++) {
  37. int pos = Arrays.binarySearch(matrix[r], elt);
  38. if (pos >= 0) {
  39. return new Coord(pos, r);
  40. }
  41. if (pos == -1) {
  42. // all values in this row, and all later rows
  43. // are too large.
  44. break;
  45. }
  46. }
  47. return new Coord(-1, -1);
  48. }
  49.  
  50. private static final void assertEquals(int a, int b) {
  51. if (a != b) {
  52. throw new IllegalStateException(String.format("A %d is not the same as B %d", a, b));
  53. }
  54. }
  55.  
  56. // @Test
  57. public void test_0() {
  58. int[][] mat = new int[3][3];
  59.  
  60. mat[0][0] = 1;
  61. mat[0][1] = 2;
  62. mat[0][2] = 3;
  63. mat[1][0] = 2;
  64. mat[1][1] = 3;
  65. mat[1][2] = 5;
  66. mat[2][0] = 3;
  67. mat[2][1] = 4;
  68. mat[2][2] = 7;
  69.  
  70. displayMatrix(mat, findElt(mat, 8));
  71.  
  72. assertEquals(-1, findElt(mat, 8).col);
  73. assertEquals(-1, findElt(mat, 8).row);
  74. }
  75.  
  76. // @Test
  77. public void test_1() {
  78. int[][] mat = new int[3][3];
  79.  
  80. mat[0][0] = 1;
  81. mat[0][1] = 2;
  82. mat[0][2] = 3;
  83. mat[1][0] = 2;
  84. mat[1][1] = 3;
  85. mat[1][2] = 5;
  86. mat[2][0] = 3;
  87. mat[2][1] = 4;
  88. mat[2][2] = 7;
  89.  
  90. displayMatrix(mat, findElt(mat, 1));
  91.  
  92. assertEquals(0, findElt(mat, 1).col);
  93. assertEquals(0, findElt(mat, 1).row);
  94. }
  95.  
  96. // @Test
  97. public void test_2() {
  98. int[][] mat = new int[3][3];
  99.  
  100. mat[0][0] = 1;
  101. mat[0][1] = 2;
  102. mat[0][2] = 3;
  103. mat[1][0] = 2;
  104. mat[1][1] = 3;
  105. mat[1][2] = 5;
  106. mat[2][0] = 3;
  107. mat[2][1] = 4;
  108. mat[2][2] = 7;
  109.  
  110. displayMatrix(mat, findElt(mat, 3));
  111.  
  112. assertEquals(2, findElt(mat, 3).col);
  113. assertEquals(0, findElt(mat, 3).row);
  114. }
  115.  
  116. // @Test
  117. public void test_3() {
  118. int[][] mat = new int[3][3];
  119.  
  120. mat[0][0] = 1;
  121. mat[0][1] = 2;
  122. mat[0][2] = 3;
  123. mat[1][0] = 2;
  124. mat[1][1] = 3;
  125. mat[1][2] = 5;
  126. mat[2][0] = 3;
  127. mat[2][1] = 4;
  128. mat[2][2] = 7;
  129.  
  130. displayMatrix(mat, findElt(mat, 7));
  131.  
  132. assertEquals(2, findElt(mat, 7).col);
  133. assertEquals(2, findElt(mat, 7).row);
  134. }
  135.  
  136. // @Test
  137. public void test_4() {
  138. int[][] mat = new int[3][3];
  139.  
  140. mat[0][0] = 1;
  141. mat[0][1] = 2;
  142. mat[0][2] = 3;
  143. mat[1][0] = 2;
  144. mat[1][1] = 3;
  145. mat[1][2] = 5;
  146. mat[2][0] = 3;
  147. mat[2][1] = 4;
  148. mat[2][2] = 7;
  149.  
  150. displayMatrix(mat, findElt(mat, 4));
  151.  
  152. assertEquals(1, findElt(mat, 4).col);
  153. assertEquals(2, findElt(mat, 4).row);
  154. }
  155.  
  156. public static void main(String[] args) {
  157. int N = 3;
  158. int[][] mat = new int[N][N];
  159.  
  160. Sol ex = new Sol();
  161.  
  162. mat[0][0] = 1;
  163. mat[0][1] = 2;
  164. mat[0][2] = 3;
  165. mat[1][0] = 2;
  166. mat[1][1] = 3;
  167. mat[1][2] = 5;
  168. mat[2][0] = 3;
  169. mat[2][1] = 4;
  170. mat[2][2] = 7;
  171.  
  172. ex.test_0();
  173. ex.test_1();
  174. ex.test_2();
  175. ex.test_3();
  176. ex.test_4();
  177.  
  178. Coord a = findElt(mat, 4);
  179. displayMatrix(mat, a);
  180.  
  181. System.out.println(a.col + " " + a.row);
  182.  
  183. }
  184. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
  1    2    3  
  2    3    5  
  3    4    7  

[ 1]   2    3  
  2    3    5  
  3    4    7  

  1    2  [ 3] 
  2    3    5  
  3    4    7  

  1    2    3  
  2    3    5  
  3    4  [ 7] 

  1    2    3  
  2    3    5  
  3  [ 4]   7  

  1    2    3  
  2    3    5  
  3  [ 4]   7  

1 2