{"id":22057,"date":"2021-09-22T17:11:56","date_gmt":"2021-09-22T11:41:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=22057"},"modified":"2021-11-22T18:36:15","modified_gmt":"2021-11-22T13:06:15","slug":"python-program-to-check-if-a-pair-with-given-product-exists-in-a-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-pair-with-given-product-exists-in-a-matrix\/","title":{"rendered":"Python Program to Check if a Pair with Given Product Exists in a Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count Frequency of k in a Matrix of Size n Where Matrix(i, j) = i+j<\/a><\/p>\n Given a matrix and the product value k, the task is to check whether the pair with the given product value exists in the given matrix or not.<\/p>\n What is a matrix:<\/strong><\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/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 check whether the pair with the given product value exists in the given matrix or not.<\/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 Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n In the previous article, we have discussed Python Program to Count Frequency of k in a Matrix of Size n Where Matrix(i, j) = i+j Given a matrix and the product value k, the task is to check whether the pair with the given product value exists in the given matrix or not. What is …<\/p>\nGiven Matrix :\u00a0\r\n1 5 4\r\n8 6 1\r\n6 2 4\r\n\r\n<\/pre>\n
Yes, the pair with the given product value k exists in the given matrix<\/pre>\n
Given Matrix :\u00a0\r\n5 2 1\r\n4 6 3\r\n1 7 1<\/pre>\n
No, the pair with the given product value k does not exist in the given matrix<\/pre>\n
Program to Check if a Pair with Given Product Exists in a Matrix in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function to say CheckPairwith_prodctK() which takes the given matrix and\r\n# given product k value as the arguments and returns true or false.\r\n\r\n\r\ndef CheckPairwith_prodctK(mtrx, gvn_k_val):\r\n # Inside the function, take a new empty list and store it in another variable.\r\n new_lst = []\r\n # Loop till the given number of rows using the For loop.\r\n for n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another Nested\r\n # For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the value of given k modulus mtrx[n][m] is equal to 0 and given k\r\n # value divided by mtrx[n[m] is present in the above declared new list using\r\n # the if # conditional statement.\r\n if ((gvn_k_val % mtrx[n][m] == 0) and\r\n (gvn_k_val \/\/ mtrx[n][m]) in new_lst):\r\n # If it is true, then return true.\r\n return True\r\n # Else append mtrx[n][m] to the above new list using the append() function.\r\n else:\r\n new_lst.append(mtrx[n][m])\r\n\r\n\r\n# Give the matrix as static input and store it in a variable.\r\nmtrx = [[1, 5, 4], [8, 6, 1], [6, 2, 4]]\r\n# Calculate the number of rows of the given matrix by\r\n# calculating the length of the nested list using the len() function\r\n# and store it in a variable mtrxrows.\r\nmtrxrows = len(mtrx)\r\n# Calculate the number of columns of the given matrix by\r\n# calculating the length of the first list in the nested list\r\n# using the len() function and store it in a variable mtrxcols.\r\nmtrxcols = len(mtrx[0])\r\n# Give the k value as static input and store it in another variable.\r\ngvn_k_val = 30\r\n# Pass the given matrix and the given k value as the arguments to the\r\n# CheckPairwith_prodctK() function and check if it returns true or false using\r\n# the if conditional statement.\r\nif (CheckPairwith_prodctK(mtrx, gvn_k_val)):\r\n # If it is true, then print \"Yes, the pair with the given product value k exists in\r\n # the given matrix\".\r\n print(\"Yes, the pair with the given product value k exists in the given matrix\")\r\n\r\nelse:\r\n # Else print \"No, the pair with the given product value k does not exist in the given\r\n # matrix\".\r\n print(\"No, the pair with the given product value k does not exist in the given matrix\")\r\n<\/pre>\n
Yes, the pair with the given product value k exists in the given matrix<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say CheckPairwith_prodctK() which takes the given matrix and\r\n# given product k value as the arguments and returns true or false.\r\n\r\n\r\ndef CheckPairwith_prodctK(mtrx, gvn_k_val):\r\n # Inside the function, take a new empty list and store it in another variable.\r\n new_lst = []\r\n # Loop till the given number of rows using the For loop.\r\n for n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another Nested\r\n # For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the value of given k modulus mtrx[n][m] is equal to 0 and given k\r\n # value divided by mtrx[n[m] is present in the above declared new list using\r\n # the if # conditional statement.\r\n if ((gvn_k_val % mtrx[n][m] == 0) and\r\n (gvn_k_val \/\/ mtrx[n][m]) in new_lst):\r\n # If it is true, then return true.\r\n return True\r\n # Else append mtrx[n][m] to the above new list using the append() function.\r\n else:\r\n new_lst.append(mtrx[n][m])\r\n\r\n\r\n# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n mtrx.append(l)\r\n \r\n# Give the k value as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_k_val = int(input(\"Enter some random number = \"))\r\n# Pass the given matrix and the given k value as the arguments to the\r\n# CheckPairwith_prodctK() function and check if it returns true or false using\r\n# the if conditional statement.\r\nif (CheckPairwith_prodctK(mtrx, gvn_k_val)):\r\n # If it is true, then print \"Yes, the pair with the given product value k exists in\r\n # the given matrix\".\r\n print(\"Yes, the pair with the given product value k exists in the given matrix\")\r\n\r\nelse:\r\n # Else print \"No, the pair with the given product value k does not exist in the given\r\n # matrix\".\r\n print(\"No, the pair with the given product value k does not exist in the given matrix\")\r\n<\/pre>\n
Enter some random number of rows of the matrix = 3\r\nEnter some random number of columns of the matrix = 3\r\nEnter {3} elements of row {1} separated by spaces = 5 2 1\r\nEnter {3} elements of row {2} separated by spaces = 4 6 3\r\nEnter {3} elements of row {3} separated by spaces = 1 7 1\r\nEnter some random number = 50\r\nNo, the pair with the given product value k does not exist in the given matrix<\/pre>\n
\n