打印最长递增的连续子数组
给定一个大小为N的数组arr[] ,任务是打印最长的递增子数组,使得子数组中的元素是连续的整数。
例子:
Input: arr[] = {1, 9, 3, 4, 20, 2}
Output: {3, 4}
Explanation: The subarray {3, 4} is the longest subarray of consecutive elements
Input: arr[] = {36, 41, 56, 32, 33, 34, 35, 43, 32, 42}
Output: {32, 33, 34, 35}
Explanation: The subarray {32, 33, 34, 35} is the longest subarray of consecutive elements
方法:这个想法是运行一个循环并保持一个计数和最大值(最初都是零)。请按照以下步骤操作:
- 从头到尾运行一个循环
- 如果当前元素不等于(前一个元素+1 ),则将计数设置为1 ,并更新窗口的起点和终点
- 否则增加计数
- 最后,打印窗口的元素
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the longest
// consecutive subarray
void findLongestConseqSubarr(vector& v)
{
int ans = 0, count = 0,
start = 0, end = 0, x, y;
// Find the maximum length
// by traversing the array
for (int i = 0; i < v.size(); i++) {
// Check if the current element
// is equal to previous element + 1
if (i > 0 && v[i] == v[i - 1] + 1) {
count++;
end = i;
}
// Reset the count
else {
start = i;
count = 1;
}
// Update the maximum
if (ans < count) {
ans = count;
x = start;
y = end;
}
}
for (int i = x; i <= y; i++)
cout << v[i] << ", ";
}
// Driver Code
int main()
{
vector arr = { 1, 9, 3, 4, 20, 2 };
findLongestConseqSubarr(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to return the longest
// consecutive subarray
static void findLongestConseqSubarr(int arr[ ])
{
int ans = 0, count = 0, start = 0, end = 0, x = 0, y = 0;
// Find the maximum length
// by traversing the array
for (int i = 0; i < arr.length; i++) {
// Check if the current element
// is equal to previous element + 1
if (i > 0 && arr[i] == arr[i - 1] + 1) {
count++;
end = i;
}
// Reset the count
else {
start = i;
count = 1;
}
// Update the maximum
if (ans < count) {
ans = count;
x = start;
y = end;
}
}
for (int i = x; i <= y; i++)
System.out.print(arr[i] + ", ");
}
public static void main (String[] args) {
int arr[ ] = { 1, 9, 3, 4, 20, 2 };
findLongestConseqSubarr(arr);
}
}
// This code is contributed by hrithikgarg03188
Python3
# Python program for the above approach
# Function to return the longest
# consecutive subarray
def findLongestConseqSubarr(v):
ans = 0
count = 0
start = 0
end = 0
# Find the maximum length
# by traversing the array
for i in range(0, len(v)):
# Check if the current element
# is equal to previous element + 1
if (i > 0 and v[i] == v[i - 1] + 1):
count = count + 1
end = i
# Reset the count
else:
start = i
count = 1
# Update the maximum
if (ans < count):
ans = count
x = start
y = end
for i in range(x, y + 1):
print(v[i], end = ", ")
# Driver Code
arr = [1, 9, 3, 4, 20, 2]
findLongestConseqSubarr(arr)
# This code is contributed by Taranpreet
C#
// C# program for the above approach
using System;
class GFG
{
// Function to return the longest
// consecutive subarray
static void findLongestConseqSubarr(int[] arr)
{
int ans = 0, count = 0, start = 0, end = 0, x = 0, y = 0;
// Find the maximum length
// by traversing the array
for (int i = 0; i < arr.Length; i++)
{
// Check if the current element
// is equal to previous element + 1
if (i > 0 && arr[i] == arr[i - 1] + 1)
{
count++;
end = i;
}
// Reset the count
else
{
start = i;
count = 1;
}
// Update the maximum
if (ans < count)
{
ans = count;
x = start;
y = end;
}
}
for (int i = x; i <= y; i++)
Console.Write(arr[i] + ", ");
}
// Driver code
public static void Main()
{
int[] arr = { 1, 9, 3, 4, 20, 2 };
findLongestConseqSubarr(arr);
}
}
// This code is contributed by Saurabh Jaiswal
Javascript
输出
3, 4,
时间复杂度: O(N)
辅助空间: O(1)