
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find k where given matrix has k by k square of same value in C++
Suppose we have a 2d matrix, we have to find the largest k × k submatrix where all of its elements are containing the same value, then find the value of k.
So, if the input is like
1 | 1 | 8 | 3 |
1 | 5 | 5 | 5 |
2 | 5 | 5 | 5 |
4 | 5 | 5 | 5 |
then the output will be 3, as there is a 3 × 3 square matrix of value 5.
To solve this, we will follow these steps −
n := row count of matrix
m := column count of matrix
Define one 2D array dp of size (n x m) and fill with 1
ret := 1
for initialize i := n - 1, when i >= 0, update (decrease i by 1), do −
for initialize j := m - 1, when j >= 0, update (decrease j by 1), do −
val := inf
if i + 1 < n and v[i + 1, j] is same as v[i, j], then −
val := minimum of dp[i + 1, j] and val
Otherwise
val := 0
if j + 1 < m and v[i, j + 1] is same as v[i, j], then −
val := minimum of dp[i, j + 1] and val
Otherwise
val := 0
if i + 1 < n and j + 1 < m and v[i + 1, j + 1] is same as v[i, j], then −
val := minimum of dp[i + 1, j + 1] and val
Otherwise
val := 0
if val is same as inf, then −
Ignore following part, skip to the next iteration
dp[i, j] := dp[i, j] + val
ret := maximum of ret and dp[i, j]
return ret
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int solve(vector<vector<int>>& v) { int n = v.size(); int m = v[0].size(); vector<vector<int>> dp(n, vector<int>(m, 1)); int ret = 1; for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { int val = INT_MAX; if (i + 1 < n && v[i + 1][j] == v[i][j]) { val = min(dp[i + 1][j], val); } else { val = 0; } if (j + 1 < m && v[i][j + 1] == v[i][j]) { val = min(dp[i][j + 1], val); } else { val = 0; } if (i + 1 < n && j + 1 < m && v[i + 1][j + 1] == v[i][j]) { val = min(dp[i + 1][j + 1], val); } else { val = 0; } if (val == INT_MAX) continue; dp[i][j] += val; ret = max(ret, dp[i][j]); } } return ret; } }; int solve(vector<vector<int>>& matrix) { return (new Solution())->solve(matrix); } int main(){ vector<vector<int>> matrix = { {1, 1, 8, 3}, {1, 5, 5, 5}, {2, 5, 5, 5}, {4, 5, 5, 5} }; cout << solve(matrix); }
Input
{ {1, 1, 8, 3}, {1, 5, 5, 5}, {2, 5, 5, 5}, {4, 5, 5, 5} };
Output
3
- Related Articles
- Program to find k where k elements have value at least k in Python
- Program to find value of K for K-Similar Strings in C++
- Program to find smallest value of K for K-Similar Strings in Python
- Find the value of k in the problem given belowArea of the triangle formed by points(k,0).(-1.2)and (4,3)is 12units2 then find k
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find the value of $k$ for which the following system of equations has infinitely many solution: $2x\ +\ 3y\ =\ k$$(k\ -\ 1)x\ +\ (k\ +\ 2)y\ =\ 3k$
- Find the value (s) of $k$ for which the points $(3k – 1, k – 2), (k, k – 7)$ and $(k – 1, -k – 2)$ are collinear.
- Find the value of $k$, if $3k\times k=64$.
- Find the value of $k$ for which the equation $x^{2}+k( 2x+k-1)+2=0$ has real and equal roots.
- Find k closest elements to a given value in C++
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Minimum Possible value of |ai + aj – k| for given array and k in C++
- Find the largest possible value of \( r \) less than 100 for which\[\frac{k}{9}+\frac{k}{10}=r\]where \( r, k \) are positive integers.
- Program to find maximum length of k ribbons of same length in Python
- For the following system of equation determine the value of $k$ for which the given system of equations has unique solution. $kx+3y=( k-3)$ and $12x+ky=k$.
