{"id":28334,"date":"2017-12-24T16:07:41","date_gmt":"2017-12-24T10:37:41","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=28334"},"modified":"2017-12-24T16:07:41","modified_gmt":"2017-12-24T10:37:41","slug":"find-specific-pair-matrix","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-specific-pair-matrix\/","title":{"rendered":"C++ Programming-Find a specific pair in Matrix"},"content":{"rendered":"<p>Given an n x n matrix mat[n][n] of integers, find the maximum value of mat(c, d) \u2013 mat(a, b) over all choices of indexes such that both c &gt; a and d &gt; b.<\/p>\n<p>Example:<\/p>\n<pre>Input:\r\nmat[N][N] = {{ 1, 2, -1, -4, -20 },\r\n             {<strong> -8<\/strong>, -3, 4, 2, 1 }, \r\n             { 3, 8, 6, 1, 3 },\r\n             { -4, -1, 1, 7, -6 },\r\n             { 0, -4, <strong>10<\/strong>, -5, 1 }};\r\nOutput: 18\r\nThe maximum value is 18 as mat[4][2] \r\n- mat[1][0] = 18 has maximum difference.<\/pre>\n<p>The program should do only ONE traversal of the matrix. i.e. expected time complexity is O(n<sup>2<\/sup>)<\/p>\n<p>A <strong>simple solution<\/strong> would be to apply Brute-Force. For all values mat(a, b) in the matrix, we find mat(c, d) that has maximum value such that c &gt; a and d &gt; b and keeps on updating maximum value found so far. We finally return the maximum value.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A Naive method to find maximum value of mat1[d]<br\/>\/\/ - ma[a][b] such that c &gt; a and d &gt; b<br\/>#include &lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/>#define N 5<br\/> <br\/>\/\/ The function returns maximum value A(c,d) - A(a,b)<br\/>\/\/ over all choices of indexes such that both c &gt; a<br\/>\/\/ and d &gt; b.<br\/>int findMaxValue(int mat[][N])<br\/>{<br\/>    \/\/ stores maximum value<br\/>    int maxValue = INT_MIN;<br\/> <br\/>    \/\/ Consider all possible pairs mat[a][b] and<br\/>    \/\/ mat1[d]<br\/>    for (int a = 0; a &lt; N - 1; a++)<br\/>      for (int b = 0; b &lt; N - 1; b++)<br\/>         for (int c = a + 1; c &lt; N; c++)<br\/>           for (int d = b + 1; d &lt; N; d++)<br\/>              if (maxValue &lt; (mat1[d] - mat[a][b]))<br\/>                  maxValue = mat1[d] - mat[a][b];<br\/> <br\/>    return maxValue;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>   int mat[N][N] = {<br\/>                  { 1, 2, -1, -4, -20 },<br\/>                  { -8, -3, 4, 2, 1 },<br\/>                  { 3, 8, 6, 1, 3 },<br\/>                  { -4, -1, 1, 7, -6 },<br\/>                  { 0, -4, 10, -5, 1 }<br\/>               };<br\/>    cout &lt;&lt; &quot;Maximum Value is &quot;<br\/>         &lt;&lt; findMaxValue(mat);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>Maximum Value is 18\r\n\r\n<\/pre>\n<p>The above program runs in O(n^4) time which is nowhere close to expected time complexity of O(n^2)<\/p>\n<p>An <strong>efficient solution<\/strong> uses extra space. We pre-process the matrix such that index(i, j) stores max of elements in matrix from (i, j) to (N-1, N-1) and in the process keeps on updating maximum value found so far. We finally return the maximum value.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Program<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ An efficient method to find maximum value of mat1[d]<br\/>\/\/ - ma[a][b] such that c &gt; a and d &gt; b<br\/>#include &lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/>#define N 5<br\/> <br\/>\/\/ The function returns maximum value A(c,d) - A(a,b)<br\/>\/\/ over all choices of indexes such that both c &gt; a<br\/>\/\/ and d &gt; b.<br\/>int findMaxValue(int mat[][N])<br\/>{<br\/>    \/\/stores maximum value<br\/>    int maxValue = INT_MIN;<br\/> <br\/>    \/\/ maxArr[i][j] stores max of elements in matrix<br\/>    \/\/ from (i, j) to (N-1, N-1)<br\/>    int maxArr[N][N];<br\/> <br\/>    \/\/ last element of maxArr will be same&#039;s as of<br\/>    \/\/ the input matrix<br\/>    maxArr[N-1][N-1] = mat[N-1][N-1];<br\/> <br\/>    \/\/ preprocess last row<br\/>    int maxv = mat[N-1][N-1];  \/\/ Initialize max<br\/>    for (int j = N - 2; j &gt;= 0; j--)<br\/>    {<br\/>        if (mat[N-1][j] &gt; maxv)<br\/>            maxv = mat[N - 1][j];<br\/>        maxArr[N-1][j] = maxv;<br\/>    }<br\/> <br\/>    \/\/ preprocess last column<br\/>    maxv = mat[N - 1][N - 1];  \/\/ Initialize max<br\/>    for (int i = N - 2; i &gt;= 0; i--)<br\/>    {<br\/>        if (mat[i][N - 1] &gt; maxv)<br\/>            maxv = mat[i][N - 1];<br\/>        maxArr[i][N - 1] = maxv;<br\/>    }<br\/> <br\/>    \/\/ preprocess rest of the matrix from bottom<br\/>    for (int i = N-2; i &gt;= 0; i--)<br\/>    {<br\/>        for (int j = N-2; j &gt;= 0; j--)<br\/>        {<br\/>            \/\/ Update maxValue<br\/>            if (maxArr[i+1][j+1] - mat[i][j] &gt;<br\/>                                            maxValue)<br\/>                maxValue = maxArr[i + 1][j + 1] - mat[i][j];<br\/> <br\/>            \/\/ set maxArr (i, j)<br\/>            maxArr[i][j] = max(mat[i][j],<br\/>                               max(maxArr[i][j + 1],<br\/>                                   maxArr[i + 1][j]) );<br\/>        }<br\/>    }<br\/> <br\/>    return maxValue;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int mat[N][N] = {<br\/>                      { 1, 2, -1, -4, -20 },<br\/>                      { -8, -3, 4, 2, 1 },<br\/>                      { 3, 8, 6, 1, 3 },<br\/>                      { -4, -1, 1, 7, -6 },<br\/>                      { 0, -4, 10, -5, 1 }<br\/>                    };<br\/>    cout &lt;&lt; &quot;Maximum Value is &quot;<br\/>         &lt;&lt; findMaxValue(mat);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Output:<\/p>\n<pre>Maximum Value is 18<\/pre>\n<p>If we are allowed to modify of the matrix, we can avoid using extra space and use input matrix instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming-Find a specific pair in Matrix-Matrix<br \/>\nGiven an n x n matrix mat[n][n] of integers, find the maximum value of mat(c, d) <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,82291],"tags":[83733,83729,83730,82475,83726,83725,83728,83727,83732,83731],"class_list":["post-28334","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-matrix","tag-applications-of-c-programming","tag-c-program-to-find-closest-pair-of-points-in-an-array","tag-computer-science-with-c-programming","tag-count-all-distinct-pairs-with-difference-equal-to-k","tag-find-a-pair-with-the-given-difference","tag-find-a-specific-pair-in-matrix","tag-find-all-symmetric-pairs-in-it","tag-given-an-array-of-pairs","tag-matrices-and-c-code","tag-vectors"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=28334"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/28334\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=28334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=28334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=28334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}