{"id":12504,"date":"2021-09-30T16:30:46","date_gmt":"2021-09-30T11:00:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=12504"},"modified":"2021-11-22T18:33:34","modified_gmt":"2021-11-22T13:03:34","slug":"python-program-to-count-pair-in-an-array-or-list-whose-product-is-divisible-by-k","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-pair-in-an-array-or-list-whose-product-is-divisible-by-k\/","title":{"rendered":"Python Program to Count Pair in an Array or List whose Product is Divisible by K"},"content":{"rendered":"
Given a list, the task is to count the number of pairs in the given list whose product is divisible by k in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to count the number of pairs in the given list whose product is divisible by k in Python.<\/p>\n 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<\/a> & achieve your goal in improving java coding skills.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nGiven list = [1, 15, 19, 2, 9, 6, 10, 12]\r\nGiven k=3<\/pre>\n
The total number of pairs are = 9<\/pre>\n
Given list = 1 9 21 7 34 29 91 3 8 5\r\nGiven k=5<\/pre>\n
The total number of pairs are = 11<\/pre>\n
Python Program to Count Pair in an Array or List whose Product is Divisible by K<\/h2>\n
\n
Method #1: Using Nested For Loops (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngivenlst = [1, 15, 19, 2, 9, 6, 10, 12]\r\n# Give the value of k as static input and store it in another variable.\r\nk = 3\r\n# Take a variable count which stores the count and initialize its value to 0.\r\npaircont = 0\r\n# Calculate the length of the given list using the len() function.\r\nlistleng = len(givenlst)\r\n# We will use two nested loops.\r\n# Iterate from 0 to the number of elements of the given list using For loop.\r\nfor m in range(0, listleng):\r\n # Loop from m+1 to the number of elements of the given list\r\n # using another For loop(Inner For loop)\r\n # where m is the iterator value of the parent For loop.\r\n for n in range(m+1, listleng):\r\n # Check If (givenlist[m]*givenlist[m] %k == 0) using If conditional Statement\r\n # where m is the iterator value of the parent For loop\r\n # and n is the inner loop iterator value.\r\n if((givenlst[m]+givenlst[n]) % k == 0):\r\n # If it is true then increment the count by 1.\r\n paircont = paircont+1\r\n# Print the count.\r\nprint('The total number of pairs are = ', paircont)\r\n<\/pre>\n
The total number of pairs are = 9<\/pre>\n
Method #2: Using Nested For Loops (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngivenlst = list(\r\n map(int, input('Enter some random List Elements separated by spaces = ').split()))\r\n# Give the value of k as user input using int(input()) and store it in another variable.\r\nk = int(input('Enter some random value of k = '))\r\n# Take a variable count which stores the count and initialize its value to 0.\r\npaircont = 0\r\n# Calculate the length of the given list using the len() function.\r\nlistleng = len(givenlst)\r\n# We will use two nested loops.\r\n# Iterate from 0 to the number of elements of the given list using For loop.\r\nfor m in range(0, listleng):\r\n # Loop from m+1 to the number of elements of the given list\r\n # using another For loop(Inner For loop)\r\n # where m is the iterator value of the parent For loop.\r\n for n in range(m+1, listleng):\r\n # Check If (givenlist[m]*givenlist[m] %k == 0) using If conditional Statement\r\n # where m is the iterator value of the parent For loop\r\n # and n is the inner loop iterator value.\r\n if((givenlst[m]+givenlst[n]) % k == 0):\r\n # If it is true then increment the count by 1.\r\n paircont = paircont+1\r\n# Print the count.\r\nprint('The total number of pairs are = ', paircont)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 9 21 7 34 29 91 3 8 5\r\nEnter some random value of k = 5\r\nThe total number of pairs are = 11<\/pre>\n
\n