Ex:- [ 2, 4, 3, 5, 1, 6, 0 ] and 5
(2, 3) ==> 2 + 3 = 5
(4, 1) ==> 4 + 1 = 5
(5, 0) ==> 5 + 0 = 5
Java
1 2 3 4 5 6 7 8 9 10 | public class ArraySum { public static void main(String []args){ int[] arr = {2, 4, 3, 5, 1, 6, 0}; int given_sum = 5; for(int i=0; i<arr.length; i++) for(int j=i+1; j<arr.length; j++) if(arr[i]+arr[j]==given_sum) System.out.println(arr[i]+" "+arr[j]); } } |
Python
given_list = [2, 4, 3, 5, 1, 6, 0] given_sum = 5 for i in range(0,len(given_list)): if given_sum-given_list[i] in given_list[i+1:]: print(given_list[i], given_sum-given_list[i]) |