Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 answers

573 users

How to count the total of all pairs permutations in an array with Java

1 Answer

0 votes
class Program {
    public static void main(String[] args) {
        int[] arr1 = {1, 8, 5};

        // The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8) 

        int total_pairs = arr1.length * (arr1.length - 1);
        System.out.println("Total Pairs = " +  total_pairs); 
        
        int[] arr2 = {1, 8, 5, 2};

        // The pairs are: (1, 8), (1, 5), (1, 2), (8, 1), (8, 5), (8, 2), 
        //                (5, 1), (5, 8), (5, 2), (2, 1), (2, 8), (2, 5)

        total_pairs = arr2.length * (arr2.length - 1);
        System.out.println("Total Pairs = " +  total_pairs); 
    }
}
 
 
 
/*
run:
 
Total Pairs = 6
Total Pairs = 12
 
*/

 



answered Jun 15, 2024 by avibootz
...