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 a matrix:
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.
Example:
Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.
In this order, the dimensions of a matrix indicate the number of rows and columns.
Here as there are 5 rows and 4 columns it is called a 5*4 matrix.
- Python Program for Multiplication of Two Matrices | How do you do Matrix Multiplication in Python?
- Program to Subtract two Matrices in Python & C++ Programming
- Program for addition of two matrices in Python & C++ Programming
Examples:
Example1:
Input:
Given Matrix : 1 5 4 8 6 1 6 2 4
Output:
Yes, the pair with the given product value k exists in the given matrix
Example2:
Input:
Given Matrix : 5 2 1 4 6 3 1 7 1
Output:
No, the pair with the given product value k does not exist in the given matrix
Program to Check if a Pair with Given Product Exists in a Matrix in Python
Below are the ways to check whether the pair with the given product value exists in the given matrix or not.
Method #1: Using For Loop (Static Input)
Approach:
- Give the matrix as static input and store it in a variable.
- Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Give the k value as static input and store it in another variable.
- Create a function to say CheckPairwith_prodctK() which takes the given matrix and given product k value as the arguments and returns true or false.
- Inside the function, take a new empty list and store it in another variable.
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Check if the value of given k modulus mtrx[n][m] is equal to 0 and given k value divided by mtrx[n[m] is present in the above declared new list using the if conditional statement.
- If it is true, then return true.
- Else append mtrx[n][m] to the above new list using the append() function.
- Pass the given matrix and the given k value as the arguments to the CheckPairwith_prodctK() function and check if it returns true or false using the if conditional statement.
- If it is true, then print “Yes, the pair with the given product value k exists in the given matrix”.
- Else print “No, the pair with the given product value k does not exist in the given matrix”.
- The Exit of the Program.
Below is the implementation:
# Create a function to say CheckPairwith_prodctK() which takes the given matrix and # given product k value as the arguments and returns true or false. def CheckPairwith_prodctK(mtrx, gvn_k_val): # Inside the function, take a new empty list and store it in another variable. new_lst = [] # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another Nested # For loop(Inner For loop). for m in range(mtrxcols): # Check if the value of given k modulus mtrx[n][m] is equal to 0 and given k # value divided by mtrx[n[m] is present in the above declared new list using # the if # conditional statement. if ((gvn_k_val % mtrx[n][m] == 0) and (gvn_k_val // mtrx[n][m]) in new_lst): # If it is true, then return true. return True # Else append mtrx[n][m] to the above new list using the append() function. else: new_lst.append(mtrx[n][m]) # Give the matrix as static input and store it in a variable. mtrx = [[1, 5, 4], [8, 6, 1], [6, 2, 4]] # Calculate the number of rows of the given matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(mtrx[0]) # Give the k value as static input and store it in another variable. gvn_k_val = 30 # Pass the given matrix and the given k value as the arguments to the # CheckPairwith_prodctK() function and check if it returns true or false using # the if conditional statement. if (CheckPairwith_prodctK(mtrx, gvn_k_val)): # If it is true, then print "Yes, the pair with the given product value k exists in # the given matrix". print("Yes, the pair with the given product value k exists in the given matrix") else: # Else print "No, the pair with the given product value k does not exist in the given # matrix". print("No, the pair with the given product value k does not exist in the given matrix")
Output:
Yes, the pair with the given product value k exists in the given matrix
Method #2: Using For loop (User Input)
Approach:
- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
- Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to gvnmatrix using the append() function.
- Give the k value as user input using the int(input()) function and store it in another variable.
- Create a function to say CheckPairwith_prodctK() which takes the given matrix and given product k value as the arguments and returns true or false.
- Inside the function, take a new empty list and store it in another variable.
- Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of columns using another Nested For loop(Inner For loop).
- Check if the value of given k modulus mtrx[n][m] is equal to 0 and given k value divided by mtrx[n[m] is present in the above declared new list using the if conditional statement.
- If it is true, then return true.
- Else append mtrx[n][m] to the above new list using the append() function.
- Pass the given matrix and the given k value as the arguments to the CheckPairwith_prodctK() function and check if it returns true or false using the if conditional statement.
- If it is true, then print “Yes, the pair with the given product value k exists in the given matrix”.
- Else print “No, the pair with the given product value k does not exist in the given matrix”.
- The Exit of the Program.
Below is the implementation:
# Create a function to say CheckPairwith_prodctK() which takes the given matrix and # given product k value as the arguments and returns true or false. def CheckPairwith_prodctK(mtrx, gvn_k_val): # Inside the function, take a new empty list and store it in another variable. new_lst = [] # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of columns using another Nested # For loop(Inner For loop). for m in range(mtrxcols): # Check if the value of given k modulus mtrx[n][m] is equal to 0 and given k # value divided by mtrx[n[m] is present in the above declared new list using # the if # conditional statement. if ((gvn_k_val % mtrx[n][m] == 0) and (gvn_k_val // mtrx[n][m]) in new_lst): # If it is true, then return true. return True # Else append mtrx[n][m] to the above new list using the append() function. else: new_lst.append(mtrx[n][m]) # Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. mtrx = [] # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. mtrx.append(l) # Give the k value as user input using the int(input()) function and # store it in another variable. gvn_k_val = int(input("Enter some random number = ")) # Pass the given matrix and the given k value as the arguments to the # CheckPairwith_prodctK() function and check if it returns true or false using # the if conditional statement. if (CheckPairwith_prodctK(mtrx, gvn_k_val)): # If it is true, then print "Yes, the pair with the given product value k exists in # the given matrix". print("Yes, the pair with the given product value k exists in the given matrix") else: # Else print "No, the pair with the given product value k does not exist in the given # matrix". print("No, the pair with the given product value k does not exist in the given matrix")
Output:
Enter some random number of rows of the matrix = 3 Enter some random number of columns of the matrix = 3 Enter {3} elements of row {1} separated by spaces = 5 2 1 Enter {3} elements of row {2} separated by spaces = 4 6 3 Enter {3} elements of row {3} separated by spaces = 1 7 1 Enter some random number = 50 No, the pair with the given product value k does not exist in the given matrix
Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.