在 O(1) 空间中查找具有元素 0 到 N-1 的常量数组中的重复项
给定一个由 n 个元素组成的常量数组,其中包含从 1 到 n-1 的元素,其中任何一个数字出现任意次数。在 O(n) 中找到这些重复数字中的任何一个,并且只使用恒定的内存空间。
例子:
Input : arr[] = {1, 2, 3, 4, 5, 6, 3}
Output : 3
由于给定的数组是下面文章中给出的常量方法将不起作用。
- 在 O(n) 时间和 O(1) 额外空间内查找重复项 |设置 1
- 在 O(n) 中的数组中重复并使用 O(1) 额外空间 |第 2 组
- 我们从 0 开始取两个变量 i & j
- 我们将循环运行,直到我到达最后一个元素或找到重复的元素
- 我们将预先增加 j 值,以便我们可以将 elem 与下一个 elem 进行比较
- 如果我们没有找到 elem,我们将增加 i,因为 j 将指向最后一个 elem,然后用 i 重新定位 j
Java
// Java program to find a duplicate
// element in an array with values in
// range from 0 to n-1.
import java.io.*;
import java.util.*;
public class GFG {
// function to find one duplicate
static int findduplicate(int []a, int n)
{
int i=0,j=0;
while(i
Java
// Java program to find a duplicate
// element in an array with values in
// range from 0 to n-1.
import java.io.*;
import java.util.*;
public class GFG {
// function to find one duplicate
static int findduplicate(int []arr, int n)
{
// return -1 because in these cases
// there can not be any repeated element
if (n <= 1)
return -1;
// initialize fast and slow
int slow = arr[0];
int fast = arr[arr[0]];
// loop to enter in the cycle
while (fast != slow)
{
// move one step for slow
slow = arr[slow];
// move two step for fast
fast = arr[arr[fast]];
}
// loop to find entry
// point of the cycle
fast = 0;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
// Driver Code
public static void main(String args[])
{
int []arr = {1, 2, 3, 4, 5, 6, 3};
int n = arr.length;
System.out.print(findduplicate(arr, n));
}
}
// This code is contributed by
// Manish Shaw (manishshaw1)
Python 3
# Python 3 program to find a duplicate
# element in an array with values in
# range from 0 to n-1.
# function to find one duplicate
def findduplicate(arr, n):
# return -1 because in these cases
# there can not be any repeated element
if (n <= 1):
return -1
# initialize fast and slow
slow = arr[0]
fast = arr[arr[0]]
# loop to enter in the cycle
while (fast != slow) :
# move one step for slow
slow = arr[slow]
# move two step for fast
fast = arr[arr[fast]]
# loop to find entry point of the cycle
fast = 0
while (slow != fast):
slow = arr[slow]
fast = arr[fast]
return slow
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5, 6, 3 ]
n = len(arr)
print(findduplicate(arr, n))
# This code is contributed by ita_c
C#
// C# program to find a duplicate
// element in an array with values in
// range from 0 to n-1.
using System;
using System.Collections.Generic;
class GFG {
// function to find one duplicate
static int findduplicate(int []arr, int n)
{
// return -1 because in these cases
// there can not be any repeated element
if (n <= 1)
return -1;
// initialize fast and slow
int slow = arr[0];
int fast = arr[arr[0]];
// loop to enter in the cycle
while (fast != slow)
{
// move one step for slow
slow = arr[slow];
// move two step for fast
fast = arr[arr[fast]];
}
// loop to find entry
// point of the cycle
fast = 0;
while (slow != fast)
{
slow = arr[slow];
fast = arr[fast];
}
return slow;
}
// Driver Code
public static void Main()
{
int []arr = {1, 2, 3, 4, 5, 6, 3};
int n = arr.Length;
Console.Write(findduplicate(arr, n));
}
}
// This code is contributed by
// Manish Shaw (manishshaw1)
PHP
Javascript
输出:
3
时间复杂度: O(n)
辅助空间: O(1)