Question

In: Computer Science

Given an array of positive numbers and a positive number S, find the length of the...

Given an array of positive numbers and a positive number S, find the length of the smallest contiguous subarray whose sum is greater than or equal to S. Return 0, if no such subarray exists.

Example 1

Input: [2, 1, 5, 2, 3, 2], S=7 Output: 2 Explanation: The smallest subarray with a sum great than or equal to '7' is [5, 2].

Example 2

Input: [2, 1, 5, 2, 8], S=7 Output: 1 Explanation: The smallest subarray with a sum greater than or equal to '7' is [8].

Example 3

Input: [3, 4, 1, 1, 6], S=8 Output: 3 Explanation: Smallest subarrays with a sum greater than or equal to '8' are [3, 4, 1] or [1, 1, 6].

CODE::

class Main {
public static int findSmallestSubarray(int[] arr, int S){
// write your code here
}
public static void main(String[] args) {
// write your code here
}
}

Solutions

Expert Solution


class Main
{
public static int findSmallestSubarray(int[] arr,int S){
int s=0;
int len=arr.length+1;
for(int i=0;i<arr.length;i++){
s=arr[i];//checking each element if it is greater or equal to S return 1
if(s>=S)
return 1;
for(int j=i+1;j<arr.length;j++){
s+=arr[j];//add each element to s and check the condition
  
if(s>=S && (j-i+1)<len){
len=j-i+1;//if sum is greater or equal to S, update len
}
}
}
if(len>arr.length)
return 0;
return len;
}
   public static void main(String[] args) {
       int arr[]={2,1,5,2,3,2};
       System.out.println(findSmallestSubarray(arr,7));//create arrays and call functions
       int arr2[]={2,1,5,2,8};
       System.out.println(findSmallestSubarray(arr2,8));
       int arr3[]={3,4,1,1,6};
       System.out.println(findSmallestSubarray(arr3,8));
   }
}

Screenshots:

The output is attached below for reference.

Please follow it.


Related Solutions

Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
a)Find two positive numbers such that the sum of the first number and twice the second...
a)Find two positive numbers such that the sum of the first number and twice the second number is 108 and the product is a maximum b)figure out the dimentions of a rectangular solid that has a square base of maximum volume if its surface area is 216 square inches
Project: Given a string s and an integer array indices of the same length. The string...
Project: Given a string s and an integer array indices of the same length. The string s will be shuffled such that the character at the i th position moves to indices[i] in the shuffled string. Return the shuffled string. Example: Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output: "leetcode" Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. You need to do: Create a class called ShuffledStringApp. Keeping class StringOperation and IO in util package. In this project, you will...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
. As input you are given two arrays: an array of numbers ? and an array...
. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?]...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
Implement if else loop to find out if the given number is positive or negative or...
Implement if else loop to find out if the given number is positive or negative or zero. You would be hardcoding the number in the below example I used 8. But you can use a different number.
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
Input 100 numbers and find and output how many numbers are positive and negative, find and...
Input 100 numbers and find and output how many numbers are positive and negative, find and output average of these numbers Write using vb.net
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT