Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions