Program to Count Frequency of k in a Matrix of Size n Where Matrix(i, j) = i+j

Python Program to Count Frequency of k in a Matrix of Size n Where Matrix (i, j) = i+j

In the previous article, we have discussed Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix

Given a number n (which is the size of the matrix)and another number k, the task is to find the frequency of k of the matrix with size n and matrix (i,j)=i+j.

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.

Examples:

Example1:

Input:

Given size of matrix = 3
Given k value = 6

Output:

The given k value{ 6 } frequency in the matix =  1

Example2:

Input:

Given size of matrix = 4
Given k value = 3

Output:

The given k value{ 3 } frequency in the matix =  2

Program to Count Frequency of k in a Matrix of Size n Where Matrix(i, j) = i+j in Python

Below are the ways to find the frequency of k of the matrix with size n and matrix (i,j)=i+j in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the number(size of matrix) as static input and store it in a variable.
  • Give another number k as static input and store it in another variable.
  • Create a function to say findfreqencyof_k() which takes the given number(size of matrix) and the given k value as the arguments and returns the frequency of k of the matrix with size n and matrix (i,j)=i+j.
  • Inside the function, check if the given number+1 is greater than or equal to the given k value using the if conditional statement.
  • If it is true, then return given k value -1.
  • Else return (2 * gvn_numb + 1 – gvn_k_val).
  • Pass the given number(size of matrix) and the given k value as the arguments to the findfreqencyof_k() function and store it in another variable.
  • Check if the above result is less than 0 using the if conditional statement.
  • If it is true, then print “The given element k does not exist in the matrix”.
  • Else print the frequency of the given k value.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say findfreqencyof_k() which takes the given number(size of matrix)
# and the given k value as the arguments and returns the frequency of k of the matrix
# with size n and matrix (i,j)=i+j.


def findfreqencyof_k(gvn_numb,  gvn_k_val):
    # Inside the function, check if the given number+1 is greater than or equal to the given
        # k value using the if conditional statement.
    if (gvn_numb + 1 >= gvn_k_val):
      # If it is true, then return given k value -1.
        return (gvn_k_val - 1)
    else:
      # Else return (2 * gvn_numb + 1 - gvn_k_val).
        return (2 * gvn_numb + 1 - gvn_k_val)


# Give the number(size of matrix) as static input and store it in a variable.
gvn_numb = 3
# Give another number k as static input and store it in another variable.
gvn_k_val = 6
# Pass the given number(size of matrix) and the given k value as the arguments to the
# findfreqencyof_k() function and store it in another variable.
rslt = findfreqencyof_k(gvn_numb, gvn_k_val)
# Check if the above result is less than 0 using the if conditional statement.
if (rslt < 0):
  # If it is true, then print "The given element k does not exist in the matrix".
    print("The given element{", gvn_k_val, "} does not exist in the matrix")
else:
  # Else print the frequency of the given k value.
    print(" The given k value{", gvn_k_val,
          "} frequency in the matix = ", rslt)

Output:

The given k value{ 6 } frequency in the matix =  1

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the number(size of matrix) as user input using the int(input()) function and store it in a variable.
  • Give another number k as user input using the int(input()) function and store it in another variable.
  • Create a function to say findfreqencyof_k() which takes the given number(size of matrix) and the given k value as the arguments and returns the frequency of k of the matrix with size n and matrix (i,j)=i+j.
  • Inside the function, check if the given number+1 is greater than or equal to the given k value using the if conditional statement.
  • If it is true, then return given k value -1.
  • Else return (2 * gvn_numb + 1 – gvn_k_val).
  • Pass the given number(size of matrix) and the given k value as the arguments to the findfreqencyof_k() function and store it in another variable.
  • Check if the above result is less than 0 using the if conditional statement.
  • If it is true, then print “The given element k does not exist in the matrix”.
  • Else print the frequency of the given k value.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math

# Create a function to say findfreqencyof_k() which takes the given number(size of matrix)
# and the given k value as the arguments and returns the frequency of k of the matrix
# with size n and matrix (i,j)=i+j.


def findfreqencyof_k(gvn_numb,  gvn_k_val):
    # Inside the function, check if the given number+1 is greater than or equal to the given
        # k value using the if conditional statement.
    if (gvn_numb + 1 >= gvn_k_val):
      # If it is true, then return given k value -1.
        return (gvn_k_val - 1)
    else:
      # Else return (2 * gvn_numb + 1 - gvn_k_val).
        return (2 * gvn_numb + 1 - gvn_k_val)


# Give the number(size of matrix) as user input using the int(input()) function 
# and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Give another number k 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 number(size of matrix) and the given k value as the arguments to the
# findfreqencyof_k() function and store it in another variable.
rslt = findfreqencyof_k(gvn_numb, gvn_k_val)
# Check if the above result is less than 0 using the if conditional statement.
if (rslt < 0):
  # If it is true, then print "The given element k does not exist in the matrix".
    print("The given element{", gvn_k_val, "} does not exist in the matrix")
else:
  # Else print the frequency of the given k value.
    print(" The given k value{", gvn_k_val,
          "} frequency in the matix = ", rslt)

Output:

Enter some random number = 4
Enter some random number = 3
The given k value{ 3 } frequency in the matix = 2

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.