📜  将数组转换为简化形式 |第 3 组(二分搜索)

📅  最后修改于: 2021-10-27 08:56:31             🧑  作者: Mango

给定一个由N 个不同整数组成的数组arr[] ,任务是将给定的数组转换为前 N 个非负整数的序列,即[0, N – 1]使得元素的顺序相同,即0放置在最小数组元素的索引处, 1放置在第二个最小元素的索引处,依此类推。

例子:

基于散列的方法:有关基于散列的方法,请参阅本文的 Set 1 帖子。
时间复杂度: O(N* log N)
辅助空间: O(N)

Vector Of Pairs Based Approach:有关使用对向量的方法,请参阅本文的 Set 2 帖子。
时间复杂度: O(N* log N)
辅助空间: O(N)

基于二分搜索的方法:按照以下步骤解决问题:

  • 初始化一个数组,比如brr[]并将数组arr[] 的所有元素存储到brr[] 中
  • 按升序对数组brr[]进行排序。
  • 遍历给定阵列ARR []以及对于每个元素,即,编曲[I]找到ARR [I]中的阵列的BRR LOWER_BOUND []和打印的是作为结果为当前元素的索引。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the reduced form
// of the given array arr[]
void convert(int arr[], int n)
{
    // Stores the sorted form of the
    // the given array arr[]
    int brr[n];
 
    for (int i = 0; i < n; i++)
        brr[i] = arr[i];
 
    // Sort the array brr[]
    sort(brr, brr + n);
 
    // Traverse the given array arr[]
    for (int i = 0; i < n; i++) {
 
        int l = 0, r = n - 1, mid;
 
        // Perform the Binary Search
        while (l <= r) {
 
            // Calculate the value of
            // mid
            mid = (l + r) / 2;
 
            if (brr[mid] == arr[i]) {
 
                // Print the current
                // index and break
                cout << mid << ' ';
                break;
            }
 
            // Update the value of l
            else if (brr[mid] < arr[i]) {
                l = mid + 1;
            }
 
            // Update the value of r
            else {
                r = mid - 1;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 20, 15, 12, 11, 50 };
    int N = sizeof(arr) / sizeof(arr[0]);
    convert(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG
{
 
    // Function to find the reduced form
    // of the given array arr[]
    static void convert(int arr[], int n)
    {
       
        // Stores the sorted form of the
        // the given array arr[]
        int brr[] = new int[n];
 
        for (int i = 0; i < n; i++)
            brr[i] = arr[i];
 
        // Sort the array brr[]
        Arrays.sort(brr);
 
        // Traverse the given array arr[]
        for (int i = 0; i < n; i++) {
 
            int l = 0, r = n - 1, mid;
 
            // Perform the Binary Search
            while (l <= r) {
 
                // Calculate the value of
                // mid
                mid = (l + r) / 2;
 
                if (brr[mid] == arr[i]) {
 
                    // Print the current
                    // index and break
                    System.out.print(mid + " ");
                    break;
                }
 
                // Update the value of l
                else if (brr[mid] < arr[i]) {
                    l = mid + 1;
                }
 
                // Update the value of r
                else {
                    r = mid - 1;
                }
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 10, 20, 15, 12, 11, 50 };
        int N = arr.length;
        convert(arr, N);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to find the reduced form
# of the given array arr[]
def convert(arr, n):
   
    # Stores the sorted form of the
    # the given array arr[]
    brr = [i for i in arr]
 
    # Sort the array brr[]
    brr = sorted(brr)
 
    # Traverse the given array arr[]
    for i in range(n):
 
        l, r, mid = 0, n - 1, 0
 
        # Perform the Binary Search
        while (l <= r):
 
            # Calculate the value of
            # mid
            mid = (l + r) // 2
 
            if (brr[mid] == arr[i]):
 
                # Prthe current
                # index and break
                print(mid,end=" ")
                break
            # Update the value of l
            elif (brr[mid] < arr[i]):
                l = mid + 1
            # Update the value of r
            else:
                r = mid - 1
 
# Driver Code
if __name__ == '__main__':
    arr=[10, 20, 15, 12, 11, 50]
    N = len(arr)
    convert(arr, N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the reduced form
// of the given array arr[]
static void convert(int[] arr, int n)
{
     
    // Stores the sorted form of the
    // the given array arr[]
    int[] brr = new int[n];
 
    for(int i = 0; i < n; i++)
        brr[i] = arr[i];
 
    // Sort the array brr[]
    Array.Sort(brr);
 
    // Traverse the given array arr[]
    for(int i = 0; i < n; i++)
    {
        int l = 0, r = n - 1, mid;
 
        // Perform the Binary Search
        while (l <= r)
        {
             
            // Calculate the value of
            // mid
            mid = (l + r) / 2;
 
            if (brr[mid] == arr[i])
            {
                 
                // Print the current
                // index and break
                Console.Write(mid + " ");
                break;
            }
 
            // Update the value of l
            else if (brr[mid] < arr[i])
            {
                l = mid + 1;
            }
 
            // Update the value of r
            else
            {
                r = mid - 1;
            }
        }
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 10, 20, 15, 12, 11, 50 };
    int N = arr.Length;
     
    convert(arr, N);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
0 4 3 2 1 5

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

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