Question: Please fill in the missing part of the C code below. The time complexity should be O(n) . thanks Given an array A of positive

Please fill in the missing part of the C code below. The time complexity should be O(n). thanks Given an array A of positive integers and an integer number K, please find the shortest subarray whose sum of each elements is not smaller than K, and return its length. You need to design an efficient algorithm with a O(n) time complexity. Otherwise, you may exceed the time limit. 
#include  #include  #define MAX_LEN 1024 * 1024 #define MAX_QUERIES 10240 int shortest_subarray(int a[], int n, int k) {  // WRITE YOUR CODE HERE } // DO NOT MODIFY THE CODE BELOW int main() { int n, i; int Q; int result; int *a = (int *)malloc(MAX_LEN * sizeof(int)); int Ks[MAX_QUERIES]; scanf("%d", &n); scanf("%d", &Q); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < Q; i++) scanf("%d", &Ks[i]); for (i = 0; i < Q; i++) { result = shortest_subarray(a, n, Ks[i]); printf("%d ", result); } free(a); return 0; } -----------------------------------------End of Code----------------------------------------- Input: First line consists of two integers N and Q; Second line is an array A of N positive integers; Then Q lines follows: each of them is a target value K; 0 <= N <= 10^6, 0 <= Q <= 10^5 Output: Q lines: the i-th line is the length of the shortest subarray for the i-th K If there exist no subarray with sum at least K, return 0. Sample Input 1: 6 1 1 2 3 4 1 2 6 Sample Output 1: 2 Sample Input 2: 4 1 1 1 1 1 5 Sample Output 2: 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!