Largest integer that can be placed at center of given square Matrix to maximise arithmetic progressions
Given a N x N matrix, such that the element at the index [N/2, N/2] is missing, the task is to find the maximum integer that can be placed at index [N/2, N/2] such that the count of arithmetic progressions over all rows, columns, and diagonals is maximized.
Example:
Input: mat[][]={{3, 4, 11}, {10, ?, 9}, {-1, 6, 7}}
Output: 5
Explanation: The maximum integer that can be placed on [1, 1] is 5. Hence, the AP’s formed is are:
- Top left diagonal: 3, 5, 7.
- Top right diagonal: −1, 5, 1.
- Middle column: 4, 5, 6.
- Right column: 11, 9, 7.
Therefore, the number of AP formed is 4 which is the maximum possible.
Input: mat[][]={{2, 2, 11}, {1, ?, 7}, {-1, 6, 6}}
Output: 4
Approach: The given problem can be solved by finding all the possible numbers that can be placed on the index [N/2, N/2] such that it forms an Arithmetic progression over any row, column, or diagonal of the given matrix and keeping track of the largest integer out of them which forms the maximum AP’s.
Let’s suppose the matrix is of 3*3 size.
Now, the missing element is (1, 1).
So, for each row, column and diagonal, three numbers are present. Say, these three numbers are A, B, C and B is missing.So, it can be observed that for integers A, B, and C to form and an AP,
B must be equal to [A + (C – A)]/2.Hence, for any value of A and C, B can be calculated using the above-discussed formula. Also, if (C – A) is odd then no integer value exists for B.
So, apply this formula to over row, column and diagonal and find the maximum element which will form the maximum element.
The same approach can be applied to N*N matrix.
Below is the implementation of the above approach:
C++
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum value // of the missing integer such that the // count of AP's formed is maximized int findMissing(vector<vector< int > >& mat) { int N = mat.size(); // Stores the occurrence of each // possible integer value unordered_map< int , int > mp; // For 1st Row int t = abs (mat[N / 2][N / 2 + 1] - mat[N / 2][N / 2 - 1]); int A = min(mat[N / 2][N / 2 + 1], mat[N / 2][N / 2 - 1]); if (t % 2 == 0) { mp[A + t / 2] += 1; } // For 1st Col t = abs (mat[N / 2 + 1][N / 2] - mat[N / 2][N / 2]); A = min(mat[N / 2 + 1][N / 2], mat[N / 2][N / 2]); if (t % 2 == 0) { mp[A + t / 2] += 1; } // For Left Diagonal t = abs (mat[N / 2 + 1][N / 2 + 1] - mat[N / 2 - 1][N / 2 - 1]); A = min(mat[N / 2 + 1][N / 2 + 1], mat[N / 2 - 1][N / 2 - 1]); if (t % 2 == 0) { mp[A + t / 2] += 1; } // For Right Diagonal t = abs (mat[N / 2 - 1][N / 2 + 1] - mat[N / 2 + 1][N / 2 - 1]); A = min(mat[N / 2 - 1][N / 2 + 1], mat[N / 2 + 1][N / 2 - 1]); if (t % 2 == 0) { mp[A + t / 2] += 1; } int ans = -1, occur = 0; // Loop to find the largest integer // with maximum count for ( auto x : mp) { if (occur < x.second) { ans = x.first; } if (occur == x.second) { ans = max(ans, x.first); } } // Return Answer return ans; } // Driver Code int main() { vector<vector< int > > mat = { { 3, 4, 11 }, { 10, INT_MAX, 9 }, { -1, 6, 7 } }; cout << findMissing(mat); } |
Java
// Java code for the above approach import java.util.*; class GFG { // Function to find the maximum value // of the missing integer such that the // count of AP's formed is maximized static int findMissing( int [][] mat) { int N = mat.length; // Stores the occurrence of each // possible integer value HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); // For 1st Row int t = Math.abs(mat[N / 2 ][N / 2 + 1 ] - mat[N / 2 ][N / 2 - 1 ]); int A = Math.min(mat[N / 2 ][N / 2 + 1 ], mat[N / 2 ][N / 2 - 1 ]); if (t % 2 == 0 ) { if (mp.containsKey(A + t / 2 )) { mp.put(A + t / 2 , mp.get(A + t / 2 ) + 1 ); } else { mp.put(A + t / 2 , 1 ); } } // For 1st Col t = Math.abs(mat[N / 2 + 1 ][N / 2 ] - mat[N / 2 ][N / 2 ]); A = Math.min(mat[N / 2 + 1 ][N / 2 ], mat[N / 2 ][N / 2 ]); if (t % 2 == 0 ) { if (mp.containsKey(A + t / 2 )) { mp.put(A + t / 2 , mp.get(A + t / 2 ) + 1 ); } else { mp.put(A + t / 2 , 1 ); } } // For Left Diagonal t = Math.abs(mat[N / 2 + 1 ][N / 2 + 1 ] - mat[N / 2 - 1 ][N / 2 - 1 ]); A = Math.min(mat[N / 2 + 1 ][N / 2 + 1 ], mat[N / 2 - 1 ][N / 2 - 1 ]); if (t % 2 == 0 ) { if (mp.containsKey(A + t / 2 )) { mp.put(A + t / 2 , mp.get(A + t / 2 ) + 1 ); } else { mp.put(A + t / 2 , 1 ); } } // For Right Diagonal t = Math.abs(mat[N / 2 - 1 ][N / 2 + 1 ] - mat[N / 2 + 1 ][N / 2 - 1 ]); A = Math.min(mat[N / 2 - 1 ][N / 2 + 1 ], mat[N / 2 + 1 ][N / 2 - 1 ]); if (t % 2 == 0 ) { if (mp.containsKey(A + t / 2 )) { mp.put(A + t / 2 , mp.get(A + t / 2 ) + 1 ); } else { mp.put(A + t / 2 , 1 ); } } int ans = - 1 , occur = 0 ; // Loop to find the largest integer // with maximum count for (Map.Entry<Integer, Integer> x : mp.entrySet()) { if (occur < x.getValue()) { ans = x.getKey(); } if (occur == x.getValue()) { ans = Math.max(ans, x.getKey()); } } // Return Answer return ans; } // Driver Code public static void main(String[] args) { int [][] mat = { { 3 , 4 , 11 }, { 10 , Integer.MAX_VALUE, 9 }, { - 1 , 6 , 7 } }; System.out.print(findMissing(mat)); } } // This code is contributed by shikhasingrajput |
Python3
# Python 3 code for the above approach from collections import defaultdict import sys # Function to find the maximum value # of the missing integer such that the # count of AP's formed is maximized def findMissing(mat): N = len (mat) # Stores the occurrence of each # possible integer value mp = defaultdict( int ) # For 1st Row t = abs (mat[N / / 2 ][N / / 2 + 1 ] - mat[N / / 2 ][N / / 2 - 1 ]) A = min (mat[N / / 2 ][N / / 2 + 1 ], mat[N / / 2 ][N / / 2 - 1 ]) if (t % 2 = = 0 ): mp[A + t / / 2 ] + = 1 # For 1st Col t = abs (mat[N / / 2 + 1 ][N / / 2 ] - mat[N / / 2 ][N / / 2 ]) A = min (mat[N / / 2 + 1 ][N / / 2 ], mat[N / / 2 ][N / / 2 ]) if (t % 2 = = 0 ): mp[A + t / / 2 ] + = 1 # For Left Diagonal t = abs (mat[N / / 2 + 1 ][N / / 2 + 1 ] - mat[N / / 2 - 1 ][N / / 2 - 1 ]) A = min (mat[N / / 2 + 1 ][N / / 2 + 1 ], mat[N / / 2 - 1 ][N / / 2 - 1 ]) if (t % 2 = = 0 ): mp[A + t / / 2 ] + = 1 # For Right Diagonal t = abs (mat[N / / 2 - 1 ][N / / 2 + 1 ] - mat[N / / 2 + 1 ][N / / 2 - 1 ]) A = min (mat[N / / 2 - 1 ][N / / 2 + 1 ], mat[N / / 2 + 1 ][N / / 2 - 1 ]) if (t % 2 = = 0 ): mp[A + t / / 2 ] + = 1 ans = - 1 occur = 0 # Loop to find the largest integer # with maximum count for x in mp: if (occur < mp[x]): ans = x if (occur = = mp[x]): ans = max (ans, x) # Return Answer return ans # Driver Code if __name__ = = "__main__" : mat = [[ 3 , 4 , 11 ], [ 10 , sys.maxsize, 9 ], [ - 1 , 6 , 7 ]] print (findMissing(mat)) # This code is contributed by ukasp. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { static int INT_MAX = 2147483647; // Function to find the maximum value // of the missing integer such that the // count of AP's formed is maximized static int findMissing( int [,]mat) { int N = mat.GetLength(0); // Stores the occurrence of each // possible integer value Dictionary< int , int > mp = new Dictionary< int , int >(); // For 1st Row int t = Math.Abs(mat[N / 2, N / 2 + 1] - mat[N / 2, N / 2 - 1]); int A = Math.Min(mat[N / 2, N / 2 + 1], mat[N / 2, N / 2 - 1]); if (t % 2 == 0) { if (mp.ContainsKey(A + t / 2)) { mp[A + t / 2] = mp[A + t / 2] + 1; } else { mp.Add(A + t / 2, 1); } } // For 1st Col t = Math.Abs(mat[N / 2 + 1, N / 2] - mat[N / 2, N / 2]); A = Math.Min(mat[N / 2 + 1, N / 2], mat[N / 2, N / 2]); if (t % 2 == 0) { if (mp.ContainsKey(A + t / 2)) { mp[A + t / 2] = mp[A + t / 2] + 1; } else { mp.Add(A + t / 2, 1); } } // For Left Diagonal t = Math.Abs(mat[N / 2 + 1, N / 2 + 1] - mat[N / 2 - 1, N / 2 - 1]); A = Math.Min(mat[N / 2 + 1, N / 2 + 1], mat[N / 2 - 1, N / 2 - 1]); if (t % 2 == 0) { if (mp.ContainsKey(A + t / 2)) { mp[A + t / 2] = mp[A + t / 2] + 1; } else { mp.Add(A + t / 2, 1); } } // For Right Diagonal t = Math.Abs(mat[N / 2 - 1, N / 2 + 1] - mat[N / 2 + 1, N / 2 - 1]); A = Math.Min(mat[N / 2 - 1, N / 2 + 1], mat[N / 2 + 1, N / 2 - 1]); if (t % 2 == 0) { if (mp.ContainsKey(A + t / 2)) { mp[A + t / 2] = mp[A + t / 2] + 1; } else { mp.Add(A + t / 2, 1); } } int ans = -1, occur = 0; // Loop to find the largest integer // with maximum count foreach (KeyValuePair< int , int > x in mp) { if (occur < x.Value) { ans = x.Key; } if (occur == x.Value) { ans = Math.Max(ans, x.Value); } } // Return Answer return ans; } // Driver Code public static void Main() { int [,]mat = { { 3, 4, 11 }, { 10, INT_MAX, 9 }, { -1, 6, 7 } }; Console.Write(findMissing(mat)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to find the maximum value // of the missing integer such that the // count of AP's formed is maximized function findMissing(mat) { let N = mat.length; // Stores the occurrence of each // possible integer value let mp = new Map(); // For 1st Row let t = Math.abs(mat[Math.floor(N / 2)][Math.floor(N / 2) + 1] - mat[Math.floor(N / 2)][Math.floor(N / 2) - 1]); let A = Math.min(mat[Math.floor(N / 2)][Math.floor(N / 2) + 1], mat[Math.floor(N / 2)][Math.floor(N / 2) - 1]); if (t % 2 == 0) { if (mp.has(A + Math.floor(t / 2))) mp.set(A + Math.floor(t / 2), mp.get(A + Math.floor(t / 2)) + 1); else mp.set(A + Math.floor(t / 2), 1); } // For 1st Col t = Math.abs(mat[Math.floor(N / 2) + 1][Math.floor(N / 2)] - mat[Math.floor(N / 2)][Math.floor(N / 2)]); A = Math.min(mat[Math.floor(N / 2) + 1][Math.floor(N / 2)], mat[Math.floor(N / 2)][Math.floor(N / 2)]); if (t % 2 == 0) { if (mp.has(A + Math.floor(t / 2))) mp.set(A + Math.floor(t / 2), mp.get(A + Math.floor(t / 2)) + 1); else mp.set(A + Math.floor(t / 2), 1); } // For Left Diagonal t = Math.abs(mat[Math.floor(N / 2) + 1][Math.floor(N / 2) + 1] - mat[Math.floor(N / 2) - 1][Math.floor(N / 2) - 1]); A = Math.min(mat[Math.floor(N / 2) + 1][Math.floor(N / 2) + 1], mat[Math.floor(N / 2) - 1][Math.floor(N / 2) - 1]); if (t % 2 == 0) { if (mp.has(A + Math.floor(t / 2))) mp.set(A + Math.floor(t / 2), mp.get(A + Math.floor(t / 2)) + 1); else mp.set(A + Math.floor(t / 2), 1); } // For Right Diagonal t = Math.abs(mat[Math.floor(N / 2) - 1][Math.floor(N / 2) + 1] - mat[Math.floor(N / 2) + 1][Math.floor(N / 2) - 1]); A = Math.min(mat[Math.floor(N / 2) - 1][Math.floor(N / 2) + 1], mat[Math.floor(N / 2) + 1][Math.floor(N / 2) - 1]); if (t % 2 == 0) { if (mp.has(A + Math.floor(t / 2))) mp.set(A + Math.floor(t / 2), mp.get(A + Math.floor(t / 2)) + 1); else mp.set(A + Math.floor(t / 2), 1); } let ans = -1, occur = 0; // Loop to find the largest integer // with maximum count for (let [key, val] of mp) { if (occur < val) { ans = key; } if (occur == val) { ans = Math.max(ans, key); } } // Return Answer return ans; } // Driver Code let mat = [[3, 4, 11], [10, Number.MAX_VALUE, 9], [-1, 6, 7]]; document.write(findMissing(mat)) // This code is contributed by Potta Lokesh </script> |
5
Time Complexity: O(1)
Auxiliary Space: O(1)