Python Program to find the Next Nearest element in a Matrix
Last Updated :
25 Aug, 2023
Given a matrix, a set of coordinates and an element, the task is to write a python program that can get the coordinates of the elements next occurrence.
Input : test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]], i, j = 1, 3, K = 3
Output : (1, 4)
Explanation : After (1, 3), 3 is found at (1, 4)
Input : test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]], i, j = 2, 3, K = 3
Output : (2, 4)
Explanation : After (2, 3), 3 is found at (2, 4)
Method : Using loop and enumerate()
In this we start iteration from the required coordinates and check just for the next nearest K inside the rectangle formed from row + 1, col + 1 to N, N coordinate. Returns -1, -1 if not occurrence is found.
Example:
Python3
# get Nearest coord.
def near_coord(test_list, x, y, val):
for idx, row in enumerate(test_list[x:]):
for j, ele in enumerate(row):
# checking for value at lower formed rectangle
if ele == val and j > y:
return idx + x, j
# if no index found
return -1, -1
# initializing list
test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3],
[8, 5, 3, 5, 3], [1, 2, 3, 4, 6]]
# printing original list
print("The original list is : " + str(test_list))
# initializing check coord
i, j = 1, 3
# initializing K
K = 3
# getting nearest coordinates
res_abs, res_ord = near_coord(test_list, i, j, K)
# printing result
print("Found K index : " + str((res_abs, res_ord)))
Output:
The original list is : [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]]
Found K index : (1, 4)
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Approach#2: Using generator
Algorithm:
1. The function find_next_nearest
takes a matrix test_list
, a starting position (i, j)
, and a value k
as input.
2. It initializes rows
with the number of rows in the matrix and cols
with the number of columns in the matrix.
3. Inside the generator
nested function, it iterates through the matrix rows from i
to rows - 1
, and for each row, iterates through the columns from j + 1
to cols - 1
.
4. For each cell in this range, it checks if the value at that cell is equal to k
. If it is, it yields the coordinates (row, col)
as a tuple.
5. The yielded coordinates are collected into a list named next_nearest
.
6. The function then returns the first element of next_nearest
if it's not empty (i.e., if a match was found), otherwise, it returns None
.
Python3
def find_next_nearest(matrix, i, j, k):
rows = len(matrix)
cols = len(matrix[0])
def generator():
for row in range(i, rows):
for col in range(j + 1, cols):
if matrix[row][col] == k:
yield (row, col)
next_nearest = list(generator())
return next_nearest[0] if next_nearest else None
test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]]
i, j, k = 1, 3, 3
output = find_next_nearest(test_list, i, j, k)
print(output)
Time Complexity: O((rows - i) * (cols - j - 1)), where rows
is the number of rows in the matrix and cols
is the number of columns in the matrix.
Space complexity: O(rows * cols), where rows
is the number of rows in the matrix and cols
is the number of columns in the matrix.
Similar Reads
Python Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
4 min read
Find index of element in array in python We often need to find the position or index of an element in an array (or list). We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we a
2 min read
Python program to find Successive row difference in Matrix Given a Matrix, the task is to write a Python program to perform differences from the previous row on the basis of the elements present. Input : test_list = [[5, 6, 3, 1], [7, 5, 3, 1], [3, 2], [7, 3, 3, 2], [2, 3], [9, 8, 1]] Output : [[], [7], [2], [7], [], [8, 9, 1]] Explanation : Comparing 1st a
7 min read
Python - Nearest occurrence between two elements in a List Given a list and two elements, x and y find the nearest occurrence index of element x from element y. Input : test_list = [2, 4, 5, 7, 8, 6, 3, 8, 7, 2, 0, 9, 4, 9, 4], x = 4, y = 6 Output : 1 Explanation : 4 is found at 1, 12 and 14th index, 6 is at 5th index, nearest is 1st index.Input : test_list
5 min read
Python program to search for the minimum element occurring consecutively n times in a matrix Given a matrix containing n rows. Each row has an equal number of m elements. The task is to find elements that come consecutively n times horizontally, vertically, diagonally in the matrix. If there are multiple such elements then print the smallest element. If there is no such element then print -
4 min read