JAVA Code : public List findTriplets(int[] arr) { // Your code here Map map = new HashMap(); int n = arr.length; for(int i = 0; i < n - 1; ++i){ for(int j= i +1; j < n; ++j){ int sum = arr[i] + arr[j]; if(!map.containsKey(sum)){ map.put(sum, new ArrayList()); } map.get(sum).add(new int[]{i, j}); } } Set resSet = new HashSet(); for(int k = 0; k < n; ++k){ int rem = -arr[k]; if(map.containsKey(rem)){ List pairs = map.get(rem); for(int[] p : pairs){ if(p[0] != k && p[1] != k){ List curr = Arrays.asList(k, p[0], p[1]); Collections.sort(curr); resSet.add(curr); } } } } return new ArrayList(resSet); }
@21flame72Күн бұрын
sir why pre increment if any edge cases there for doing i++;
@ajinkyajain2302Күн бұрын
No, you can use either pre increment or post increment, it doesn't matter in this scenario.