Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,365 questions

11,625 answers

573 users

How to find and print pairs in int array that equal to target number in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
  
    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 3, 1, 2};
       
        findAndPrintPairsEqualtoTarget(arr, 3);
    }
    
    public static void findAndPrintPairsEqualtoTarget(int[] array, int target) {	
	   for (int i = 0; i < array.length; i++)
	   {	
            for (int j = i + 1; j < array.length; j++)
            {	
                if (array[i] + array[j] == target)
                    System.out.println("indexes " + i + " and " + j
						               + " are " + array[i] + " + " + array[j] 
                                       + " = " + target);
            }
	   }
    }
}
    
/*
run:
   
indexes 0 and 1 are 1 + 2 = 3
indexes 0 and 8 are 1 + 2 = 3
indexes 1 and 7 are 2 + 1 = 3
indexes 7 and 8 are 1 + 2 = 3
    
*/

 




answered Jun 29, 2017 by avibootz
...