📜  查找范围 [1, N*M+1] 中缺失的数字,表示为大小为 N*M 的矩阵

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

查找范围 [1, N*M+1] 中缺失的数字,表示为大小为 N*M 的矩阵

给定一个N x M矩阵mat[][] ,其中所有元素都是从 1 开始的自然数,并且除了 1 个元素之外是连续的,找到该元素。

例子

方法:可以通过检查矩阵每一行的最后一个元素的 M 的倍数来解决问题,如果不匹配,则缺失的元素在该行中。应用线性搜索并找到它。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
#define Max 1000
 
// Function to return Missing
// Element from matrix Mat
int check(int Mat[][Max], int N, int M)
{
    // Edge Case
    if (Mat[0][0] != 1)
        return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0][0] + M - 1;
 
    for (int i = 0; i < N; i++) {
 
        // If last element of respective row
        // matches respective multiples of X
        if (Mat[i][M - 1] == X)
            X = X + M;
 
        // Else missing element
        // is in that row
        else {
 
            // Initializing first element
            //  of the row
            int Y = X - M + 1;
 
            // Initializing column index
            int j = 0;
 
            // Linear Search
            while (Y <= X) {
                if (Mat[i][j] == Y) {
                    Y++;
                    j++;
                }
                else
                    return Y;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int N = 3;
    int M = 4;
 
    int Mat[][Max] = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       {  9, 11, 12, 13 } };
 
    cout << check(Mat, N, M);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to return Missing
// Element from matrix Mat
static int check(int Mat[][], int N, int M)
{
    // Edge Case
    if (Mat[0][0] != 1)
        return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0][0] + M - 1;
 
    for (int i = 0; i < N; i++) {
 
        // If last element of respective row
        // matches respective multiples of X
        if (Mat[i][M - 1] == X)
            X = X + M;
 
        // Else missing element
        // is in that row
        else {
 
            // Initializing first element
            //  of the row
            int Y = X - M + 1;
 
            // Initializing column index
            int j = 0;
 
            // Linear Search
            while (Y <= X) {
                if (Mat[i][j] == Y) {
                    Y++;
                    j++;
                }
                else
                    return Y;
            }
        }
    }
    return 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    int M = 4;
 
    int Mat[][] = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       {  9, 11, 12, 13 } };
 
    System.out.print(check(Mat, N, M));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python 3 program for the above approach
  
# Function to return Missing
# Element from matrix Mat
def check(Mat, N, M):
     
    # Edge Case
    if (Mat[0][0] != 1):
        return 1
 
    # Initialize last element of
    # first row
    X = Mat[0][0] + M - 1;
 
    for i in range(N):
 
        # If last element of respective row
        # matches respective multiples of X
        if (Mat[i][M - 1] == X):
            X = X + M
 
        # Else missing element
        # is in that row
        else:
 
            # Initializing first element
            #  of the row
            Y = X - M + 1
 
            # Initializing column index
            j = 0;
 
            # Linear Search
            while (Y <= X):
                if (Mat[i][j] == Y):
                    Y = Y + 1
                    j = j + 1
                else:
                    return Y
 
# Driver Code
if __name__ == "__main__":
 
    N, M = 3, 4
     
    Mat = [[ 1, 2, 3, 4 ],
           [ 5, 6, 7, 8 ],
           [ 9, 11, 12, 13]]
            
    ans = check(Mat, N, M)
    print(ans)
 
# This code is contributed by Abhishek Thakur.


C#
// C# program for the above approach
using System;
class GFG {
 
 
  // Function to return Missing
  // Element from matrix Mat
  static int check(int [,] Mat, int N, int M)
  {
    // Edge Case
    if (Mat[0, 0] != 1)
      return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0, 0] + M - 1;
 
    for (int i = 0; i < N; i++) {
 
      // If last element of respective row
      // matches respective multiples of X
      if (Mat[i, M - 1] == X)
        X = X + M;
 
      // Else missing element
      // is in that row
      else {
 
        // Initializing first element
        //  of the row
        int Y = X - M + 1;
 
        // Initializing column index
        int j = 0;
 
        // Linear Search
        while (Y <= X) {
          if (Mat[i, j] == Y) {
            Y++;
            j++;
          }
          else
            return Y;
        }
      }
    }
    return 0;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 3;
    int M = 4;
 
    int[, ] Mat = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 11, 12, 13 } };
 
    Console.Write(check(Mat, N, M));
  }
}
 
// This code is contributed by ukasp.


Javascript


C++
// C++ program to implement the above approach
#include 
using namespace std;
#define Max 1000
 
// Function to return Missing
// Element from matrix MAt
int check(int Mat[][Max], int N, int M)
{
 
    // Edge Case
    if (Mat[0][0] != 1)
        return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0][0] + M - 1;
    int m, l = 0, h = N - 1;
 
    // Checking for row
    while (l < h) {
 
        // Avoiding overflow
        m = l + (h - l) / 2;
 
        if (Mat[m][M - 1] == X * (m + 1))
            l = m + 1;
        else
            h = m;
    }
 
    // Set the required row index and
    // last element of previous row
    int R = h;
    X = X * h;
    l = 0, h = M - 1;
 
    // Checking for column
    while (l < h) {
 
        // Avoiding overflow
        m = l + (h - l) / 2;
 
        if (Mat[R][m] == X + (m + 1))
            l = m + 1;
        else
            h = m;
    }
 
    // Returning Missing Element
    return X + h + 1;
}
 
// Driver Code
int main()
{
    int N = 3;
    int M = 4;
    int Mat[][Max] = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       { 9, 11, 12, 13 } };
    cout << check(Mat, N, M);
    return 0;
}


Java
// Java program to implement the above approach
import java.util.*;
class GFG{
 
  // Function to return Missing
  // Element from matrix MAt
  static int check(int Mat[][], int N, int M)
  {
 
    // Edge Case
    if (Mat[0][0] != 1)
      return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0][0] + M - 1;
    int m, l = 0, h = N - 1;
 
    // Checking for row
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[m][M - 1] == X * (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Set the required row index and
    // last element of previous row
    int R = h;
    X = X * h;
    l = 0;
    h = M - 1;
 
    // Checking for column
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[R][m] == X + (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Returning Missing Element
    return X + h + 1;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 3;
    int M = 4;
    int Mat[][] = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 11, 12, 13 } };
    System.out.print(check(Mat, N, M));
  }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python program to implement the above approach
 
# Function to return Missing
# Element from matrix MAt
def check(Mat, N, M):
 
    # Edge Case
    if (Mat[0][0] != 1):
        return 1;
 
    # Initialize last element of
    # first row
    X = Mat[0][0] + M - 1;
    m = 0;
    l = 0
    h = N - 1;
 
    # Checking for row
    while (l < h):
 
        # Avoiding overflow
        m = l + (h - l) // 2;
 
        if (Mat[m][M - 1] == X * (m + 1)):
            l = m + 1;
        else:
            h = m;
     
 
    # Set the required row index and
    # last element of previous row
    R = h;
    X = X * h;
    l = 0;
    h = M - 1;
 
    # Checking for column
    while (l < h):
 
        # Avoiding overflow
        m = l + (h - l) // 2;
 
        if (Mat[R][m] == X + (m + 1)):
            l = m + 1;
        else:
            h = m;
     
    # Returning Missing Element
    return X + h + 1;
 
# Driver Code
if __name__ == '__main__':
    N = 3;
    M = 4;
    Mat = [[ 1, 2, 3, 4 ],[ 5, 6, 7, 8 ],[ 9, 11, 12, 13 ]] ;
    print(check(Mat, N, M));
 
 
# This code is contributed by Rajput-Ji


C#
// C# program to implement the above approach
using System;
 
public class GFG{
 
  // Function to return Missing
  // Element from matrix MAt
  static int check(int [,]Mat, int N, int M)
  {
 
    // Edge Case
    if (Mat[0,0] != 1)
      return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0,0] + M - 1;
    int m, l = 0, h = N - 1;
 
    // Checking for row
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[m,M - 1] == X * (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Set the required row index and
    // last element of previous row
    int R = h;
    X = X * h;
    l = 0;
    h = M - 1;
 
    // Checking for column
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[R,m] == X + (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Returning Missing Element
    return X + h + 1;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 3;
    int M = 4;
    int [,]Mat = { { 1, 2, 3, 4 },
                  { 5, 6, 7, 8 },
                  { 9, 11, 12, 13 } };
    Console.Write(check(Mat, N, M));
  }
}
 
// This code is contributed by Rajput-Ji


Javascript



输出
10

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

有效的方法:有效的方法是使用二进制搜索算法检查行中最后一个元素的倍数。如果不匹配,则缺少的元素在该行或其前几行中,如果匹配,则缺少的元素在下一行中。首先,使用二分查找找到该行,然后以相同的方式对该行应用二分查找以查找该列。

下面是上述方法的实现:

C++

// C++ program to implement the above approach
#include 
using namespace std;
#define Max 1000
 
// Function to return Missing
// Element from matrix MAt
int check(int Mat[][Max], int N, int M)
{
 
    // Edge Case
    if (Mat[0][0] != 1)
        return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0][0] + M - 1;
    int m, l = 0, h = N - 1;
 
