Find a Specific Pair in Matrix using JavaScript Last Updated : 05 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In this problem, we are given a 2-D matrix and we have to find the maximum value of ‘MAT[a][b]’ - ‘MAT[c][d]’ over all possible indices (0 <= ‘a’, ‘b’, ‘c’, ‘d’ < ‘N’) such that . ‘a’ > ‘c’ and ‘b’ > ‘d’. Below is an example to understand the problem statement clearly Example: Input [ [1 , 2 , 3] , [4 , 5 , 6], [7, 8 , 9] ] Output:8Below are different approaches to Find a specific pair in Matrix using JavaScript: Table of Content Brute force ApproachOptimal ApproachBrute force ApproachWe will initialize a variable answer with a value of negative infinity to store the maximum difference found in matrix. Now we iterate through each combination in matrix using nested loop making sure that 'a' > 'c' and 'b' > 'd'. Update answer with the maximum difference found using Math.max(). Return answer as result. Example: To demonstrate finding a specific pair in Matrix using brute force approach JavaScript function maxDifference(matrix) { const N = matrix.length; let answer = -Infinity; for (let a = 0; a < N; a++) { for (let b = 0; b < N; b++) { for (let c = 0; c < a; c++) { for (let d = 0; d < b; d++) { answer = Math .max(answer, matrix[a][b] - matrix[c][d]); } } } } return answer; } const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const result = maxDifference(matrix); console.log("Maximum difference using Linear approach is ", result); OutputMaximum difference using Linear approach is 8 Time Complexity : O(N^4) Space Complexity: O(1) Optimal ApproachDefine a function that takes a 2D array mat. Initialize answer to store the maximum difference and maxArr to store maximum values from each position to the bottom-right corner. Preprocess the last row and column of maxArr, then traverse the matrix from bottom to top and right to left, updating answer and maxArr. Print the result stored in answer. Example: To demonstrate finding a specific pair in Matrix using optimal approach JavaScript function maxDifference(mat) { const N = mat.length; let answer = Number.MIN_SAFE_INTEGER; const maxArr = new Array(N) .fill(null).map(() => new Array(N)); maxArr[N - 1][N - 1] = mat[N - 1][N - 1]; let maxv = mat[N - 1][N - 1]; for (let j = N - 2; j >= 0; j--) { if (mat[N - 1][j] > maxv) maxv = mat[N - 1][j]; maxArr[N - 1][j] = maxv; } maxv = mat[N - 1][N - 1]; for (let i = N - 2; i >= 0; i--) { if (mat[i][N - 1] > maxv) maxv = mat[i][N - 1]; maxArr[i][N - 1] = maxv; } for (let i = N - 2; i >= 0; i--) { for (let j = N - 2; j >= 0; j--) { if (maxArr[i + 1][j + 1] - mat[i][j] > answer) answer = maxArr[i + 1][j + 1] - mat[i][j]; maxArr[i][j] = Math .max(mat[i][j], Math .max(maxArr[i][j + 1], maxArr[i + 1][j])); } } return answer; } const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; const result = maxDifference(matrix); console.log("Maximum difference using optimal approach is ", result); OutputMaximum difference using optimal approach is 8 Time Complexity: O(N^2) Space Complexity: O(N^2) Comment More infoAdvertise with us Next Article Find a Specific Pair in Matrix using JavaScript bug8wdqo Follow Improve Article Tags : JavaScript Web Technologies JavaScript Programs Similar Reads Javascript Program to check if a matrix is symmetric A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row.Examples: Input : 1 2 3 2 1 4 3 4 3 Output : Yes Input : 3 5 8 3 4 7 8 5 3 Output : NoA Simple solution is to do follo 2 min read Find Common Elements in all Rows of a Given Matrix using JavaScript Matrix manipulation takes place in a large number of applications, and the detection of shared elements among rows is one of the common challenges. Example: Input : [1, 2, 3], [2, 3, 4], [3, 4, 5]Output: [3]Input : [1, 2, 3], [4, 5, 6], [7, 8, 9]Output: [ ]There are the following approaches for find 3 min read Print Boundary Elements of a Matrix in JavaScript Matrixes are basic data structures that are frequently used to represent grids or tables of numbers. Gathering and showing the elements that are positioned along a matrix's edges is the process of printing the matrix's boundary elements. There are several approaches in JavaScript to print the bounda 3 min read Javascript Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix 3 min read How to find if an array contains a specific string in JavaScript/jQuery ? In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array 5 min read Javascript Program to Print matrix in snake pattern Given n x n matrix In the given matrix, you have to print the elements of the matrix in the snake pattern.Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5 2 min read Check if Pair with given Sum Exists in Array using JavaScript We will check if a pair with a given Sum exists in an Array or not using JavaScript. 2Sum ProblemThe 2sum is a famous problem asked in most interviews. We have to check whether a pair of numbers exists whose sum is equal to the target value. We are given an array of integers and the target value. We 4 min read Javascript Program to Find a pair with the given difference Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78Output: Pair Found: (2, 80)Input: arr[] = {90, 70, 20, 80, 50}, n = 45Output: No Such PairNative Approach:The simplest method is t 4 min read Set of pairs of numbers in JavaScript To Access all sets of pairs of Numbers in an array, We need to access all possible pairs of numbers and need to apply conditions and other operations. In this article, we will see how to access a pair of numbers in JavaScript using different approaches. The first approach is Nested Loop and the seco 2 min read Javascript Program to check idempotent matrix Given an N * N matrix and the task is to check matrix is an idempotent matrix or not.Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the same matrix. The matrix M is said to be an idempotent matrix if and only if M * M = M. In an idempotent m 2 min read Like