Program to Count Pair in an Array or List whose Product is Divisible by K

Python Program to Count Pair in an Array or List whose Product is Divisible by K

Given a list, the task is to count the number of pairs in the given list whose product is divisible by k in Python.

Examples:

Example1:

Input:

Given list = [1, 15, 19, 2, 9, 6, 10, 12]
Given k=3

Output:

The total number of pairs are =  9

Example2:

Input:

Given list = 1 9 21 7 34 29 91 3 8 5
Given k=5

Output:

The total number of pairs are = 11

Python Program to Count Pair in an Array or List whose Product is Divisible by K

Below are the ways to count the number of pairs in the given list whose product is divisible by k in Python.

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Method #1: Using Nested For Loops (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the value of k as static input and store it in another variable.
  • We will tackle this problem using two nested loops.
  • Take a variable count which stores the count and initialize its value to 0.
  • Calculate the length of the given list using the len() function.
  • Iterate from 0 to the number of elements of the given list using For loop.
  • Loop from m+1 to the number of elements of the given list using another For loop(Inner For loop) where m is the iterator value of the parent For loop.
  • Check If (givenlist[m]*givenlist[m] %k == 0) using If conditional Statement where m is the iterator value of the parent For loop and n is the inner loop iterator value.
  • If it is true then increment the count by 1.
  • Print the count.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
givenlst = [1, 15, 19, 2, 9, 6, 10, 12]
# Give the value of k as static input and store it in another variable.
k = 3
# Take a variable count which stores the count and initialize its value to 0.
paircont = 0
# Calculate the length of the given list using the len() function.
listleng = len(givenlst)
# We will use two nested loops.
# Iterate from 0 to the number of elements of the given list using For loop.
for m in range(0, listleng):
    # Loop from m+1 to the number of elements of the given list
    # using another For loop(Inner For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(m+1, listleng):
        # Check If (givenlist[m]*givenlist[m] %k == 0) using If conditional Statement
        # where m is the iterator value of the parent For loop
        # and n is the inner loop iterator value.
        if((givenlst[m]+givenlst[n]) % k == 0):
            # If it is true then increment the count by 1.
            paircont = paircont+1
# Print the count.
print('The total number of pairs are = ', paircont)

Output:

The total number of pairs are =  9

Method #2: Using Nested For Loops (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the value of k as user input using int(input()) and store it in another variable.
  • We will tackle this problem using two nested loops.
  • Take a variable count which stores the count and initialize its value to 0.
  • Calculate the length of the given list using the len() function.
  • Iterate from 0 to the number of elements of the given list using For loop.
  • Loop from m+1 to the number of elements of the given list using another For loop(Inner For loop) where m is the iterator value of the parent For loop.
  • Check If (givenlist[m]*givenlist[m] %k == 0) using If conditional Statement where m is the iterator value of the parent For loop and n is the inner loop iterator value.
  • If it is true then increment the count by 1.
  • Print the count.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
givenlst = list(
    map(int, input('Enter some random List Elements separated by spaces = ').split()))
# Give the value of k as user input using int(input()) and store it in another variable.
k = int(input('Enter some random value of k = '))
# Take a variable count which stores the count and initialize its value to 0.
paircont = 0
# Calculate the length of the given list using the len() function.
listleng = len(givenlst)
# We will use two nested loops.
# Iterate from 0 to the number of elements of the given list using For loop.
for m in range(0, listleng):
    # Loop from m+1 to the number of elements of the given list
    # using another For loop(Inner For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(m+1, listleng):
        # Check If (givenlist[m]*givenlist[m] %k == 0) using If conditional Statement
        # where m is the iterator value of the parent For loop
        # and n is the inner loop iterator value.
        if((givenlst[m]+givenlst[n]) % k == 0):
            # If it is true then increment the count by 1.
            paircont = paircont+1
# Print the count.
print('The total number of pairs are = ', paircont)

Output:

Enter some random List Elements separated by spaces = 1 9 21 7 34 29 91 3 8 5
Enter some random value of k = 5
The total number of pairs are = 11

Related Programs: