简体   繁体   English

在o(1)的整数数组中找到i和j之间的元素数

[英]Finding the number of elements between i and j in an integer array in o(1)

Given an unsorted integer array and 2 numbers i and j such that 0 <= i <= j <= C(a constant say MAX_INTEGER) what kind of pre-processing can be performed on it so that you will be able to find the number of numbers between i and j(both inclusive) in o(1) time. 给定一个未排序的整数数组以及2个数字i和j,使得0 <= i <= j <= C(一个常量,表示MAX_INTEGER)可以对其执行哪种预处理,以便您可以找到该数字在o(1)时间内i和j(包括两端)之间的数字的和。 The array can also have duplicates. 该数组也可以有重复项。

I had thought of building a frequency array f[] for the elements in the array(space o(C)) and also another array cf[] for cumulative frequency(space o(C)). 我曾考虑过为数组(空间o(C))中的元素建立一个频率数组f [],以及为累积频率(空间o(C))建立另一个数组cf []。

So given i and j, i can look up the cumulative frequency array and do cf[j] - cf[i] - This will give the number of elements between i and j. 因此,给定i和j,我可以查找累积频率阵列并执行cf [j]-cf [i]-这将给出i和j之间的元素数量。 To include i and j, look up the frequency array and add the values. 要包括i和j,请查找频率数组并添加值。 ie cf[j] - cf[i] + f[i]+f[j] Time complexity will be o(1) * 4 = constant time. 即cf [j]-cf [i] + f [i] + f [j]时间复杂度将为o(1)* 4 =恒定时间。

The look up in the frequency array can be avoided by finding the previous non zero cf array element for both i and j in the respective direction. 可以通过在相应方向上为i和j查找先前的非零cf数组元素来避免在频率数组中查找。 This will increase the time complexity but will reduce the space complexity. 这将增加时间复杂度,但将减少空间复杂度。

Wanted to know if there is a better solution for this problem. 想知道是否有针对此问题的更好解决方案。

Note - Values of i and j will be available to you only after the pre-processing is completed. 注意-i和j的值只有在预处理完成后才可用。

-Vijay -Vijay

I can't imagine how you'd do this in O(1) without using O(C) additional space. 我无法想象在不使用O(C)额外空间的情况下如何在O(1)中执行此操作。

You can do the lookup in O(log n) very easily if you just create a sorted copy of the array on startup. 如果仅在启动时创建数组的排序副本,则可以非常轻松地在O(log n)中进行查找。 (O(n log n)). (O(n log n))。

The lookup then becomes: 查找将变为:

Binary search to find the first occurrence of i
Binary search to find the last occurrence of j
result = position_of_j - position_of_i + 1

Now, if the range of items in the array is relatively small, you could do it in O(max - min + 1) extra space and get O(1) lookup. 现在,如果数组中的项范围相对较小,则可以在O(max-min + 1)额外空间中进行操作并获得O(1)查找。 But worst case, (max - min + 1) == C . 但最坏的情况是(max - min + 1) == C

How about this, 这个怎么样,

First off, sort the integer array. 首先,对整数数组进行排序。 and create a hash table with a key for every unique integer in the array and the value as indices in the array at which that integer occurs first and last in the sorted array,(since dup is possible). 并为该数组中的每个唯一整数创建一个带有键的哈希表,并将该值作为数组中索引的值,该整数在已排序数组中的第一个和最后一个出现(因为dup是可能的)。 Space complexity of hash table would be O(n) and access complexity is constant, you have to allocate the space for hash table accordingly. 哈希表的空间复杂度为O(n),访问复杂度为常数,因此必须为哈希表分配空间。

Given these extra data structures, if you want to find out the range of numbers between i and j, get first index of i and get last index of j, from the hash table and subtract the first from the latter to get the result. 给定这些额外的数据结构,如果您想找出i和j之间的数字范围,请从哈希表中获取i的第一个索引,并获取j的最后一个索引,然后从哈希表中减去第一个索引以得到结果。

Dim result() as integer
Dim C(1000) as integer
Dim Buff() as integer
Dim i as integer=50 
dim j as integer=450 
for count =0 to c.count-1
    if c(count)>i and c(count)<j then 
       dim length as integer=buff.count-1
       redim result(lenght+1)
       for buffcount=0 to buff.count
           result(buffcount)=buff(buffcount)
       next  
       result(result.count-1)=c(count)
       redim buff(result.lenght-1)
       buff=result
    end if 
next 

Idea: 理念:

 1. Binary search to find the first occurrence of i               
 2. Binary search to find the last occurrence of j       
 3. result = position_of_j - position_of_i + 1

or 要么

1. first enter size of array    
2. then enter elements of array    
3. then query size    
4. then enter left, right

code link : HackerEarth 代码链接: HackerEarth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找数组中A [i] * A [j]&gt; A [i] + A [j]的对数 - Finding number of pairs in array where A[i] * A[j] > A[i] + A[j] 创建一个方法,该方法接受一个整数数组,该整数数组填充元素之间缺少的数字 - create a method that accepts an integer array that fills in the missing number between the elements C#:O(n²) 解决方案,用于查找大小为 n 且数字为 k 的数组的 n/k 个元素 - C#: O(n²) solution to finding n/k elements of an array of of size n and a number k C:查找数组中的元素数量[] - C: finding the number of elements in an array[] 查找数组中元素之间的“距离” - Finding 'distance' between elements in an array 找出数组中三元组i,j,k的数量,以使索引为i到j-1的元素的异或等于索引为j到k的元素的异或 - Find the number of triplets i,j,k in an array such that the xor of elements indexed i to j-1 is equal to the xor of elements indexed j to k 在 O(j) 中打印元素? - Print elements in `O(j)`? 在整数数组中找到一对元素,以使abs(v [i] -v [j])最小化 - Find pair of elements in integer array such that abs(v[i]-v[j]) is minimized 给定数字X,在两个排序的数组中找到两个元素,例如A [i] + B [j] = X在O(n + m)中 - Given a number X , find two elements in two sorted arrays such A[i]+B[j] = X in O(n+m) 如果数组中有整数索引元素,则Bash获取数字 - Bash get the number if integer indexed elements in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM