Smallest index in given range of indices which is not equal to X
Last Updated :
25 Apr, 2023
Given an integer array arr[] of size N, and Q queries of the form of {L, R, X}, the task is to find the smallest index between L and R from the given array such that arr[i] != X. If no such index exists in array, print -1.
Examples:
Input: arr[] = {1, 2, 1, 1, 3, 5}, query[][] = {{0, 3, 1}, {1, 5, 2}, {2, 3, 1}}
Output: 1
2
-1
Explanation:
The first index from 0 to 3 which does not contain 1 is 1
The first index from 1 to 5 which does not contain 2 is 2
All the indices from 2 to 3 contain 1. Hence, the answer is -1.
Input: arr[] = { 1, 2, 3, 2, 1, 1, 3, 5}, query[][] = { { 1, 4, 2 }, { 4, 5, 1 }, { 2, 3, 1 }, { 4, 6, 1 }}
Output: 2
-1
2
6
Naive Approach:
Iterate over the range [L, R] for every query and check if any index exists which does not contain X . If such an index is found, print that index. Otherwise, print -1.
Time Complexity: O (N * Q)
Auxiliary Space: O (1)
Efficient Approach:
The above approach can be further optimized by precomputing and storing the index of the next element which is different from the current element, for every array element, which reduces computational complexity to O(1) for every query.
Follow the steps below to solve the problem:
- Create an auxiliary array nextpos[] to store for every array element, the index of the next element which is different from the current element.
- Now to process every query, firstly check if the value at index L is not equal to X. If so, then the answer will be L.
- Otherwise, it means that arr[L] = X. In this case, we need to find the next index which has a value different from arr[L]. This can be obtained from nextpos[L].
- If nextpos[L] is less than equal to R, then print nexpos[L].
- If neither of the above conditions are satisfied, then answer will be -1.
Illustration:
arr[] = {1, 2, 1, 1, 3, 5}, query[][] = {{0, 3, 1}, {1, 5, 2}, {2, 3, 1}}
For the given arr[], nextpos[] array will be {1, 2, 4, 4, 5, 6}
For 1st Query: L = 0, R = 3, X = 1
arr[0] = 1 = X
nextpos[0] = 1( < 3)
Hence, the answer is 1.
For the 2nd Query: L = 1, R = 5, X = 2
arr[1] = 2( = X)
nextpos[1] = 2( < 5)
Hence, the answer is 2.
For the 3rd Query: L = 2, R = 3, X = 1
arr[2] = 1( = X)
nextpos[2] = 4( > R)
Hence, the answer is -1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void precompute( int nextpos[], int arr[],
int N)
{
nextpos[N - 1] = N;
for ( int i = N - 2; i >= 0; i--) {
if (arr[i] == arr[i + 1])
nextpos[i] = nextpos[i + 1];
else
nextpos[i] = i + 1;
}
}
void findIndex( int query[][3], int arr[],
int N, int Q)
{
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 (arr[l] != x)
ans = l;
else {
int d = nextpos[l];
if (d <= r)
ans = d;
}
cout << ans << "\n" ;
}
}
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
class GFG{
static void precompute( int nextpos[], int arr[],
int N)
{
nextpos[N - 1 ] = N;
for ( int i = N - 2 ; i >= 0 ; i--)
{
if (arr[i] == arr[i + 1 ])
nextpos[i] = nextpos[i + 1 ];
else
nextpos[i] = i + 1 ;
}
}
static void findIndex( int query[][], int arr[],
int N, int Q)
{
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 (arr[l] != x)
ans = l;
else
{
int d = nextpos[l];
if (d <= r)
ans = d;
}
System.out.print(ans + "\n" );
}
}
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);
}
}
|
Python3
def precompute(nextpos, arr, N):
nextpos[N - 1 ] = N
for i in range (N - 2 , - 1 , - 1 ):
if arr[i] = = arr[i + 1 ]:
nextpos[i] = nextpos[i + 1 ]
else :
nextpos[i] = i + 1
def findIndex(query, arr, N, Q):
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 arr[l] ! = x:
ans = l
else :
d = nextpos[l]
if d < = r:
ans = d
print (ans)
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)
|
C#
using System;
class GFG{
static void precompute( int []nextpos,
int []arr, int N)
{
nextpos[N - 1] = N;
for ( int i = N - 2; i >= 0; i--)
{
if (arr[i] == arr[i + 1])
nextpos[i] = nextpos[i + 1];
else
nextpos[i] = i + 1;
}
}
static void findIndex( int [,]query, int []arr,
int N, int Q)
{
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 (arr[l] != x)
ans = l;
else
{
int d = nextpos[l];
if (d <= r)
ans = d;
}
Console.Write(ans + "\n" );
}
}
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);
}
}
|
Javascript
<script>
function precompute(nextpos, arr, N)
{
nextpos[N - 1] = N;
for ( var i = N - 2; i >= 0; i--) {
if (arr[i] == arr[i + 1])
nextpos[i] = nextpos[i + 1];
else
nextpos[i] = i + 1;
}
}
function findIndex(query, arr, N, Q)
{
var nextpos = Array(N);
precompute(nextpos, arr, N);
for ( var i = 0; i < Q; i++) {
var l, r, x;
l = query[i][0];
r = query[i][1];
x = query[i][2];
var ans = -1;
if (arr[l] != x)
ans = l;
else {
var d = nextpos[l];
if (d <= r)
ans = d;
}
document.write( ans + "<br>" );
}
}
var N, Q;
N = 6;
Q = 3;
var arr = [1, 2, 1, 1, 3, 5];
var query = [ [ 0, 3, 1 ],
[ 1, 5, 2 ],
[ 2, 3, 1 ] ];
findIndex(query, arr, N, Q);
</script>
|
Time Complexity: O(N+Q)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...