A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a Java program to find the length of the longest consecutive sequence of a given array of integers
Q:

Write a Java program to find the length of the longest consecutive sequence of a given array of integers

0

Write a Java program to find the length of the longest consecutive sequence of a given array of integers

Original array: [1, 1, 2, 3, 3, 4, 5, 2, 4, 5, 6, 7, 8, 9, 6, -1, -2]
7

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.*;
public class Solution {	
 public static int longest_sequence(int[] nums) {
    if (nums == null) {
      throw new IllegalArgumentException("Null array..!");
    }
    if (nums.length == 0) {
      return 0;
    }
    boolean flag = false;
    int result = 0;
    int start = 0, end = 0;
    for (int i = 1; i < nums.length; i++) {
      if (nums[i - 1] < nums[i]) {
        end = i;
      } else {
        start = i;
      }
      if (end - start > result) {
        flag = true;
        result = end - start;
      }
    }
    if (flag) 
	{
      return result + 1;
    } 
	else 
	{
      return result;
    }
 }

   public static void main(String[] args) {
        int[] nums = { 1, 1, 2, 3, 3, 4, 5, 2, 4, 5, 6, 7, 8, 9, 6, -1, -2 };
		System.out.println("\nOriginal array: "+Arrays.toString(nums));  
		System.out.println(longest_sequence(nums));		
		}

}

Sample Output:

Original array: [1, 1, 2, 3, 3, 4, 5, 2, 4, 5, 6, 7, 8, 9, 6, -1, -2]
7

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now