Question: C++ 1 short program. 1. write a function that gets an array of integers (A) and an integer (k) as its input. 2. write a
C++
1 short program.
1. write a function that gets an array of integers (A) and an integer (k) as its input.
2. write a program to find a contiguous subarray of size k in this array that sums up to zero.
3. Example: If A = [1, 2, 3, 4, 5, 6] and k = 4, this subarray is [2, 3, 4, 5].
4. time complexity of your program at its worst-case should be O(N), where N is the size of array.
what i have so far:
#include
int findWindow(int a[], int N, int k);
int main() {
int a[6] = { 1, 2, -3, -4, 5, 6 }; int k = 4; int N = 6; int sum;
sum = findWindow(a, N, k); cout << sum << endl;
system("Pause"); return 0; }
int findWindow(int a[], int N, int k) {
int sum = 0;
for (int i = 0; i < k; i++) { sum = sum + a[i]; }
for (int i = 1; i < N - k; i++) { sum = sum - a[i - 1]; sum = sum + a[i + k]; }
if (sum == 0) return 0;
// for loop from 0 to k-1 // do summation
// for loop from 0 to N-1 // remove old element and add new one
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
