Join Regular Classroom : Visit ClassroomTech

Coding Questions | Building Problem | Codewindow.in

There are n consecutive buildings and their heights are represented in arrays. You need to find the sum of maximum height for any buildings that are in increasing order. Given an array of positive integers of length N, write a program to find the maximum sum of strictly increasing subarrays.

Example:
Input:
7
1 2 3 2 5 1 7

Output:
8

Explanation:
Some strictly increasing subarrays are
{1,2,3} sum=6.
{2,5} sum=7
{1,7} sum=8
Maximum Sum=8

Solution:

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int arr[] = new int[n];
		String str[] = br.readLine().split(" ");
		for(int i=0; i<n; i++)
		    arr[i] = Integer.parseInt(str[i]);
		int max = 0, total = 0;
		for(int i=0; i<n; i++) {
		    total += arr[i];
		    if(i+1 < n && arr[i] > arr[i+1]) {
		        if (total > max) {
		            max = total;
		            total = 0;
		        }
		    }
		}
		if (total > max) {
		    max = total;
		    total = 0;
		}
		System.out.println(max);
	}
}
You Missed
Also Checkout