/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static int shortestSatisfyingSubarrayFreq(int[] arr, int k){
int ans=0;
for(int start=0; start<arr.length; start++){
int sum=0;
for(int end=start; end<arr.length; end++){
sum +=arr[end];
if(sum==k){
if((end-start+1)==minLength){
ans++;
} else if ((end-start+1)<minLength) {
minLength = (end-start+1);
ans = 1;
}
}
}
}
return minLength
==Integer.
MAX_VALUE ? 0 : ans
; }
static int longestSatisfyingSubarrayFreq(int[] arr, int k){
int ans=0;
for(int start=0; start<arr.length; start++){
int sum=0;
for(int end=start; end<arr.length; end++){
sum +=arr[end];
if(sum==k){
if((end-start+1)==maxLength){
ans++;
} else if ((end-start+1)>maxLength) {
maxLength = (end-start+1);
ans = 1;
}
}
}
}
return maxLength
==Integer.
MIN_VALUE ? 0 : ans
; }
{
// Find count of shortest/largest subarrays with sum k in given array
Scanner sc
= new Scanner
(System.
in); int arrLength = sc.nextInt();
int[] arr = new int[arrLength];
for(int i=0; i<arrLength; i++){
arr[i] = sc.nextInt();
}
int k = sc.nextInt();
/* use example
10
0 5 -5 3 2 1 0 6 -1 5
5
// freq of shortest length subarray having sum = k is :2
*/
System.
out.
println("freq of shortest length subarray having sum = k is :"+ shortestSatisfyingSubarrayFreq(arr, k));
System.
out.
println("freq of longest length subarray having sum = k is :"+ longestSatisfyingSubarrayFreq(arr, k));
}
}