-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSequence.java
More file actions
67 lines (67 loc) · 2.09 KB
/
LongestSequence.java
File metadata and controls
67 lines (67 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Given a series of numbers , find the longest sequence in the series. A sequence could be one of
the following:
An Ascending Sequence
Example:
Input: 836926
Output: 369
An Descending Sequence
Example:
Input: 2995316
Output: 9531
An Equal Sequence
Example:
Input: 255566
Output: 555
(Easy) -> Write a program that reads a series of numbers from the input and finds the longest asce
nding, descending or equal sequence in the series.
(medium) -> Write a program to find all of the sequences above.
*/
import java.util.*;
public class LongestSequence{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String temp = "";
String maxd = "0";
String maxa = "0";
String maxe = "0";
for (int i=0;i<str.length()-1 ;i++ ) {
temp = Character.toString(str.charAt(i));
int k=i;
if (Character.getNumericValue(str.charAt(k))>Character.getNumericValue(str.charAt(k+1))) {
while(((k+1)<str.length()-1)&&(str.charAt(k)>str.charAt(k+1))){
temp+=Character.toString(str.charAt(k+1));
k++;
}
}
if (Integer.parseInt(temp)>Integer.parseInt(maxd)) {
maxd=temp;
}
temp = Character.toString(str.charAt(i));
k=i;
if (Character.getNumericValue(str.charAt(k))<Character.getNumericValue(str.charAt(k+1))) {
while(((k+1)<str.length()-1)&&(str.charAt(k)<str.charAt(k+1))){
temp+=Character.toString(str.charAt(k+1));
k++;
}
}
if (Integer.parseInt(temp)>Integer.parseInt(maxa)) {
maxa=temp;
}
temp = Character.toString(str.charAt(i));
k=i;
if (Character.getNumericValue(str.charAt(k))==Character.getNumericValue(str.charAt(k+1))) {
while(((k+1)<str.length()-1)&&(str.charAt(k)==str.charAt(k+1))){
temp+=Character.toString(str.charAt(k+1));
k++;
}
}
if (Integer.parseInt(temp)>Integer.parseInt(maxe)) {
maxe=temp;
}
}
System.out.println("The longest Ascending Sequence is "+maxa);
System.out.println("The longest Descending Sequence is "+maxd);
System.out.println("The longest Equal Sequence is "+maxe);
}
}