📜  可以放置在给定方阵中心以最大化算术级数的最大整数

📅  最后修改于: 2022-05-13 01:56:06.044000             🧑  作者: Mango

可以放置在给定方阵中心以最大化算术级数的最大整数

给定一个N x N矩阵,使得索引[N/2, N/2]处的元素丢失,任务是找到可以放在索引[N/2, N/2]处的最大整数,例如使所有行、列和对角线上的算术级数数最大化。

例子:

方法:给定的问题可以通过找到可以放在索引[N/2, N/2]上的所有可能的数字来解决,使得它在给定矩阵的任何行、列或对角线上形成算术级数,并且跟踪其中最大的整数,形成最大的 AP

相同的方法可以应用于N*N矩阵。

下面是上述方法的实现:

C++
// C++ code for the above approach
#include 
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 >& mat)
{
    int N = mat.size();
 
    // Stores the occurence of each
    // possible integer value
    unordered_map 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 > 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 occurence of each
    // possible integer value
    HashMap mp = new HashMap();
 
    // 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 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 occurence 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 occurence of each
    // possible integer value
    Dictionary mp =
           new Dictionary();
 
    // 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 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



输出
5

时间复杂度: O(1)
辅助空间: O(1)