    // Checking for row
    while (l < h) {
 
        // Avoiding overflow
        m = l + (h - l) / 2;
 
        if (Mat[m][M - 1] == X * (m + 1))
            l = m + 1;
        else
            h = m;
    }
 
    // Set the required row index and
    // last element of previous row
    int R = h;
    X = X * h;
    l = 0, h = M - 1;
 
    // Checking for column
    while (l < h) {
 
        // Avoiding overflow
        m = l + (h - l) / 2;
 
        if (Mat[R][m] == X + (m + 1))
            l = m + 1;
        else
            h = m;
    }
 
    // Returning Missing Element
    return X + h + 1;
}
 
// Driver Code
int main()
{
    int N = 3;
    int M = 4;
    int Mat[][Max] = { { 1, 2, 3, 4 },
                       { 5, 6, 7, 8 },
                       { 9, 11, 12, 13 } };
    cout << check(Mat, N, M);
    return 0;
}

Java

// Java program to implement the above approach
import java.util.*;
class GFG{
 
  // Function to return Missing
  // Element from matrix MAt
  static int check(int Mat[][], int N, int M)
  {
 
    // Edge Case
    if (Mat[0][0] != 1)
      return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0][0] + M - 1;
    int m, l = 0, h = N - 1;
 
    // Checking for row
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[m][M - 1] == X * (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Set the required row index and
    // last element of previous row
    int R = h;
    X = X * h;
    l = 0;
    h = M - 1;
 
    // Checking for column
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[R][m] == X + (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Returning Missing Element
    return X + h + 1;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 3;
    int M = 4;
    int Mat[][] = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 11, 12, 13 } };
    System.out.print(check(Mat, N, M));
  }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python program to implement the above approach
 
# Function to return Missing
# Element from matrix MAt
def check(Mat, N, M):
 
    # Edge Case
    if (Mat[0][0] != 1):
        return 1;
 
    # Initialize last element of
    # first row
    X = Mat[0][0] + M - 1;
    m = 0;
    l = 0
    h = N - 1;
 
    # Checking for row
    while (l < h):
 
        # Avoiding overflow
        m = l + (h - l) // 2;
 
        if (Mat[m][M - 1] == X * (m + 1)):
            l = m + 1;
        else:
            h = m;
     
 
    # Set the required row index and
    # last element of previous row
    R = h;
    X = X * h;
    l = 0;
    h = M - 1;
 
    # Checking for column
    while (l < h):
 
        # Avoiding overflow
        m = l + (h - l) // 2;
 
        if (Mat[R][m] == X + (m + 1)):
            l = m + 1;
        else:
            h = m;
     
    # Returning Missing Element
    return X + h + 1;
 
# Driver Code
if __name__ == '__main__':
    N = 3;
    M = 4;
    Mat = [[ 1, 2, 3, 4 ],[ 5, 6, 7, 8 ],[ 9, 11, 12, 13 ]] ;
    print(check(Mat, N, M));
 
 
# This code is contributed by Rajput-Ji

C#

// C# program to implement the above approach
using System;
 
public class GFG{
 
  // Function to return Missing
  // Element from matrix MAt
  static int check(int [,]Mat, int N, int M)
  {
 
    // Edge Case
    if (Mat[0,0] != 1)
      return 1;
 
    // Initialize last element of
    // first row
    int X = Mat[0,0] + M - 1;
    int m, l = 0, h = N - 1;
 
    // Checking for row
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[m,M - 1] == X * (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Set the required row index and
    // last element of previous row
    int R = h;
    X = X * h;
    l = 0;
    h = M - 1;
 
    // Checking for column
    while (l < h) {
 
      // Avoiding overflow
      m = l + (h - l) / 2;
 
      if (Mat[R,m] == X + (m + 1))
        l = m + 1;
      else
        h = m;
    }
 
    // Returning Missing Element
    return X + h + 1;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 3;
    int M = 4;
    int [,]Mat = { { 1, 2, 3, 4 },
                  { 5, 6, 7, 8 },
                  { 9, 11, 12, 13 } };
    Console.Write(check(Mat, N, M));
  }
}
 
// This code is contributed by Rajput-Ji

Javascript



输出
10

时间复杂度:O(log N + log M)
辅助空间:O(1)