3: Longest Streak Again [10 marks] In the provided Problem3.java file, complete
ID: 3873424 • Letter: 3
Question
3: Longest Streak Again [10 marks] In the provided Problem3.java file, complete the method called longestStreak. There should be no other methods or attributes in your class. The contract (specification) of the method is given below public static int[] longestStreak (boolean [] values) /*Purposecomputes the length and locations of all the maximal sequences of consecutive true occurrences in values * Inputs : values is a non-null array of booleans with length at least 1 * * Outputs : outputs an integer array with one or more elements first element is the length of a maximal sequence oif consecutive trues in the input array values - the next elements are the indexes of the starting points* (in the input array values) of each of the maximal sequences of consecutive trues (in increasing order) if there are no true values in the input array, output [01 x/ Do not change the method signature (use the provided skeleton java file). Changing the method modifiers, return type, name or input arguments will result in zero correctness marks for this problem. Mark breakdown: 2 for style, 8 for correctness Put your Problem3.java file in your assignmenti.zip file Examples: Using the same examples as Problem 2, the output would be (in the same order) [2,1],[0][1,0,2] and [3,1,5] . Other examples include boolean [] test.case [true, false, true, false, true Problem3.longestStreak test case) test-case = new boolean []{false, false, false, true, true); Problem3.longestStreak test case) returns returns [2,3]Explanation / Answer
Problem3.java
import java.util.Scanner;
import java.util.*;
public class Problem3 {
public static List<Integer> longestStreak(Boolean [] values)
{
List<Integer> out = new ArrayList<Integer>();
int maxx = 0;
for(int i = 0; i< values.length;i++)
{
int tmp = 0;
for(int j = i; j< values.length;j++)
{
if(values[j] == true)
{
tmp = tmp + 1;
}
else
{
break;
}
}
maxx = Math.max(maxx,tmp);
}
if(maxx == 0)
{
out.add(0);
}
else
{
out.add(maxx);
for(int i = 0; i< values.length;i++)
{
int tmp = 0;
for(int j = i; j< values.length;j++)
{
if(values[j] == true)
{
tmp = tmp + 1;
}
else
{
break;
}
}
if(tmp == maxx)
{
out.add(i);
}
}
}
return out;
}
public static void main(String[] args)
{
Boolean[] values1 = {true,false,true,false,true};
Boolean[] values2 = {false,false,false,true,true};
System.out.println(longestStreak(values1));
System.out.println(longestStreak(values2));
}
}
Sample Output:
[1, 0, 2, 4]
[2, 3]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.