📌  相关文章
📜  在给定的N个范围内搜索元素

📅  最后修改于: 2021-04-29 16:29:22             🧑  作者: Mango

给定一个由N个排序范围和一个数字K组成的数组。任务是找到K所在范围的索引。如果K不在任何给定范围内,则打印-1
注意:给定的范围均不重合。

例子:

幼稚的方法:可以按照以下步骤解决上述问题。

  • 遍历所有范围。
  • 检查条件K> = arr [i] .first && K <= arr [i] .second在任何迭代中是否成立。
  • 如果数字K不在给定范围内,则打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the index of the range
// in which K lies and uses linear search
int findNumber(pair a[], int n, int K)
{
  
    // Iterate and find the element
    for (int i = 0; i < n; i++) {
  
        // If K lies in the current range
        if (K >= a[i].first && K <= a[i].second)
            return i;
    }
  
    // K doesn't lie in any of the given ranges
    return -1;
}
  
// Driver code
int main()
{
    pair a[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 6;
    int index = findNumber(a, n, k);
    if (index != -1)
        cout << index;
    else
        cout << -1;
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
static class pair 
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to return the index 
// of the range in which K lies 
// and uses linear search
static int findNumber(pair a[], 
                      int n, int K)
{
  
    // Iterate and find the element
    for (int i = 0; i < n; i++)
    {
  
        // If K lies in the current range
        if (K >= a[i].first && 
            K <= a[i].second)
            return i;
    }
  
    // K doesn't lie in any 
    // of the given ranges
    return -1;
}
  
// Driver code
public static void main(String[] args)
{
    pair a[] = {new pair(1, 3 ), 
                new pair(4, 7 ),
                new pair(8, 11 )};
    int n = a.length;
    int k = 6;
    int index = findNumber(a, n, k);
    if (index != -1)
        System.out.println(index);
    else
        System.out.println(-1);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python 3 implementation of the approach
  
# Function to return the index of the range
# in which K lies and uses linear search
def findNumber(a, n, K):
      
    # Iterate and find the element
    for i in range(0, n, 1):
          
        # If K lies in the current range
        if (K >= a[i][0] and K <= a[i][1]):
            return i
  
    # K doesn't lie in any of the
    # given ranges
    return -1
  
# Driver code
if __name__ == '__main__':
    a = [[1, 3], [4, 7], [8, 11]]
    n = len(a)
    k = 6
    index = findNumber(a, n, k)
    if (index != -1):
        print(index, end = "")
    else:
        print(-1, end = "")
          
# This code is contributed by 
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
      
class pair 
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to return the index 
// of the range in which K lies 
// and uses linear search
static int findNumber(pair []a, 
                    int n, int K)
{
  
    // Iterate and find the element
    for (int i = 0; i < n; i++)
    {
  
        // If K lies in the current range
        if (K >= a[i].first && 
            K <= a[i].second)
            return i;
    }
  
    // K doesn't lie in any 
    // of the given ranges
    return -1;
}
  
// Driver code
public static void Main(String[] args)
{
    pair []a = {new pair(1, 3 ), 
                new pair(4, 7 ),
                new pair(8, 11 )};
    int n = a.Length;
    int k = 6;
    int index = findNumber(a, n, k);
    if (index != -1)
        Console.WriteLine(index);
    else
        Console.WriteLine(-1);
}
}
  
// This code is contributed by 29AjayKumar


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the index of the range
// in which K lies and uses binary search
int findNumber(pair a[], int n, int K)
{
  
    int low = 0, high = n - 1;
  
    // Binary search
    while (low <= high) {
  
        // Find the mid element
        int mid = (low + high) >> 1;
  
        // If element is found
        if (K >= a[mid].first && K <= a[mid].second)
            return mid;
  
        // Check in first half
        else if (K < a[mid].first)
            high = mid - 1;
  
        // Check in second half
        else
            low = mid + 1;
    }
  
    // Not found
    return -1;
}
  
// Driver code
int main()
{
    pair a[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 6;
    int index = findNumber(a, n, k);
    if (index != -1)
        cout << index;
    else
        cout << -1;
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG 
{
static class pair 
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to return the index of the range 
// in which K lies and uses binary search 
static int findNumber(pair a[], int n, int K) 
{ 
    int low = 0, high = n - 1; 
  
    // Binary search 
    while (low <= high) 
    { 
  
        // Find the mid element 
        int mid = (low + high) >> 1; 
  
        // If element is found 
        if (K >= a[mid].first && 
            K <= a[mid].second) 
            return mid; 
  
        // Check in first half 
        else if (K < a[mid].first) 
            high = mid - 1; 
  
        // Check in second half 
        else
            low = mid + 1; 
    } 
  
    // Not found 
    return -1; 
} 
  
// Driver code 
public static void main(String[] args)
{
    pair a[] = { new pair(1, 3), 
                 new pair(4, 7), 
                 new pair(8, 11) }; 
    int n = a.length; 
    int k = 6; 
    int index = findNumber(a, n, k); 
    if (index != -1) 
        System.out.println(index);
    else
        System.out.println(-1);
    }
}
  
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
  
# Function to return the index of the range
# in which K lies and uses binary search
def findNumber(a, n, K):
  
    low = 0
    high = n - 1
  
    # Binary search
    while (low <= high):
  
        # Find the mid element
        mid = (low + high) >> 1
  
        # If element is found
        if (K >= a[mid][0] and K <= a[mid][1]):
            return mid
  
        # Check in first half
        elif (K < a[mid][0]):
            high = mid - 1
  
        # Check in second half
        else:
            low = mid + 1
  
    # Not found
    return -1
  
# Driver code
a= [ [ 1, 3 ], [ 4, 7 ], [ 8, 11 ] ]
n = len(a)
k = 6
index = findNumber(a, n, k)
if (index != -1):
    print(index)
else:
    print(-1)
  
# This code is contributed by mohit kumar


C#
// C# implementation of the above approach
using System;
class GFG 
{
public class pair 
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to return the index of the range 
// in which K lies and uses binary search 
static int findNumber(pair []a, int n, int K) 
{ 
    int low = 0, high = n - 1; 
  
    // Binary search 
    while (low <= high) 
    { 
  
        // Find the mid element 
        int mid = (low + high) >> 1; 
  
        // If element is found 
        if (K >= a[mid].first && 
            K <= a[mid].second) 
            return mid; 
  
        // Check in first half 
        else if (K < a[mid].first) 
            high = mid - 1; 
  
        // Check in second half 
        else
            low = mid + 1; 
    } 
  
    // Not found 
    return -1; 
} 
  
// Driver code 
public static void Main(String[] args)
{
    pair []a = {new pair(1, 3), 
                new pair(4, 7), 
                new pair(8, 11)}; 
    int n = a.Length; 
    int k = 6; 
    int index = findNumber(a, n, k); 
    if (index != -1) 
        Console.WriteLine(index);
    else
        Console.WriteLine(-1);
    }
}
  
// This code is contributed by Rajput-Ji


输出:
1

时间复杂度: O(N)

高效方法:二进制搜索可用于在O(log N)中查找元素。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the index of the range
// in which K lies and uses binary search
int findNumber(pair a[], int n, int K)
{
  
    int low = 0, high = n - 1;
  
    // Binary search
    while (low <= high) {
  
        // Find the mid element
        int mid = (low + high) >> 1;
  
        // If element is found
        if (K >= a[mid].first && K <= a[mid].second)
            return mid;
  
        // Check in first half
        else if (K < a[mid].first)
            high = mid - 1;
  
        // Check in second half
        else
            low = mid + 1;
    }
  
    // Not found
    return -1;
}
  
// Driver code
int main()
{
    pair a[] = { { 1, 3 }, { 4, 7 }, { 8, 11 } };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 6;
    int index = findNumber(a, n, k);
    if (index != -1)
        cout << index;
    else
        cout << -1;
  
    return 0;
}

Java

// Java implementation of the approach 
class GFG 
{
static class pair 
{ 
    int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to return the index of the range 
// in which K lies and uses binary search 
static int findNumber(pair a[], int n, int K) 
{ 
    int low = 0, high = n - 1; 
  
    // Binary search 
    while (low <= high) 
    { 
  
        // Find the mid element 
        int mid = (low + high) >> 1; 
  
        // If element is found 
        if (K >= a[mid].first && 
            K <= a[mid].second) 
            return mid; 
  
        // Check in first half 
        else if (K < a[mid].first) 
            high = mid - 1; 
  
        // Check in second half 
        else
            low = mid + 1; 
    } 
  
    // Not found 
    return -1; 
} 
  
// Driver code 
public static void main(String[] args)
{
    pair a[] = { new pair(1, 3), 
                 new pair(4, 7), 
                 new pair(8, 11) }; 
    int n = a.length; 
    int k = 6; 
    int index = findNumber(a, n, k); 
    if (index != -1) 
        System.out.println(index);
    else
        System.out.println(-1);
    }
}
  
// This code is contributed by Princi Singh

Python3

# Python3 implementation of the approach
  
# Function to return the index of the range
# in which K lies and uses binary search
def findNumber(a, n, K):
  
    low = 0
    high = n - 1
  
    # Binary search
    while (low <= high):
  
        # Find the mid element
        mid = (low + high) >> 1
  
        # If element is found
        if (K >= a[mid][0] and K <= a[mid][1]):
            return mid
  
        # Check in first half
        elif (K < a[mid][0]):
            high = mid - 1
  
        # Check in second half
        else:
            low = mid + 1
  
    # Not found
    return -1
  
# Driver code
a= [ [ 1, 3 ], [ 4, 7 ], [ 8, 11 ] ]
n = len(a)
k = 6
index = findNumber(a, n, k)
if (index != -1):
    print(index)
else:
    print(-1)
  
# This code is contributed by mohit kumar

C#

// C# implementation of the above approach
using System;
class GFG 
{
public class pair 
{ 
    public int first, second; 
    public pair(int first, int second) 
    { 
        this.first = first; 
        this.second = second; 
    } 
} 
  
// Function to return the index of the range 
// in which K lies and uses binary search 
static int findNumber(pair []a, int n, int K) 
{ 
    int low = 0, high = n - 1; 
  
    // Binary search 
    while (low <= high) 
    { 
  
        // Find the mid element 
        int mid = (low + high) >> 1; 
  
        // If element is found 
        if (K >= a[mid].first && 
            K <= a[mid].second) 
            return mid; 
  
        // Check in first half 
        else if (K < a[mid].first) 
            high = mid - 1; 
  
        // Check in second half 
        else
            low = mid + 1; 
    } 
  
    // Not found 
    return -1; 
} 
  
// Driver code 
public static void Main(String[] args)
{
    pair []a = {new pair(1, 3), 
                new pair(4, 7), 
                new pair(8, 11)}; 
    int n = a.Length; 
    int k = 6; 
    int index = findNumber(a, n, k); 
    if (index != -1) 
        Console.WriteLine(index);
    else
        Console.WriteLine(-1);
    }
}
  
// This code is contributed by Rajput-Ji
输出:
1

时间复杂度: O(log N)