Advertisement
since85stas

Untitled

Mar 18th, 2021
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class SumFour3 {
  5.  
  6.  
  7. public static void main(String[] args) throws IOException {
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
  9. StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
  10. int n = Integer.parseInt(tokenizer.nextToken());
  11.  
  12. tokenizer = new StringTokenizer(reader.readLine());
  13. short s = Short.parseShort(tokenizer.nextToken());
  14.  
  15. int[] array = new int[n];
  16. tokenizer = new StringTokenizer(reader.readLine());
  17. for (int i = 0; i < n; i++) {
  18. array[i] = Integer.parseInt(tokenizer.nextToken());
  19. }
  20.  
  21. Arrays.sort(array);
  22.  
  23. findTriplets(array, array.length, s);
  24. }
  25.  
  26. static void findTriplets(int arr[], int n, int s)
  27. {
  28. HashSet<Four> resSet = new HashSet<>();
  29.  
  30. for (int i = 0; i < n - 2; i++)
  31. {
  32. // HashSet<Integer> set = new HashSet<Integer>();
  33. for (int j = i + 1; j < n - 1; j++)
  34. {
  35. // Find all pairs with sum equals to
  36. // "-arr[i]"
  37. HashSet<Integer> set = new HashSet<Integer>();
  38. for (int k = j+1; k < n; k++) {
  39. int x = (s - arr[i] - arr[j] - arr[k]);
  40. if (set.contains(x))
  41. {
  42. // System.out.printf("%d %d %d %d\n", x, arr[i], arr[j], arr[k]);
  43. if (x < arr[i]) {
  44. resSet.add(new Four(x, arr[i], arr[j], arr[k]));
  45. } else if (x < arr[j]) {
  46. resSet.add(new Four(arr[i],x, arr[j], arr[k]));
  47. } else if (x < arr[k]) {
  48. resSet.add(new Four(arr[i],arr[j], x, arr[k]));
  49. } else {
  50. resSet.add(new Four(arr[i],arr[j], arr[k],x));
  51. }
  52. }
  53. else
  54. {
  55. set.add(arr[k]);
  56. }
  57. }
  58.  
  59. }
  60. }
  61.  
  62. List<Four> list = new ArrayList<>(resSet);
  63. //
  64. Collections.sort(list);
  65. System.out.println(list.size());
  66. for (Four f:
  67. list) {
  68. System.out.println(f.comp);
  69. }
  70.  
  71. // if (found == false)
  72. // {
  73. // System.out.printf(" No Triplet Found\n");
  74. // }
  75. }
  76.  
  77. static class Four implements Comparable<Four> {
  78.  
  79. int a;
  80. int b;
  81. int c;
  82. int d;
  83.  
  84. String comp;
  85.  
  86. public Four (int a, int b, int c, int d) {
  87. this.a = a;
  88. this.b = b;
  89. this.c = c;
  90. this.d = d;
  91.  
  92. StringBuilder builder = new StringBuilder();
  93. builder.append(this.a).append(" ").append(this.b).append(" ").append(this.c).append(" ").append(this.d);
  94. comp = builder.toString();
  95. }
  96.  
  97. @Override
  98. public boolean equals(Object o) {
  99. if (this == o) return true;
  100. if (!(o instanceof Four)) return false;
  101. Four four = (Four) o;
  102. return a == four.a &&
  103. b == four.b &&
  104. c == four.c &&
  105. d == four.d;
  106. }
  107.  
  108. @Override
  109. public int hashCode() {
  110. return Objects.hash(a, b, c, d);
  111. }
  112.  
  113. @Override
  114. public int compareTo(Four four) {
  115. if (this.equals(four)) return 0;
  116. else {
  117. if (a < four.a) return -1;
  118. else if(a > four.a) return 1;
  119. else {
  120. if (b < four.b) return -1;
  121. else if(b > four.b) return 1;
  122. else {
  123. if (c < four.c) return -1;
  124. else if(c > four.c) return 1;
  125. else {
  126. if (d < four.d) return -1;
  127. else if(d > four.d) return 1;
  128. else {
  129. return 0;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137.  
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement