Arrays 阵列中的稀有元素

Arrays 阵列中的稀有元素,arrays,algorithm,performance,sorting,divide-and-conquer,Arrays,Algorithm,Performance,Sorting,Divide And Conquer,我们得到一个由n个整数组成的排序数组a[1..n]。我们说元素xea如果出现是很罕见的 在严格小于n/10倍的情况下。也就是说,如果有一些索引1,那么x是罕见的。是的,可以在O(logn)中进行。我假设您的内存中已经有了这个数组。否则,它不可能比在O(n)中更快地完成,因为您至少需要读取数组 假设step是小于n/10的最大整数。如果step等于零,那么我们显然没有稀有元素 考虑以下算法: int start = 1; while (true) { if (n - start + 1 <

我们得到一个由n个整数组成的排序数组a[1..n]。我们说元素xea如果出现是很罕见的
在严格小于n/10倍的情况下。也就是说,如果有一些索引1,那么x是罕见的。是的,可以在
O(logn)
中进行。我假设您的内存中已经有了这个数组。否则,它不可能比在
O(n)
中更快地完成,因为您至少需要读取数组

假设
step
是小于
n/10
的最大整数。如果
step
等于零,那么我们显然没有稀有元素

考虑以下算法:

int start = 1;
while (true) {
  if (n - start + 1 <= step) {
    OutputRare(A[start]); Exit;
  }
  int next_index = start + step;
  if (A[start] != A[next_index]) {
    OutputRare(A[start); Exit;
  }
  // Here we need to find the smallest index starting from start with
  // element that is not equal to A[start]. If such element does not
  // exist function returns -1.
  next_index = FindFirstNonEqual(A[start], start);
  if (next_index == -1) {
    // There is no rare elements
    Exit;
  }
  start = next_index;
}
int start=1;
while(true){

如果(n-start+1)听起来像家庭作业,那么问题是什么?用问题更新。在家做这个工作。所以,是的,这是家庭作业。不是作业,如果你是这个意思的话。