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.
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.
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT