📌  相关文章
📜  最小化N,以使直到N的所有因子的总和大于或等于X

📅  最后修改于: 2021-05-17 01:39:04             🧑  作者: Mango

给定一个数X ,任务是找到最小数N ,使得从1到N的所有因子的总和大于等于X。
例子:

天真的方法:天真的方法是从1开始运行一个循环,直到factor的总数大于X为止。
下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
#define MAX 1000050
#define lli long int
 
// Function to count total factors of N
lli CountFactors(lli N)
{
    lli cnt = 0;
    for (lli i = 1;
         i * i <= N; i++) {
 
        if (N % i == 0)
            cnt += ((N / i == i) ? 1 : 2);
    }
 
    // Return the count
    return cnt;
}
 
// Function to search lowest N
// such that the given condition
// is satisfied
lli minN(lli X)
{
    lli i = 1;
    lli total = 0;
 
    while (1) {
 
        // Add the count of total factor
        // of i to variable total
        total = total + CountFactors(i);
 
        // If total count is greater than
        // equal to N, then return i
        if (total >= X)
            return i;
        i++;
    }
}
 
// Driver Code
int main()
{
    // Given sum
    lli X = 10;
 
    // Function Call
    cout << minN(X) << endl;
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
static final int MAX = 1000050;
 
// Function to count total factors of N
static int CountFactors(int N)
{
    int cnt = 0;
    for(int i = 1; i * i <= N; i++)
    {
       if (N % i == 0)
           cnt += ((N / i == i) ? 1 : 2);
    }
 
    // Return the count
    return cnt;
}
 
// Function to search lowest N
// such that the given condition
// is satisfied
static int minN(int X)
{
    int i = 1;
    int total = 0;
 
    while (true)
    {
         
        // Add the count of total factor
        // of i to variable total
        total = total + CountFactors(i);
 
        // If total count is greater than
        // equal to N, then return i
        if (total >= X)
            return i;
        i++;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given sum
    int X = 10;
 
    // Function Call
    System.out.print(minN(X) + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for
# the above approach
MAX = 1000050
 
# Function to count
# total factors of N
def CountFactors(N):
 
    cnt = 0
    i = 1
    while i * i <= N:
 
        if (N % i == 0):
            if (N // i == i):
                cnt +=  1
            else:
                cnt +=  2
        i += 1  
 
    # Return the count
    return cnt
 
# Function to search lowest N
# such that the given condition
# is satisfied
def minN(X):
 
    i = 1
    total = 0
 
    while (1):
 
        # Add the count of total factor
        # of i to variable total
        total = total + CountFactors(i)
 
        # If total count is greater than
        # equal to N, then return i
        if (total >= X):
            return i
        i += 1
 
# Driver Code
if __name__ == "__main__":
 
    # Given sum
    X = 10
 
    # Function Call
    print( minN(X))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
  
//static readonly int MAX = 1000050;
  
// Function to count total factors of N
static int CountFactors(int N)
{
    int cnt = 0;
    for(int i = 1; i * i <= N; i++)
    {
       if (N % i == 0)
           cnt += ((N / i == i) ? 1 : 2);
    }
  
    // Return the count
    return cnt;
}
  
// Function to search lowest N
// such that the given condition
// is satisfied
static int minN(int X)
{
    int i = 1;
    int total = 0;
  
    while (true)
    {
          
        // Add the count of total factor
        // of i to variable total
        total = total + CountFactors(i);
  
        // If total count is greater than
        // equal to N, then return i
        if (total >= X)
            return i;
        i++;
    }
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given sum
    int X = 10;
  
    // Function Call
    Console.Write(minN(X) + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
#define MAX 1000050
#define lli long int
 
// Array to store smallest
// prime factors of each no.
lli spf[MAX + 1];
 
// Function to calculate smallest
// prime factor of N.
void calculate_SPF()
{
    for (lli i = 0; i <= MAX; i++)
        spf[i] = i;
 
    for (lli i = 4; i <= MAX; i += 2)
        spf[i] = 2;
 
    for (lli i = 3;
         i * i <= MAX; i++) {
 
        if (spf[i] == i) {
            for (int j = i * i;
                 j <= MAX; j += i)
 
                // marking spf[j] if
                // it is not previously
                // marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
 
// Array to store the count of
// factor for N
lli tfactor[MAX + 1];
 
// Prefix array which contains
// the count of factors from 1 to N
lli pre[MAX + 1];
 
// Function to count total factors
// from 1 to N
void CountTotalfactors()
{
    tfactor[1] = pre[1] = 1;
 
    for (lli i = 2; i <= MAX; i++) {
 
        lli mspf = spf[i];
        lli prim = mspf;
        lli temp = i;
        lli cnt = 0;
 
        while (temp % mspf == 0) {
            temp /= mspf;
            cnt += 1;
            prim = prim * mspf;
        }
 
        // Store total factors of i
        tfactor[i] = (cnt + 1)
                     * tfactor[temp];
 
        // Stores total factors
        // from 1 to i
        pre[i] = pre[i - 1]
                 + tfactor[i];
    }
}
 
// Function to search lowest X
// such that the given condition
// is satisfied
lli BinarySearch(lli X)
{
    lli start = 1;
    lli end = MAX - 1;
 
    while (start < end) {
 
        // Find mid
        lli mid = (start + end) / 2;
 
        if (pre[mid] == X)
            return mid;
 
        // Search in the right half
        else if (pre[mid] < X)
            start = mid + 1;
 
        // Search in the left half
        else
            end = mid;
    }
 
    // Return the position after
    // Binary Search
    return start;
}
 
// Function to find the required sum
void findSumOfCount(int X)
{
 
    // Precompute smallest prime
    // factor of each value
    calculate_SPF();
 
    // Calculate count of total
    // factors from 1 to N
    CountTotalfactors();
 
    // Binary search to find minimum N
    cout << BinarySearch(X)
         << endl;
}
 
// Driver Code
int main()
{
    // Given Sum
    int X = 10;
 
    // Function Call
    findSumOfCount(X);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static final int MAX = 1000050;
 
// Array to store smallest
// prime factors of each no.
static int []spf = new int[MAX + 1];
 
// Function to calculate smallest
// prime factor of N.
static void calculate_SPF()
{
    for(int i = 0; i <= MAX; i++)
       spf[i] = i;
 
    for(int i = 4; i <= MAX; i += 2)
       spf[i] = 2;
 
    for(int i = 3; i * i <= MAX; i++)
    {
       if (spf[i] == i)
       {
           for(int j = i * i;
                   j <= MAX; j += i)
            
              // marking spf[j] if
              // it is not previously
              // marked
              if (spf[j] == j)
                  spf[j] = i;
       }
    }
}
 
// Array to store the count of
// factor for N
static int []tfactor = new int[MAX + 1];
 
// Prefix array which contains
// the count of factors from 1 to N
static int []pre = new int[MAX + 1];
 
// Function to count total factors
// from 1 to N
static void CountTotalfactors()
{
    tfactor[1] = pre[1] = 1;
 
    for(int i = 2; i <= MAX; i++)
    {
       int mspf = spf[i];
       int prim = mspf;
       int temp = i;
       int cnt = 0;
        
       while (temp % mspf == 0)
       {
           temp /= mspf;
           cnt += 1;
           prim = prim * mspf;
       }
        
       // Store total factors of i
       tfactor[i] = (cnt + 1) * tfactor[temp];
        
       // Stores total factors
       // from 1 to i
       pre[i] = pre[i - 1] + tfactor[i];
    }
}
 
// Function to search lowest X
// such that the given condition
// is satisfied
static int BinarySearch(int X)
{
    int start = 1;
    int end = MAX - 1;
 
    while (start < end)
    {
         
        // Find mid
        int mid = (start + end) / 2;
 
        if (pre[mid] == X)
            return mid;
 
        // Search in the right half
        else if (pre[mid] < X)
            start = mid + 1;
 
        // Search in the left half
        else
            end = mid;
    }
 
    // Return the position after
    // Binary Search
    return start;
}
 
// Function to find the required sum
static void findSumOfCount(int X)
{
 
    // Precompute smallest prime
    // factor of each value
    calculate_SPF();
 
    // Calculate count of total
    // factors from 1 to N
    CountTotalfactors();
 
    // Binary search to find minimum N
    System.out.print(BinarySearch(X) + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Sum
    int X = 10;
 
    // Function Call
    findSumOfCount(X);
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program for the
# above approach
MAX = 1000050
  
# Array to store smallest
# prime factors of each no.
spf = [0 for i in range(MAX + 1)]
  
# Function to calculate smallest
# prime factor of N.
def calculate_SPF():
     
    for i in range(MAX + 1):
        spf[i] = i;
     
    for i in range(4, MAX + 1, 2):
        spf[i] = 2;
     
    i = 3
      
    while(i * i <= MAX):   
        if (spf[i] == i)           
            j = i * i
             
            while(j <= MAX):
  
                # Marking spf[j] if
                # it is not previously
                # marked
                if (spf[j] == j):
                    spf[j] = i;
                 
                j += i
        i += 1       
         
# Array to store the
# count of factor for N
tfactor = [0 for i in range(MAX + 1)]
  
# Prefix array which contains
# the count of factors from 1 to N
pre = [0 for i in range(MAX + 1)]
  
# Function to count
# total factors from 1 to N
def CountTotalfactors():
 
    tfactor[1] = pre[1] = 1;
     
    for i in range(2, MAX + 1):
        mspf = spf[i];
        prim = mspf;
        temp = i;
        cnt = 0;
  
        while (temp % mspf == 0):
            temp //= mspf;
            cnt += 1;
            prim = prim * mspf;
  
        # Store total factors of i
        tfactor[i] = (cnt + 1) *
                      tfactor[temp];
  
        # Stores total factors
        # from 1 to i
        pre[i] = pre[i - 1] +
                 tfactor[i];   
 
# Function to search lowest X
# such that the given condition
# is satisfied
def BinarySearch(X):
 
    start = 1;
    end = MAX - 1;
  
    while (start < end):
  
        # Find mid
        mid = (start + end) // 2;
  
        if (pre[mid] == X):
            return mid;
  
        # Search in the right half
        elif (pre[mid] < X):
            start = mid + 1;
  
        # Search in the left half
        else:
            end = mid;   
  
    # Return the position after
    # Binary Search
    return start;
 
# Function to find the
# required sum
def findSumOfCount(X):
  
    # Precompute smallest prime
    # factor of each value
    calculate_SPF();
  
    # Calculate count of total
    # factors from 1 to N
    CountTotalfactors();
  
    # Binary search to find
    # minimum N
    print(BinarySearch(X))
     
# Driver code
if __name__ == "__main__":
     
    # Given Sum
    X = 10;
  
    # Function Call
    findSumOfCount(X);
         
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
class GFG{
 
const int MAX = 1000050;
 
// Array to store smallest
// prime factors of each no.
static int []spf = new int[MAX + 1];
 
// Function to calculate smallest
// prime factor of N.
static void calculate_SPF()
{
    for(int i = 0; i <= MAX; i++)
    spf[i] = i;
 
    for(int i = 4; i <= MAX; i += 2)
    spf[i] = 2;
 
    for(int i = 3; i * i <= MAX; i++)
    {
        if (spf[i] == i)
        {
            for(int j = i * i;
                    j <= MAX; j += i)
                 
                // marking spf[j] if
                // it is not previously
                // marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
 
// Array to store the count of
// factor for N
static int []tfactor = new int[MAX + 1];
 
// Prefix array which contains
// the count of factors from 1 to N
static int []pre = new int[MAX + 1];
 
// Function to count total factors
// from 1 to N
static void CountTotalfactors()
{
    tfactor[1] = pre[1] = 1;
 
    for(int i = 2; i <= MAX; i++)
    {
        int mspf = spf[i];
        int prim = mspf;
        int temp = i;
        int cnt = 0;
             
        while (temp % mspf == 0)
        {
            temp /= mspf;
            cnt += 1;
            prim = prim * mspf;
        }
             
        // Store total factors of i
        tfactor[i] = (cnt + 1) * tfactor[temp];
             
        // Stores total factors
        // from 1 to i
        pre[i] = pre[i - 1] + tfactor[i];
    }
}
 
// Function to search lowest X
// such that the given condition
// is satisfied
static int BinarySearch(int X)
{
    int start = 1;
    int end = MAX - 1;
 
    while (start < end)
    {
         
        // Find mid
        int mid = (start + end) / 2;
 
        if (pre[mid] == X)
            return mid;
 
        // Search in the right half
        else if (pre[mid] < X)
            start = mid + 1;
 
        // Search in the left half
        else
            end = mid;
    }
 
    // Return the position after
    // Binary Search
    return start;
}
 
// Function to find the required sum
static void findSumOfCount(int X)
{
 
    // Precompute smallest prime
    // factor of each value
    calculate_SPF();
 
    // Calculate count of total
    // factors from 1 to N
    CountTotalfactors();
 
    // Binary search to find minimum N
    Console.Write(BinarySearch(X) + "\n");
}
 
// Driver Code
public static void Main()
{
     
    // Given Sum
    int X = 10;
 
    // Function Call
    findSumOfCount(X);
}
}
 
// This code is contributed by Code_Mech


输出:
5

时间复杂度: O(N * sqrt(N))
辅助空间: O(1)
高效方法:可以通过进行一些预计算来优化上述方法。步骤如下:

  1. 使用本文讨论的方法计算并存储每个数字的最小素因数(SPF)。
  2. 使用Eratosthenes筛和上述计算的最小素因数,可以有效地找到N的因数。
  3. 创建一个前缀和数组,以存储从1到MAX的因子计数之和。
  4. 为了找到满足上述条件的最低N ,而不是在前缀数组中进行线性搜索,而是对前缀和数组进行二进制搜索(因为前缀和数组的顺序将递增),因此时间复杂度对于多个查询也。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
#define MAX 1000050
#define lli long int
 
// Array to store smallest
// prime factors of each no.
lli spf[MAX + 1];
 
// Function to calculate smallest
// prime factor of N.
void calculate_SPF()
{
    for (lli i = 0; i <= MAX; i++)
        spf[i] = i;
 
    for (lli i = 4; i <= MAX; i += 2)
        spf[i] = 2;
 
    for (lli i = 3;
         i * i <= MAX; i++) {
 
        if (spf[i] == i) {
            for (int j = i * i;
                 j <= MAX; j += i)
 
                // marking spf[j] if
                // it is not previously
                // marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
 
// Array to store the count of
// factor for N
lli tfactor[MAX + 1];
 
// Prefix array which contains
// the count of factors from 1 to N
lli pre[MAX + 1];
 
// Function to count total factors
// from 1 to N
void CountTotalfactors()
{
    tfactor[1] = pre[1] = 1;
 
    for (lli i = 2; i <= MAX; i++) {
 
        lli mspf = spf[i];
        lli prim = mspf;
        lli temp = i;
        lli cnt = 0;
 
        while (temp % mspf == 0) {
            temp /= mspf;
            cnt += 1;
            prim = prim * mspf;
        }
 
        // Store total factors of i
        tfactor[i] = (cnt + 1)
                     * tfactor[temp];
 
        // Stores total factors
        // from 1 to i
        pre[i] = pre[i - 1]
                 + tfactor[i];
    }
}
 
// Function to search lowest X
// such that the given condition
// is satisfied
lli BinarySearch(lli X)
{
    lli start = 1;
    lli end = MAX - 1;
 
    while (start < end) {
 
        // Find mid
        lli mid = (start + end) / 2;
 
        if (pre[mid] == X)
            return mid;
 
        // Search in the right half
        else if (pre[mid] < X)
            start = mid + 1;
 
        // Search in the left half
        else
            end = mid;
    }
 
    // Return the position after
    // Binary Search
    return start;
}
 
// Function to find the required sum
void findSumOfCount(int X)
{
 
    // Precompute smallest prime
    // factor of each value
    calculate_SPF();
 
    // Calculate count of total
    // factors from 1 to N
    CountTotalfactors();
 
    // Binary search to find minimum N
    cout << BinarySearch(X)
         << endl;
}
 
// Driver Code
int main()
{
    // Given Sum
    int X = 10;
 
    // Function Call
    findSumOfCount(X);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
static final int MAX = 1000050;
 
// Array to store smallest
// prime factors of each no.
static int []spf = new int[MAX + 1];
 
// Function to calculate smallest
// prime factor of N.
static void calculate_SPF()
{
    for(int i = 0; i <= MAX; i++)
       spf[i] = i;
 
    for(int i = 4; i <= MAX; i += 2)
       spf[i] = 2;
 
    for(int i = 3; i * i <= MAX; i++)
    {
       if (spf[i] == i)
       {
           for(int j = i * i;
                   j <= MAX; j += i)
            
              // marking spf[j] if
              // it is not previously
              // marked
              if (spf[j] == j)
                  spf[j] = i;
       }
    }
}
 
// Array to store the count of
// factor for N
static int []tfactor = new int[MAX + 1];
 
// Prefix array which contains
// the count of factors from 1 to N
static int []pre = new int[MAX + 1];
 
// Function to count total factors
// from 1 to N
static void CountTotalfactors()
{
    tfactor[1] = pre[1] = 1;
 
    for(int i = 2; i <= MAX; i++)
    {
       int mspf = spf[i];
       int prim = mspf;
       int temp = i;
       int cnt = 0;
        
       while (temp % mspf == 0)
       {
           temp /= mspf;
           cnt += 1;
           prim = prim * mspf;
       }
        
       // Store total factors of i
       tfactor[i] = (cnt + 1) * tfactor[temp];
        
       // Stores total factors
       // from 1 to i
       pre[i] = pre[i - 1] + tfactor[i];
    }
}
 
// Function to search lowest X
// such that the given condition
// is satisfied
static int BinarySearch(int X)
{
    int start = 1;
    int end = MAX - 1;
 
    while (start < end)
    {
         
        // Find mid
        int mid = (start + end) / 2;
 
        if (pre[mid] == X)
            return mid;
 
        // Search in the right half
        else if (pre[mid] < X)
            start = mid + 1;
 
        // Search in the left half
        else
            end = mid;
    }
 
    // Return the position after
    // Binary Search
    return start;
}
 
// Function to find the required sum
static void findSumOfCount(int X)
{
 
    // Precompute smallest prime
    // factor of each value
    calculate_SPF();
 
    // Calculate count of total
    // factors from 1 to N
    CountTotalfactors();
 
    // Binary search to find minimum N
    System.out.print(BinarySearch(X) + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Sum
    int X = 10;
 
    // Function Call
    findSumOfCount(X);
}
}
 
// This code is contributed by PrinciRaj1992

Python3

# Python3 program for the
# above approach
MAX = 1000050
  
# Array to store smallest
# prime factors of each no.
spf = [0 for i in range(MAX + 1)]
  
# Function to calculate smallest
# prime factor of N.
def calculate_SPF():
     
    for i in range(MAX + 1):
        spf[i] = i;
     
    for i in range(4, MAX + 1, 2):
        spf[i] = 2;
     
    i = 3
      
    while(i * i <= MAX):   
        if (spf[i] == i)           
            j = i * i
             
            while(j <= MAX):
  
                # Marking spf[j] if
                # it is not previously
                # marked
                if (spf[j] == j):
                    spf[j] = i;
                 
                j += i
        i += 1       
         
# Array to store the
# count of factor for N
tfactor = [0 for i in range(MAX + 1)]
  
# Prefix array which contains
# the count of factors from 1 to N
pre = [0 for i in range(MAX + 1)]
  
# Function to count
# total factors from 1 to N
def CountTotalfactors():
 
    tfactor[1] = pre[1] = 1;
     
    for i in range(2, MAX + 1):
        mspf = spf[i];
        prim = mspf;
        temp = i;
        cnt = 0;
  
        while (temp % mspf == 0):
            temp //= mspf;
            cnt += 1;
            prim = prim * mspf;
  
        # Store total factors of i
        tfactor[i] = (cnt + 1) *
                      tfactor[temp];
  
        # Stores total factors
        # from 1 to i
        pre[i] = pre[i - 1] +
                 tfactor[i];   
 
# Function to search lowest X
# such that the given condition
# is satisfied
def BinarySearch(X):
 
    start = 1;
    end = MAX - 1;
  
    while (start < end):
  
        # Find mid
        mid = (start + end) // 2;
  
        if (pre[mid] == X):
            return mid;
  
        # Search in the right half
        elif (pre[mid] < X):
            start = mid + 1;
  
        # Search in the left half
        else:
            end = mid;   
  
    # Return the position after
    # Binary Search
    return start;
 
# Function to find the
# required sum
def findSumOfCount(X):
  
    # Precompute smallest prime
    # factor of each value
    calculate_SPF();
  
    # Calculate count of total
    # factors from 1 to N
    CountTotalfactors();
  
    # Binary search to find
    # minimum N
    print(BinarySearch(X))
     
# Driver code
if __name__ == "__main__":
     
    # Given Sum
    X = 10;
  
    # Function Call
    findSumOfCount(X);
         
# This code is contributed by rutvik_56

C#

// C# program for the above approach
using System;
class GFG{
 
const int MAX = 1000050;
 
// Array to store smallest
// prime factors of each no.
static int []spf = new int[MAX + 1];
 
// Function to calculate smallest
// prime factor of N.
static void calculate_SPF()
{
    for(int i = 0; i <= MAX; i++)
    spf[i] = i;
 
    for(int i = 4; i <= MAX; i += 2)
    spf[i] = 2;
 
    for(int i = 3; i * i <= MAX; i++)
    {
        if (spf[i] == i)
        {
            for(int j = i * i;
                    j <= MAX; j += i)
                 
                // marking spf[j] if
                // it is not previously
                // marked
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}
 
// Array to store the count of
// factor for N
static int []tfactor = new int[MAX + 1];
 
// Prefix array which contains
// the count of factors from 1 to N
static int []pre = new int[MAX + 1];
 
// Function to count total factors
// from 1 to N
static void CountTotalfactors()
{
    tfactor[1] = pre[1] = 1;
 
    for(int i = 2; i <= MAX; i++)
    {
        int mspf = spf[i];
        int prim = mspf;
        int temp = i;
        int cnt = 0;
             
        while (temp % mspf == 0)
        {
            temp /= mspf;
            cnt += 1;
            prim = prim * mspf;
        }
             
        // Store total factors of i
        tfactor[i] = (cnt + 1) * tfactor[temp];
             
        // Stores total factors
        // from 1 to i
        pre[i] = pre[i - 1] + tfactor[i];
    }
}
 
// Function to search lowest X
// such that the given condition
// is satisfied
static int BinarySearch(int X)
{
    int start = 1;
    int end = MAX - 1;
 
    while (start < end)
    {
         
        // Find mid
        int mid = (start + end) / 2;
 
        if (pre[mid] == X)
            return mid;
 
        // Search in the right half
        else if (pre[mid] < X)
            start = mid + 1;
 
        // Search in the left half
        else
            end = mid;
    }
 
    // Return the position after
    // Binary Search
    return start;
}
 
// Function to find the required sum
static void findSumOfCount(int X)
{
 
    // Precompute smallest prime
    // factor of each value
    calculate_SPF();
 
    // Calculate count of total
    // factors from 1 to N
    CountTotalfactors();
 
    // Binary search to find minimum N
    Console.Write(BinarySearch(X) + "\n");
}
 
// Driver Code
public static void Main()
{
     
    // Given Sum
    int X = 10;
 
    // Function Call
    findSumOfCount(X);
}
}
 
// This code is contributed by Code_Mech
输出:
5

时间复杂度: O(MAX * log(MAX))
辅助空间: O(MAX)
注意:如果存在Q个查询,则为所有Q个查询找到N的时间复杂度将为O(Q * log(MAX))