检查数组元素在 O(n) 时间和 O(1) 空间中是否连续(处理正数和负数)
给定一个由不同数字组成的未排序数组,编写一个函数,如果数组由连续数字组成,则返回 true。
例子 :
Input : arr[] = {5, 4, 2, 1, 3}
Output : Yes
Input : arr[] = {2, 1, 0, -3, -1, -2}
Output : Yes
我们在下面的帖子中讨论了三种不同的方法
检查数组元素是否连续
在上面的帖子中。讨论了解决这个问题的三种方法,时间复杂度为 O(n),额外空间为 O(1),但该解决方案不能处理负数的情况。因此,在这篇文章中,将讨论一种时间复杂度为 O(n) 并使用 O(1) 空间的方法,该方法也将处理负数的情况。这里的一个重要假设是元素是不同的。
- 找到数组的总和。
- 如果给定的数组元素是连续的,则意味着它们在 AP 中。因此,找到最小元素,即 AP 的第一项,然后计算 ap_sum = n/2 * [2a +(n-1)*d] 其中 d = 1。所以,ap_sum = n/2 * [2a +(n-1) ]
- 比较两个总和。如果相等则打印 Yes,否则打印 No。
C++
// C++ program for above implementation
#include
using namespace std;
// Function to check if elements are consecutive
bool areConsecutives(int arr[], int n)
{
// Find minimum element in array
int first_term = *(min_element(arr, arr + n));
// Calculate AP sum
int ap_sum = (n * (2 * first_term + (n - 1) * 1)) / 2;
// Calculate given array sum
int arr_sum = 0;
for (int i = 0; i < n; i++)
arr_sum += arr[i];
// Compare both sums and return
return ap_sum == arr_sum;
}
// Drivers code
int main()
{
int arr[] = { 2, 1, 0, -3, -1, -2 };
int n = sizeof(arr) / sizeof(arr[0]);
areConsecutives(arr, n) ? cout << "Yes"
: cout << "No";
return 0;
}
Java
// Java program to check if array elements
// are consecutive
import java.io.*;
class GFG {
// Function to check if elements are
// consecutive
static Boolean areConsecutives(int arr[],
int n)
{
// Find minimum element in array
int first_term = Integer.MAX_VALUE;
for (int j = 0; j < n; j++)
{
if(arr[j] < first_term)
first_term = arr[j];
}
// Calculate AP sum
int ap_sum = (n * (2 * first_term +
(n - 1) * 1)) / 2;
// Calculate given array sum
int arr_sum = 0;
for (int i = 0; i < n; i++)
arr_sum += arr[i];
// Compare both sums and return
return ap_sum == arr_sum;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 1, 0, -3, -1, -2 };
int n = arr.length;
Boolean result = areConsecutives(arr, n);
if(result == true)
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Prerna Saini
Python 3
# Python 3 program for above
# implementation
import sys
# Function to check if elements
# are consecutive
def areConsecutives(arr, n):
# Find minimum element in array
first_term = sys.maxsize
for i in range(n):
if arr[i] < first_term:
first_term = arr[i]
# Calculate AP sum
ap_sum = ((n * (2 * first_term +
(n - 1) * 1)) // 2)
# Calculate given array sum
arr_sum = 0
for i in range( n):
arr_sum += arr[i]
# Compare both sums and return
return ap_sum == arr_sum
# Driver Code
arr = [ 2, 1, 0, -3, -1, -2 ]
n = len(arr)
if areConsecutives(arr, n):
print( "Yes")
else:
print( "No")
# This code is contributed
# by ChitraNayal
C#
// C# program to check if array
// elements are consecutive
using System;
class GFG {
// Function to check if elements
// are consecutive
static bool areConsecutives(int []arr,
int n)
{
// Find minimum element in array
int first_term = int.MaxValue;
for (int j = 0; j < n; j++)
{
if(arr[j] < first_term)
first_term = arr[j];
}
// Calculate AP sum
int ap_sum = (n * (2 * first_term +
(n - 1) * 1)) / 2;
// Calculate given array sum
int arr_sum = 0;
for (int i = 0; i < n; i++)
arr_sum += arr[i];
// Compare both sums and return
return ap_sum == arr_sum;
}
// Driver code
public static void Main()
{
int []arr = {2, 1, 0, -3, -1, -2};
int n = arr.Length;
bool result = areConsecutives(arr, n);
if(result == true)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
Yes