📌  相关文章
📜  检查数组是否包含允许重复的连续整数

📅  最后修改于: 2021-05-04 11:01:38             🧑  作者: Mango

给定一个由N个整数组成的数组Arr (允许重复)。如果它是一组连续的整数,则打印“是”,否则打印“否”。

例子:

方法:

  1. 这里讨论了类似的方法。在继续本文之前,建议先仔细阅读一下。
  2. 从数组中找到最大和最小元素。
  3. 使用与数组中max元素的差值更新数组。
  4. 遍历数组并执行以下操作
    • 如果0 <= abs(arr [i])-1)arr [abs(arr [i])-1)]> 0表示元素为正(第一次访问),请通过执行arr [使其为负abs(arr [i])-1] = -arr [abs(arr [i])-1]
    • 否则继续循环意味着该值已被访问或超出数组范围
  5. 再次遍历循环并检查是否有任何元素为正,表示元素的差大于1
    中断循环并显示“否”
  6. 如果没有发现任何元素为正,则打印“是”

下面是实现代码:

C++
// C++ program to check if
// array contains contiguous
// integers with duplicates
// allowed in O(1) space
#include 
using namespace std;
  
// Function to return true or
// false
bool check(int arr[], int n)
{
  
    int k = INT_MIN;
    int r = INT_MAX;
  
    // To find the max and min
    // in the array
    for (int i = 0; i < n; i++) {
        k = max(k, arr[i]);
        r = min(r, arr[i]);
    }
  
    k += 1;
  
    // Update the array with
    // the difference from
    // the max element
    for (int i = 0; i < n; i++)
        arr[i] = k - arr[i];
  
    for (int i = 0; i < n; i++) {
  
        // if the element is positive
        // and less than the size of
        // array(in range), make it negative
        if (abs(arr[i]) - 1 < n
            && arr[abs(arr[i]) - 1] > 0) {
            arr[abs(arr[i]) - 1]
                = -arr[abs(arr[i]) - 1];
        }
    }
  
    int flag = 0;
  
    // Loop from 0 to end of the array
    for (int i = 0; i <= k - r - 1; i++) {
  
        // Found positive, out of range
        if (arr[i] > 0) {
            flag = 1;
            break;
        }
    }
  
    return flag == 0;
}
  
// Driver function
int main()
{
  
    // Given array
    int arr[] = { 5, 2, 3, 6, 4, 4, 6, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function Calling
    if (check(arr, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to check if array contains 
// contiguous integers with duplicates
// allowed in O(1) space
import java.util.*;
  
class GFG
{
      
// Function to return true or
// false
static boolean check(int arr[], int n)
{
  
    int k = Integer.MIN_VALUE;
    int r = Integer.MAX_VALUE;
  
    // To find the max and min
    // in the array
    for (int i = 0; i < n; i++)
    {
        k = Math.max(k, arr[i]);
        r = Math.min(r, arr[i]);
    }
  
    k += 1;
  
    // Update the array with
    // the difference from
    // the max element
    for (int i = 0; i < n; i++)
        arr[i] = k - arr[i];
  
    for (int i = 0; i < n; i++)
    {
  
        // if the element is positive
        // and less than the size of
        // array(in range), make it negative
        if (Math.abs(arr[i]) - 1 < n && 
        arr[Math.abs(arr[i]) - 1] > 0) 
        {
            arr[Math.abs(arr[i]) - 1] = -arr[Math.abs(arr[i]) - 1];
        }
    }
  
    int flag = 0;
  
    // Loop from 0 to end of the array
    for (int i = 0; i <= k - r - 1; i++)
    {
  
        // Found positive, out of range
        if (arr[i] > 0)
        {
            flag = 1;
            break;
        }
    }
    return flag == 0;
}
  
// Driver function
public static void main(String []args)
{
  
    // Given array
    int arr[] = { 5, 2, 3, 6, 4, 4, 6, 6 };
    int n = arr.length;
  
    // Function Calling
    if (check(arr, n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by Surendra_Gangwar


Python3
# Python3 program to check if array contains 
# contiguous integers with duplicates allowed 
# in O(1) space
  
# Function to return true or false
def check(arr, n):
  
    k = -10**9
    r = 10**9
  
    # To find the max and min in the array
    for i in range(n):
        k = max(k, arr[i])
        r = min(r, arr[i])
  
    k += 1
  
    # Update the array with the difference 
    # from the max element
    for i in range(n):
        arr[i] = k - arr[i]
  
    for i in range(n):
  
        # if the element is positive
        # and less than the size of
        # array(in range), make it negative
        if (abs(arr[i]) - 1 < n and 
                arr[abs(arr[i]) - 1] > 0):
            arr[abs(arr[i]) - 1]= -arr[abs(arr[i]) - 1]
  
    flag = 0
  
    # Loop from 0 to end of the array
    for i in range(k - r):
  
        # Found positive, out of range
        if (arr[i] > 0):
            flag = 1
            break
  
    return flag == 0
  
# Driver Code
  
# Given array
arr = [5, 2, 3, 6, 4, 4, 6, 6]
n = len(arr)
  
# Function Calling
if (check(arr, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by Mohit Kumar


C#
// C# program to check if array contains 
// contiguous integers with duplicates
// allowed in O(1) space
using System;
  
class GFG
{
      
// Function to return true or
// false
static bool check(int []arr, int n)
{
    int k = int.MinValue;
    int r = int.MaxValue;
  
    // To find the max and min
    // in the array
    for (int i = 0; i < n; i++)
    {
        k = Math.Max(k, arr[i]);
        r = Math.Min(r, arr[i]);
    }
  
    k += 1;
  
    // Update the array with
    // the difference from
    // the max element
    for (int i = 0; i < n; i++)
        arr[i] = k - arr[i];
  
    for (int i = 0; i < n; i++)
    {
  
        // if the element is positive
        // and less than the size of
        // array(in range), make it negative
        if (Math.Abs(arr[i]) - 1 < n && 
        arr[Math.Abs(arr[i]) - 1] > 0) 
        {
            arr[Math.Abs(arr[i]) - 1] = -
                         arr[Math.Abs(arr[i]) - 1];
        }
    }
  
    int flag = 0;
  
    // Loop from 0 to end of the array
    for (int i = 0; i <= k - r - 1; i++)
    {
  
        // Found positive, out of range
        if (arr[i] > 0)
        {
            flag = 1;
            break;
        }
    }
    return flag == 0;
}
  
// Driver Code
static public void Main ()
{
  
    // Given array
    int []arr = { 5, 2, 3, 6, 4, 4, 6, 6 };
    int n = arr.Length;
      
    // Function Calling
    if (check(arr, n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by ajit


输出:

Yes

时间复杂度: O(N)
空间复杂度: O(1)多余的空间