Contiguous Subarrays

	public static int[] countSubarrays(int[] arr) {
		int[] output = new int[arr.length];
		for (int i = 0; i < arr.length; i++) {
			int j = i;
			int less = 0;
			while(j>=0 && arr[j]<=arr[i]) {
				less++;
				j--;
			}
			output[i] = less;
			
			j=i;
			int greater = 0;
			while(j < arr.length && arr[j] <= arr[i]) {
				greater++;
				j++;
			}
			output[i]+=greater-1;//one from duplication
		}
		return output;
	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s