I need some help with the analysisi of the following code: import java.util.*; public class Main { static void findSequence(int [] a, int l){ int x=1; int BNumber=a[0]; int y=0; int index=0; for(int n=1;ny){ BNumber=a[n-1]; y=x; } x=1; }else { x++; } } if( x>y){ BNumber=a[l-1]; y=x; } index=y-1; System.out.println("The longest same number sequence starts at index "+index+" with "+y+" values of "+BNumber); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a series of numbers ending with 0: "); String s=sc.nextLine(); String[] splitArr = s.split(" "); int[] array = new int[splitArr.length]; for (int i = 0; i < splitArr.length; i++) { array[i] = Integer.parseInt(splitArr[i]); } findSequence(array,array.length); } }
I need some help with the analysisi of the following code:
import java.util.*;
public class Main {
static void findSequence(int [] a, int l){
int x=1;
int BNumber=a[0];
int y=0;
int index=0;
for(int n=1;n<l;n++){
if(a[n]!=a[n-1]){
if(x>y){
BNumber=a[n-1];
y=x;
}
x=1;
}else {
x++;
}
}
if( x>y){
BNumber=a[l-1];
y=x;
}
index=y-1;
System.out.println("The longest same number sequence starts at index "+index+" with "+y+" values of "+BNumber);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a series of numbers ending with 0: ");
String s=sc.nextLine();
String[] splitArr = s.split(" ");
int[] array = new int[splitArr.length];
for (int i = 0; i < splitArr.length; i++) {
array[i] = Integer.parseInt(splitArr[i]);
}
findSequence(array,array.length);
}
}
Step by step
Solved in 2 steps