fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int m;
  11. public static int n;
  12.  
  13. public static boolean check(int[][] matrix, int c){
  14. HashMap<Integer, Integer> k = new HashMap<>();
  15. TreeMap<String, Integer> u = new TreeMap<>();
  16.  
  17. int ck = 0;
  18. int count = 0;
  19. int p = 0;
  20.  
  21. for(int i = 0; i < n; i++){
  22. count = 0;
  23. for(int j = 0; j < m; j++){
  24. if(matrix[i][j] >= c){
  25. ck = j;
  26. count++;
  27. }
  28. }
  29. if(count == 1){
  30. k.put(ck, 1 );
  31. }
  32. if(count == 0){
  33. return false;
  34. }
  35. }
  36.  
  37. p = k.size();
  38. for(int i = 0; i < n; i++){
  39. count = 0;
  40. int gp = 0, ck1 = 0, ck2 = 0;
  41. for(int j = 0; j < m; j++){
  42. if(k.getOrDefault(j, 0) == 0 && matrix[i][j] >= c){
  43. count ++;
  44. if(count == 1){
  45. ck1 = j;
  46. }
  47. if(count == 2){
  48. ck2 = j;
  49. }
  50. }
  51. if((k.getOrDefault(j, 0) == 1) && matrix[i][j] >= c){
  52. gp = 1;
  53. }
  54. }
  55.  
  56.  
  57. if(gp == 1){
  58.  
  59. }else{
  60. if(count == 2){
  61. String key = ck1 + "#" + ck2;
  62. u.put(key, u.getOrDefault(key, 0) + 1);
  63. }
  64. }
  65. }
  66.  
  67. int kgp = u.size();
  68. int m1 = m - p;
  69. int v = (m1 * (m1 - 1)) / 2;
  70.  
  71. if(kgp == v){
  72. return false;
  73. }
  74. return true;
  75. }
  76. public static void main (String[] args) throws java.lang.Exception
  77. {
  78. // your code goes here
  79. m = 4;
  80. n = 4;
  81. int[][] matrix = {
  82. {3, 4, 2, 2},
  83. {3, 3, 3, 4},
  84. {2, 4, 2, 3},
  85. {4, 2, 4, 2}
  86. };
  87.  
  88. int c = 0;
  89. while(check(matrix, c)){
  90. c++;
  91. }
  92. System.out.print(c - 1);
  93.  
  94. }
  95. }
Success #stdin #stdout 0.11s 55528KB
stdin
Standard input is empty
stdout
3