📜  给定索引范围内不等于 X 的最小索引

📅  最后修改于: 2021-09-17 07:27:11             🧑  作者: Mango

给定一个大小为N的整数数组arr[]和形式为{L, R, X} 的Q查询,任务是从给定数组中找到LR之间的最小索引,使得arr[i] !=十。如果数组中不存在这样的索引,则打印-1

例子:

天真的方法:
为每个查询迭代范围[L, R]并检查是否存在不包含X 的任何索引。如果找到这样的索引,则打印该索引。否则,打印-1

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

有效的方法:
通过对每个数组元素预先计算并存储与当前元素不同的下一个元素的索引,可以进一步优化上述方法,这将每个查询的计算复杂度降低到 O(1)。
请按照以下步骤解决问题:

  1. 创建一个辅助数组nextpos[]来为每个数组元素存储与当前元素不同的下一个元素的索引。
  2. 现在要处理每个查询,首先检查索引L处的值是否不等于X 。如果是这样,那么答案将是L
  3. 否则,这意味着arr[L] = X 。在这种情况下,我们需要找到下一个值与arr[L]不同的索引。这可以从nextpos[L] 获得
  4. 如果nextpos[L]小于等于R ,则打印 nexpos[L]。
  5. 如果上述条件都不满足,则答案将为-1

插图:

下面是上述方法的实现:

C++
// C++ Program to find the smallest
// index in the array in the range
// [L, R] which does not contain X
 
#include 
using namespace std;
 
// Precompute the index of next
// different element in the array
// for every array element
void precompute(int nextpos[], int arr[],
                int N)
{
    // Default value
    nextpos[N - 1] = N;
 
    for (int i = N - 2; i >= 0; i--) {
 
        // Compute nextpos[i] using
        // nextpos[i+1]
        if (arr[i] == arr[i + 1])
            nextpos[i] = nextpos[i + 1];
        else
            nextpos[i] = i + 1;
    }
}
// Function to return the smallest index
void findIndex(int query[][3], int arr[],
               int N, int Q)
{
    // nextpos[i] will store the next
    // position p where arr[p]!=arr[i]
    int nextpos[N];
    precompute(nextpos, arr, N);
 
    for (int i = 0; i < Q; i++) {
        int l, r, x;
        l = query[i][0];
        r = query[i][1];
        x = query[i][2];
 
        int ans = -1;
 
        // If X is not present at l
        if (arr[l] != x)
            ans = l;
 
        // Otherwise
        else {
 
            // Find the index which
            // stores a value different
            // from X
            int d = nextpos[l];
 
            // If that index is within
            // the range
            if (d <= r)
                ans = d;
        }
        cout << ans << "\n";
    }
}
// Driver Code
int main()
{
    int N, Q;
    N = 6;
    Q = 3;
    int arr[] = { 1, 2, 1, 1, 3, 5 };
    int query[Q][3] = { { 0, 3, 1 },
                        { 1, 5, 2 },
                        { 2, 3, 1 } };
 
    findIndex(query, arr, N, Q);
 
    return 0;
}


Java
// Java program to find the smallest
// index in the array in the range
// [L, R] which does not contain X
class GFG{
 
// Precompute the index of next
// different element in the array
// for every array element
static void precompute(int nextpos[], int arr[],
                       int N)
{
     
    // Default value
    nextpos[N - 1] = N;
 
    for(int i = N - 2; i >= 0; i--)
    {
         
       // Compute nextpos[i] using
       // nextpos[i+1]
       if (arr[i] == arr[i + 1])
           nextpos[i] = nextpos[i + 1];
       else
           nextpos[i] = i + 1;
    }
}
 
// Function to return the smallest index
static void findIndex(int query[][], int arr[],
                      int N, int Q)
{
     
    // nextpos[i] will store the next
    // position p where arr[p]!=arr[i]
    int []nextpos = new int[N];
    precompute(nextpos, arr, N);
 
    for(int i = 0; i < Q; i++)
    {
       int l, r, x;
       l = query[i][0];
       r = query[i][1];
       x = query[i][2];
        
       int ans = -1;
        
       // If X is not present at l
       if (arr[l] != x)
           ans = l;
            
       // Otherwise
       else
       {
            
           // Find the index which
           // stores a value different
           // from X
           int d = nextpos[l];
            
           // If that index is within
           // the range
           if (d <= r)
               ans = d;
       }
       System.out.print(ans + "\n");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N, Q;
    N = 6;
    Q = 3;
 
    int arr[] = { 1, 2, 1, 1, 3, 5 };
    int query[][] = { { 0, 3, 1 },
                      { 1, 5, 2 },
                      { 2, 3, 1 } };
 
    findIndex(query, arr, N, Q);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to find the smallest
# index in the array in the range
# [L, R] which does not contain X
 
# Precompute the index of next
# different element in the array
# for every array element
 
def precompute(nextpos, arr, N):
 
    # Default value
    nextpos[N - 1] = N
   
    for i in range(N - 2, -1, -1):
   
        # Compute nextpos[i] using
        # nextpos[i+1]
        if arr[i] == arr[i + 1]:
            nextpos[i] = nextpos[i + 1]
        else:
            nextpos[i] = i + 1
 
# Function to return the smallest index
def findIndex(query, arr, N, Q):
 
    # nextpos[i] will store the next
    # position p where arr[p]!=arr[i]
    nextpos = [0] * N
    precompute(nextpos, arr, N)
   
    for i in range(Q):
        l = query[i][0]
        r = query[i][1]
        x = query[i][2]
   
        ans = -1
   
        # If X is not present at l
        if arr[l] != x:
            ans = l
   
        # Otherwise
        else:
   
            # Find the index which
            # stores a value different
            # from X
            d = nextpos[l]
   
            # If that index is within
            # the range
            if d <= r:
                ans = d
         
        print(ans)
         
# Driver code     
N = 6
Q = 3
arr = [ 1, 2, 1, 1, 3, 5 ]
query = [ [ 0, 3, 1 ],
          [ 1, 5, 2 ],
          [ 2, 3, 1 ] ]
 
findIndex(query, arr, N, Q)
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to find the smallest
// index in the array in the range
// [L, R] which does not contain X
using System;
 
class GFG{
 
// Precompute the index of next
// different element in the array
// for every array element
static void precompute(int []nextpos,
                       int []arr, int N)
{
     
    // Default value
    nextpos[N - 1] = N;
 
    for(int i = N - 2; i >= 0; i--)
    {
         
        // Compute nextpos[i] using
        // nextpos[i+1]
        if (arr[i] == arr[i + 1])
            nextpos[i] = nextpos[i + 1];
        else
            nextpos[i] = i + 1;
    }
}
 
// Function to return the smallest index
static void findIndex(int [,]query, int []arr,
                      int N, int Q)
{
     
    // nextpos[i] will store the next
    // position p where arr[p]!=arr[i]
    int []nextpos = new int[N];
    precompute(nextpos, arr, N);
 
    for(int i = 0; i < Q; i++)
    {
        int l, r, x;
        l = query[i, 0];
        r = query[i, 1];
        x = query[i, 2];
             
        int ans = -1;
             
        // If X is not present at l
        if (arr[l] != x)
            ans = l;
                 
        // Otherwise
        else
        {
                 
            // Find the index which
            // stores a value different
            // from X
            int d = nextpos[l];
                 
            // If that index is within
            // the range
            if (d <= r)
                ans = d;
        }
        Console.Write(ans + "\n");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N, Q;
    N = 6;
    Q = 3;
 
    int []arr = { 1, 2, 1, 1, 3, 5 };
    int [,]query = { { 0, 3, 1 },
                     { 1, 5, 2 },
                     { 2, 3, 1 } };
 
    findIndex(query, arr, N, Q);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1
2
-1

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程