📜  检查给定数组是否为ak排序数组

📅  最后修改于: 2021-04-23 18:55:25             🧑  作者: Mango

给定n个不同元素的数组。检查给定数组是否为k排序数组。 k个排序数组是一个数组,其中每个元素距其在排序数组中的目标位置最多k个距离。
例如,让我们考虑k为2,在排序数组中索引7处的元素可以在给定数组中索引5、6、7、8、9处。

例子 :

Input : arr[] = {3, 2, 1, 5, 6, 4}, k = 2
Output : Yes
Every element is at most 2 distance away
from its target position in the sorted array.

Input : arr[] = {13, 8, 10, 7, 15, 14, 12}, k = 3
Output : No
13 is more than k = 3 distance away
from its target position in the sorted array.

将原始数组arr []的elements元素复制到辅助数组aux []
排序aux [] 。现在,对于arr []中索引i处的每个元素,使用Binary Search在aux []中找到其索引j 。如果对于任何元素k ,则arr []不是k排序的数组。否则,它是一个k排序数组。这里的绝对值是绝对值。

C++
// C++ implementation to check whether the given array
// is a k sorted array or not
#include 
using namespace std;
  
// function to find index of element 'x' in sorted 'arr'
// uses binary search technique
int binarySearch(int arr[], int low, int high, int x)
{
    while (low <= high)
    {
        int mid = (low + high) / 2;
          
        if (arr[mid] == x)
            return mid;
        else if (arr[mid] > x)
            high = mid - 1;
        else    
            low = mid + 1;    
    }
}
  
// function to check whether the given array is
// a 'k' sorted array or not
string isKSortedArray(int arr[], int n, int k)
{
    // auxiliary array 'aux'
    int aux[n];
      
    // copy elements of 'arr' to 'aux'
    for (int i = 0; i k, then that element is
        // not at-most k distance away from its 
        // target position. Thus,  'arr' is not a 
        // k sorted array 
        if (abs(i - j) > k)
            return "No";
    }
      
    // 'arr' is a k sorted array
    return "Yes";    
}
  
// Driver program to test above
int main()
{
    int arr[] = {3, 2, 1, 5, 6, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
    cout << "Is it a k sorted array?: "
         << isKSortedArray(arr, n, k);
    return 0;     
}


Java
// Java implementation to check whether the given array
// is a k sorted array or not
  
import java.util.Arrays;
  
class Test
{
    // Method to check whether the given array is
    // a 'k' sorted array or not
    static String isKSortedArray(int arr[], int n, int k)
    {
        // auxiliary array 'aux'
        int aux[] = new int[n];
           
        // copy elements of 'arr' to 'aux'
        for (int i = 0; i k, then that element is
            // not at-most k distance away from its 
            // target position. Thus,  'arr' is not a 
            // k sorted array 
            if (Math.abs(i - j) > k)
                return "No";
        }
           
        // 'arr' is a k sorted array
        return "Yes";    
    }
  
    // Driver method
    public static void main(String args[])
    {
        int arr[] = {3, 2, 1, 5, 6, 4};
        int k = 2;
          
        System.out.println("Is it a k sorted array ?: " +
                            isKSortedArray(arr, arr.length, k));
    }
}


Python3
# Python 3 implementation to check 
# whether the given array is a k 
# sorted array or not
  
# function to find index of element 
# 'x' in sorted 'arr' uses binary 
# search technique
def binarySearch(arr, low, high, x):
    while (low <= high):
        mid = int((low + high) / 2)
          
        if (arr[mid] == x):
            return mid
        elif(arr[mid] > x):
            high = mid - 1
        else:
            low = mid + 1
  
# function to check whether the given 
# array is a 'k' sorted array or not
def isKSortedArray(arr, n, k):
      
    # auxiliary array 'aux'
    aux = [0 for i in range(n)]
      
    # copy elements of 'arr' to 'aux'
    for i in range(0, n, 1):
        aux[i] = arr[i]
      
    # sort 'aux' 
    aux.sort(reverse = False)
      
    # for every element of 'arr' at 
    # index 'i', find its index 'j' in 'aux'
    for i in range(0, n, 1):
          
        # index of arr[i] in sorted
        # array 'aux'
        j = binarySearch(aux, 0, n - 1, arr[i])
          
        # if abs(i-j) > k, then that element is
        # not at-most k distance away from its 
        # target position. Thus, 'arr' is not a 
        # k sorted array 
        if (abs(i - j) > k):
            return "No"
      
    # 'arr' is a k sorted array
    return "Yes"
  
# Driver Code
if __name__ == '__main__':
    arr = [3, 2, 1, 5, 6, 4]
    n = len(arr)
    k = 2
    print("Is it a k sorted array?:", 
           isKSortedArray(arr, n, k))
  
# This code is contributed by
# Shashank_Sharma


C#
// C# implementation to check 
// whether the given array is a 
// k sorted array or not
using System;
using System.Collections;
  
class GFG {
      
    // Method to check whether the given
    // array is a 'k' sorted array or not
    static String isKSortedArray(int []arr, int n, int k)
    {
        // auxiliary array 'aux'
        int []aux = new int[n];
          
        // copy elements of 'arr' to 'aux'
        for (int i = 0; i k, then that element is
            // not at-most k distance away from its 
            // target position. Thus, 'arr' is not a 
            // k sorted array 
            if (Math.Abs(i - j) > k)
                return "No";
        }
          
        // 'arr' is a k sorted array
        return "Yes"; 
    }
  
    // Driver method
    public static void Main()
    {
        int []arr = {3, 2, 1, 5, 6, 4};
        int k = 2;
          
        Console.WriteLine("Is it a k sorted array ?: " +
                           isKSortedArray(arr, arr.Length, k));
    }
}
  
// This code is contributed by Sam007


输出:

Is it a k sorted array?: Yes

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