📌  相关文章
📜  矩阵中等于 X 的单元格计数构造为行和列的总和

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

矩阵中等于 X 的单元格计数构造为行和列的总和

给定整数N表示方阵的大小,其每个元素是其行和列的总和,即mat[i][j] = i + j (0 < i, j, ≤ N) 和整数X ,任务是找出有多少单元格具有值X

例子:

天真的方法:最简单的方法是创建矩阵并计算具有值X的单元格。

下面是上述方法的实现。

C++
// C++ code of the above approach.
 
#include 
using namespace std;
 
// Function to find the number of times
// X is present in the matrix
long long int solve(long long int n,
                    long long int x)
{
    long long int total = 0;
 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
 
            if (i + j == x)
                total++;
    }
 
    return total;
}
 
// Driver code
int main()
{
 
    int N = 4;
    int X = 2;
 
    cout << solve(N, X);
    return 0;
}


Java
// Java code of the above approach.
import java.io.*;
 
class GFG {
 
  // Function to find the number of times
  // X is present in the matrix
  static long  solve(long  n,
                     long  x)
  {
    long  total = 0;
 
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++)
 
        if (i + j == x)
          total++;
    }
 
    return total;
  }
 
  // Driver code
  public static void main (String[] args) {
    int N = 4;
    int X = 2;
 
    System.out.println(solve(N, X));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 code of the above approach.
 
# Function to find the number of times
# X is present in the matrix
def solve(n, x):
    total = 0;
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            if (i + j == x):
                total += 1;
    return total;
 
# Driver code
if __name__ == '__main__':
    N = 4;
    X = 2;
 
    print(solve(N, X));
 
# This code is contributed by gauravrajput1


C#
// C# code of the above approach.
using System;
 
class GFG {
 
    // Function to find the number of times
    // X is present in the matrix
    static long solve(long n, long x)
    {
        long total = 0;
 
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++)
 
                if (i + j == x)
                    total++;
        }
 
        return total;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 4;
        int X = 2;
 
        Console.WriteLine(solve(N, X));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ code for the above approach.
 
#include 
using namespace std;
 
// Function to find
// the number of occurrences of X
long long int solve(long long int N,
                    long long int X)
{
    if (X == 0 || X > 2 * N)
        return 0;
 
    if (X <= N + 1)
        return X - 1;
    else
        return N + N + 1 - X;
}
 
// Driver code
int main()
{
 
    int N = 4;
    int X = 2;
 
    cout << solve(N, X);
    return 0;
}


Java
// Java Program of the above approach.
import java.util.*;
class GFG {
 
  // Function to find
  // the number of occurrences of X
  static int solve(int N, int X)
  {
    if (X == 0 || X > 2 * N)
      return 0;
 
    if (X <= N + 1)
      return X - 1;
    else
      return N + N + 1 - X;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 4;
    int X = 2;
 
    System.out.print(solve(N, X));
  }
}
 
// This code is contributed by code_hunt.


C#
// C# Program of the above approach.
using System;
 
class GFG {
 
    // Function to find
    // the number of occurrences of X
    static int solve(int N, int X)
    {
        if (X == 0 || X > 2 * N)
            return 0;
 
        if (X <= N + 1)
            return X - 1;
        else
            return N + N + 1 - X;
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int X = 2;
 
        Console.Write(solve(N, X));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
1

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

有效方法:可以使用以下数学观察有效地解决问题:

按照下面提到的步骤来实现上述观察:

  • 检查 X 是否大于 N+1。
  • 然后使用上面推导的公式。

下面是上述方法的实现。

C++

// C++ code for the above approach.
 
#include 
using namespace std;
 
// Function to find
// the number of occurrences of X
long long int solve(long long int N,
                    long long int X)
{
    if (X == 0 || X > 2 * N)
        return 0;
 
    if (X <= N + 1)
        return X - 1;
    else
        return N + N + 1 - X;
}
 
// Driver code
int main()
{
 
    int N = 4;
    int X = 2;
 
    cout << solve(N, X);
    return 0;
}

Java

// Java Program of the above approach.
import java.util.*;
class GFG {
 
  // Function to find
  // the number of occurrences of X
  static int solve(int N, int X)
  {
    if (X == 0 || X > 2 * N)
      return 0;
 
    if (X <= N + 1)
      return X - 1;
    else
      return N + N + 1 - X;
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int N = 4;
    int X = 2;
 
    System.out.print(solve(N, X));
  }
}
 
// This code is contributed by code_hunt.

C#

// C# Program of the above approach.
using System;
 
class GFG {
 
    // Function to find
    // the number of occurrences of X
    static int solve(int N, int X)
    {
        if (X == 0 || X > 2 * N)
            return 0;
 
        if (X <= N + 1)
            return X - 1;
        else
            return N + N + 1 - X;
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 4;
        int X = 2;
 
        Console.Write(solve(N, X));
    }
}
 
// This code is contributed by ukasp.

Javascript



输出
1

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