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,941 questions

51,879 answers

573 users

How to count the total pairs in an array in all permutations with even and odd products in Java

2 Answers

0 votes
class Program {
    static void countProductPairs(int[] arr, int[] pairs) {
        int evenPairs = 0;
        int oddPairs = 0;
  
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] * arr[j] % 2 == 0) {
                    evenPairs++;
                } else {
                    oddPairs++;
                }
            }
        }
  
        pairs[0] = evenPairs * 2;
        pairs[1] = oddPairs * 2;
    }
      
    public static void main(String[] args) {
        int[] arr = {1, 8, 5};
        int[] pairs = new int[2];
         
        // The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8) 
        // Pairs with Even product: (1, 8), (8, 1), (8, 5), (5, 8) = 4
        // Total pairs with Odd product: (1, 5), (1, 5) = 2
 
        countProductPairs(arr, pairs);
  
        System.out.println("Total Even Product Pairs = " + pairs[0] );
        System.out.println("Total Odd Product Pairs = " + pairs[1]);
    }
}
  
  
  
/*
run:
  
Total Even Product Pairs = 4
Total Odd Product Pairs = 2
  
*/

 

 



answered Jun 15, 2024 by avibootz
edited Jun 15, 2024 by avibootz
0 votes
class Program {
    static int countOddProductPairs(int arr[]) { 
        int odd = 0; 
      
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 2 != 0) { 
                odd++; 
            }
        } 
         
        return odd * (odd - 1); 
    } 
      
    static int countEvenProductPairs(int total_odd_product_pairs, int arr[]) { 
        int total_pairs = arr.length * (arr.length - 1); 
         
        return total_pairs - total_odd_product_pairs; 
    } 
      
    public static void main(String[] args) {
        int[] arr = {1, 8, 5};
 
        // The pairs are: (1, 8), (1, 5), (8, 1), (8, 5), (5, 1), (5, 8) 
        // Pairs with Even product: (1, 8), (8, 1), (8, 5), (5, 8) = 4
        // Total pairs with Odd product: (1, 5), (1, 5) = 2
 
        int total_odd_product_pairs = countOddProductPairs(arr); 
        int total_even_product_pairs = countEvenProductPairs(total_odd_product_pairs, arr); 
  
        System.out.println("Total Even Product Pairs = " +  total_even_product_pairs);
           
        System.out.println("Total Odd Product Pairs= "+ total_odd_product_pairs);
    }
}
  
  
  
/*
run:
  
Total Even Product Pairs = 4
Total Odd Product Pairs= 2
  
*/

 



answered Jun 15, 2024 by avibootz
edited Jun 18, 2024 by avibootz
...