Subsequence closest to 0

Source: Internet
Author: User

Given the integers m, n and array X [N] to find an I, make X [I] + X [I + 1] + X [I + 2] + X [I + 3] + X [I + 4]… X [I + M] is closest to zero.

(0 <= I <n-m)

I. Brute Force Solution

Traverse each I value, calculate the sum of sub-sequences, and then find

Int find (int A [], int N, int m) // search for m + 1 numbers to minimize their sum {int I = 0; int thissum = 0; int J = 0; int ans = int_max; for (I = 0; I <n-m; I ++) // time complexity is O (M * n) {thissum = 0; For (j = I; j <= I + m; j ++) thissum + = A [J]; If (ABS (INT) (thissum) <ABS (INT) (ANS) ans = ABS (INT) thissum);} return ans ;}

Obviously, the time complexity is O (M * n), and the space complexity is O (1)

Ii. Dynamic Planning:

Create an array DP []. DP [I] Stores X [I] + X [I + 1] +... For the value of X [I + M], the recursive relationship is:

DP [I + 1] = DP [I]-A [I] + A [I + 1 + M], traverse the DP [] array, and find the nearest zero.

int find2(int a[],int n,int m){int *dp=(int *)malloc(n*sizeof(int));int i=0;int tmp=0;for(i=0;i<=m;i++){tmp+=a[i];}dp[0]=tmp;for(i=1;i<n-m;i++){dp[i]=dp[i-1]+a[i+m]-a[i-1];}int ans=INT_MAX;for(i=0;i<n;i++){if(abs((int)dp[i])<ans)ans=abs((int)dp[i]);}return ans;}
The time complexity is O (n), and the space complexity is O (n)

You can also make some improvements to the above Code, the above DP [] array, just use a variable.

int find3(int a[],int n,int m)   {int tmp=0;int ans=INT_MAX;int i=0;for(i=0;i<=m;i++)tmp+=a[i];if(abs((int)tmp)<ans)ans=abs((int)tmp);for(i=1;i<n-m;i++){tmp=tmp-a[i-1]+a[i+m];if(abs((int)tmp)<ans)ans=abs((int)tmp);}return ans;}

The time complexity is O (n), and the space complexity is O (1)

Complete code:

# Include <iostream> # include <cstdlib> # include <cmath> # include <ctype. h> using namespace STD; int find (int A [], int N, int m) // find m + 1 numbers to minimize their sum {int I = 0; int thissum = 0; Int J = 0; int ans = int_max; for (I = 0; I <n-m; I ++) // The time complexity is O (M * n) {thissum = 0; For (j = I; j <= I + m; j ++) thissum + = A [J]; If (ABS (INT) (thissum) <ABS (INT) (ANS) ans = ABS (INT) thissum);} return ans;} int find2 (int A [], int N, int m) {int * dp = (int *) malloc (N * sizeof (INT); int I = 0; int TMP = 0; for (I = 0; I <= m; I ++) {TMP + = A [I];} DP [0] = TMP; for (I = 1; I <n-m; I ++) {DP [I] = DP [I-1] + A [I + M]-A [I-1];} int ans = int_max; for (I = 0; I <N; I ++) {If (ABS (INT) DP [I]) <ans) ans = ABS (INT) DP [I]);} return ans ;} int find3 (int A [], int N, int m) // {int TMP = 0; int ans = int_max; int I = 0; for (I = 0; I <= m; I ++) TMP + = A [I]; If (ABS (INT) TMP) <ans) ans = ABS (INT) TMP ); for (I = 1; I <n-m; I ++) {TMP = TMP-A [I-1] + A [I + M]; If (ABS (INT) TMP) <ans) ans = ABS (INT) TMP);} return ans;} int main () {int A [10] = {1,-2, 3,-4, -5,-6, 7, 0}; cout <find (A,) <Endl; cout <find2 (A,) <Endl; cout <find3 (A, 10, 3) <Endl; System ("pause"); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.