Hey I’ve got this cool idea for an app! It’s kind of simple but just hear me out. All the user has to do is enter a bunch of integers. Then the application prints the largest sum of a strictly ascending sequence of the array. A strictly ascending sequence is a sequence where the current number is always lesser than the next number.
For example, the user enters 2 4 5 1 7 3, the output should be 11 (2 + 4 + 5).
Then that’s it! I think this is going to be a hit! Don’t you think? Well I do. If you help me then we’re going to be rich!
Note: For this problem, a sequence must contain at least 2 numbers. If there is no sequence found, then the largest sum is 0.
Output:
Enter the size: 6
Enter element #1: 2
Enter element #2: 4
Enter element #3: 5
Enter element #4: 1
Enter element #5: 7
Enter element #6: 3
Largest sum = 11
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int sum=0;
System.out.println("Enter the size");
int n=input.nextInt();
for(int i=1;i<=n;i++){
System.out.print("Enter element #:"+i+" ");
int el=input.nextInt();
if(el>(el-1)){
sum=i+i-1;
}
else{
System.out.println("Largest sum is 0");
}
}
System.out.println("Largest sum is "+sum);
}
}
Comments
Leave a comment