fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static int bruteForceCountPairs(int[] arr, int k) {
  5. int count = 0;
  6. for (int i = 0; i < arr.length - 1; ++i) {
  7. for (int j = i + 1; j < arr.length; ++j) {
  8. if (arr[i] + arr[j] == k) {
  9. count++;
  10. }
  11. }
  12. }
  13. return count;
  14. }
  15.  
  16. public static void main(String[] args) {
  17. int[] arr = {1, 2, 3, 4, 5};
  18. int k = 6;
  19. System.out.println("Count of pairs: " + bruteForceCountPairs(arr, k));
  20. }
  21. }
Success #stdin #stdout 0.1s 55664KB
stdin
Standard input is empty
stdout
Count of pairs: 2