tag:blogger.com,1999:blog-8712770457197348465.post1963104449983839980..comments2024-03-29T00:43:22.798-07:00Comments on Javarevisited: How to Find all Pairs in Array of Integers Whose sum is Equal to a Given Number in Java? Solution Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger33125tag:blogger.com,1999:blog-8712770457197348465.post-53042568288066773232020-04-14T08:57:50.740-07:002020-04-14T08:57:50.740-07:00How can I write a program to found all numbers fro...How can I write a program to found all numbers from 1 to n whose sum is equal to n.<br />Example:<br />15= 1+14,2+13,3+12,....,1+2+12,1+3+11,.....,1+2+3+4+5.Anonymoushttps://www.blogger.com/profile/04897677028710462279noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76151348178132762222020-03-05T10:25:27.333-08:002020-03-05T10:25:27.333-08:00else {
System.out.println("...else {<br /> System.out.println("pair:("+arr[i] +","+target+")");<br /> set.remove(target);<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80870747919356986552020-03-05T10:23:22.928-08:002020-03-05T10:23:22.928-08:00printPairsUsingSet------duplicate pair is printing...printPairsUsingSet------duplicate pair is printing so need to remove once found set .remove(target);Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-84927989922177562132020-02-23T17:50:20.981-08:002020-02-23T17:50:20.981-08:00If we take the example int arr[] = { 4, 5, 7, -1, ...If we take the example int arr[] = { 4, 5, 7, -1, 5, 1, 1 }; int sum = 6;<br />The hashset method does not return all possible pairs.Mitali Jainhttps://www.blogger.com/profile/10950246962643848425noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27968488973115260232020-02-05T03:46:27.135-08:002020-02-05T03:46:27.135-08:00Yes, but time complexity of this solution is O(n^2...Yes, but time complexity of this solution is O(n^2) which is not great, can you optimize your solution upto O(n) ?javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90246364154869318592020-02-04T11:12:09.778-08:002020-02-04T11:12:09.778-08:00easiest answer
class pairing{
public static void ...easiest answer<br />class pairing{<br /> public static void main(String[] args) {<br /> int[] array ={1,2,3,4,5,6,9};<br /> int a= 10;<br /> for(int i=0;i<array.length;i++){<br /> for(int j=i+1;j<array.length;j++){<br /> int b= array[i]+array[j];<br /> if(a == b){<br />System.out.println("the corresponding pair is {"+array[i]+"," +array[j]+"}");<br /Anonymoushttps://www.blogger.com/profile/17283112672083378511noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-86140498625500695782019-11-20T14:39:02.987-08:002019-11-20T14:39:02.987-08:00public static void main(String args[]) {
int[] a...public static void main(String args[]) {<br /> int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };<br /> int toplam = 10;<br /><br /> for (int i = 1; i < arr.length; i++) {<br /> for (int j = 0; j < arr.length; j++) {<br /> if ((arr[i] + arr[j]) == toplam) {<br /> System.out.println(arr[i]+ "+" +arr[j] + "=" + toplam);<br /><br /> }<br /><br /> }<br /><br />Anonymoushttps://www.blogger.com/profile/12033983669164594035noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61190538784776623392019-09-12T08:07:04.915-07:002019-09-12T08:07:04.915-07:00Yes set returns false if we try insert duplicate.
...Yes set returns false if we try insert duplicate.<br />NOT operator should be removed from if.<br />Also try to code it to find the optimal result.Anonymoushttps://www.blogger.com/profile/07159689989634597692noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-8440359513287008922019-07-30T06:29:51.082-07:002019-07-30T06:29:51.082-07:00Python is like cheat-code for coding, makes it so ...Python is like cheat-code for coding, makes it so simple and succinct. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-17093446349437393622019-07-29T07:34:24.153-07:002019-07-29T07:34:24.153-07:00#!python3.7
#Function to print pairs
def print_pa...#!python3.7<br /><br />#Function to print pairs<br />def print_pairs_matching_sum(arr, n, s):<br /> mdict = {}<br /> for i in arr:<br /> if i not in mdict:<br /> mdict[i] = s - i<br /> if (s-i) in mdict:<br /> print(mdict[i], i)<br /><br /><br />#Test Driving Code<br />a = [2, 4, 5, 1, 3, 2, 3, 4]<br />print_pairs_matching_sum(a, len(a), 6)Prakashhttps://www.blogger.com/profile/09704825189822006762noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68499145613802896262019-04-05T12:18:37.999-07:002019-04-05T12:18:37.999-07:00public class Sum{
public static void main(String[...public class Sum{<br /> public static void main(String[] as){<br /> int[] v={2,6,3,9,11};<br /> int m=9;<br /> for(int i=0;i<5;i++){<br /> for(int j=i+1;j<5;j++){<br /> if(v[i]+v[j]==m){<br /> System.out.println("["+v[i]+" "+v[j]+"]");<br /> }<br /> }<br /> }<br /> <br /> <br /> }<br />}Arjunhttps://www.blogger.com/profile/09362436553575212114noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-13080569777471977102019-02-23T21:53:01.233-08:002019-02-23T21:53:01.233-08:00I wnt this in python can i get? I wnt this in python can i get? Anonymoushttps://www.blogger.com/profile/17295242914871280684noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-20886706100131314772019-01-02T03:37:32.295-08:002019-01-02T03:37:32.295-08:00public static void main(String[] args) {
int[] n...public static void main(String[] args) {<br /> int[] numbers = {2, 4, 3, 5, 6, -2, 4, 7, 8, 9};<br /> int sumNumber = 7;<br /> <br /> Arrays.sort(numbers);<br /> <br /> for (int i = 0; i < numbers.length; i++) {<br /> int num1 = numbers[i];<br /> for (int j = i; j < numbers.length; j++) {<br /> int num2 = numbers[j];<br /> if(num1 + num2 == sumNumber) {<br /> Anonymoushttps://www.blogger.com/profile/06871741811473571036noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-76482206413547380122018-12-28T02:33:43.864-08:002018-12-28T02:33:43.864-08:00 A = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
for ... A = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]<br /><br /> for i in range(len(A)-1):<br /> for j in range(i+1, len(A)):<br /> if A[i] + A[j] == 7:<br /> print i, j<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-61024596416347567622018-10-04T23:11:22.324-07:002018-10-04T23:11:22.324-07:00Using the integers -1, 4, 40, and -25, create as m...Using the integers -1, 4, 40, and -25, create as many equations you can with an answer of 34. You can use each integer only once.Anonymoushttps://www.blogger.com/profile/04411355302387984694noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67264138853424185152018-10-04T23:09:12.808-07:002018-10-04T23:09:12.808-07:00Give 5 pairs of integers whose product is less tha...Give 5 pairs of integers whose product is less than zero and whose sum is -26.Anonymoushttps://www.blogger.com/profile/04411355302387984694noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41877194101596225872018-04-05T20:33:28.321-07:002018-04-05T20:33:28.321-07:00In third case, sorting will take O(nlogn ) and whi...In third case, sorting will take O(nlogn ) and while loop will take o(n) time then in combination o(nlogn) +o(n) it should be o(n). But answer states O(nlogn). Please provide your thoughts on this. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10830980155347294332018-02-16T22:22:41.843-08:002018-02-16T22:22:41.843-08:00Not only is it simple, it is super simple in ruby ...Not only is it simple, it is super simple in ruby or clojure :<br /><br />In ruby : [2, 6, 3, 9, 11].combination(2).select{|i| i.sum == 9}<br />In clojure : (filter #(= (reduce + %) 5) (combo/combinations '(1 2 3 4 5) 2))Pankaj Dohareyhttps://www.blogger.com/profile/12590695105739765705noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-59945268611718529902018-01-10T10:43:52.405-08:002018-01-10T10:43:52.405-08:00In the Method 2 : take input as {2,1,5,7} sum =8 ....In the Method 2 : take input as {2,1,5,7} sum =8 .... it gives only {1,7} ...even though there are 2 outputs possible i,e {1,7} and {2,1,5}Anonymoushttps://www.blogger.com/profile/11636537341340277723noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27155078362901989412017-07-22T11:14:40.198-07:002017-07-22T11:14:40.198-07:00This code only considers 2 numbers for forming the...This code only considers 2 numbers for forming the pairs. I am not very good at coding, but I guess this code will consider all pairs.<br /><br /><br />public static String sum_pairs(int[] array, int number)<br /> {<br /> String result = "";<br /> int sum = 0;<br /> int j = 0;<br /> String temp = "";<br /> <br /> for(int i = 0; i < array.length; i ++)<br /> {<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55901106054218721912017-04-20T02:28:57.410-07:002017-04-20T02:28:57.410-07:00finding all pairs of elements with a given differe...finding all pairs of elements with a given difference in an array of integers in javaAnonymoushttps://www.blogger.com/profile/15463514271940750309noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36353582764904505942017-02-17T23:38:34.756-08:002017-02-17T23:38:34.756-08:00import java.util.HashMap;
import java.util.Map;
pu...import java.util.HashMap;<br />import java.util.Map;<br />public class SumEqualToPairOfNumber {<br /> public static void main(String[] args) {<br /> int[] arr = {1, 2, 1, 3, 6, 4, 2, 2, 3};<br /> int x = 5;<br /> Map result = findPairs(arr, x);<br /> for (Map.Entry entry : result.entrySet())<br /> {<br /> System.out.println("(" + entry.getKey() + "," + entry.getValueRAJESH KUMARhttps://www.blogger.com/profile/13507253096642575823noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43226965415996689782016-10-01T04:48:52.141-07:002016-10-01T04:48:52.141-07:00This is so interesting and helpful for me a lot..t...This is so interesting and helpful for me a lot..thanks JAVA revisited..Anjihttps://www.blogger.com/profile/15081224401591474330noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58183100280478020142016-08-20T07:09:57.022-07:002016-08-20T07:09:57.022-07:00Hi Javin,
It seems that the 3rd method only works ...Hi Javin,<br />It seems that the 3rd method only works in the case when first set of pairs, after sorting, produces the desired sum. <br />In your case, e.g.<br />-11,20( array 1)<br />-1,10 ( array 2)<br /><br />because then only the left and right index have proceeded further in your case.<br /><br />Can you check the same program, for below set of arrays and desired sum = 8.<br />int[] ar = {Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-15077992607025505332016-08-14T11:41:02.264-07:002016-08-14T11:41:02.264-07:00public static void main(String[] args) {
int a[] ...public static void main(String[] args) {<br /> int a[] = {5,3,4,8};<br /> pairSum(a,9);<br />}<br /><br />private static void pairSum(int[] a,int sum) {<br /> Arrays.sort(a);<br /> System.out.println(Arrays.toString(a));<br /> int i=0;<br /> int j=0;<br /> int temp=a[0];<br /> for( i=0,j=(a.length-1);i<j;){<br /> temp= a[i]+a[j];<br /> if(temp==sum){<br /> System.out.println(a[i]+" , Anonymousnoreply@blogger.com