AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-array: Print the length of largest consecutive ascending sequence of integers in array

please solution in simple logic.

corejava x 353
array x 12
Posted On : 2015-06-25 22:57:26.0
profile Mantu Kumar - anyforum.in Mantu Kumar
8200
up-rate
3
down-rate

Answers


You shouldn´t have to use a temporary list for this. Just loop through the array or list with a counter and increment for each element that is greater than or equal to the previous. Use another variable to store the maximum:

LargestSeqArray.java:
------------------------------------------------------------
class LargestSeqArray{
public static void main(String s1[]){
int[] a = { 12, 121, 13, 14, 10, 11, 14, 16, 20, 22, 32, 34, 21 };

int count = 1, max = 1;

for (int i = 1; i < a.length; i++) {
if (a[i] >= a[i - 1]) {
count++;
} else {
if (count > max) {
max = count;
}
count = 1;
}
}

System.out.println(max);
}
}


Here, count is the number of contiguous and sorted elements. We increment it while we´re on a continuous/increasing streak. Once this streak breaks (else clause), we check if count is greater than max, our variable for storing the maximum count: if it is, we set max to count. We then set count back to 1, since in the else clause we know our streak has ended and we´ll need to start over.

Posted On : 2015-06-27 13:11:31
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188249150
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in