Requirements: The program must be able to handle 1000 elements; Each element is of type int32; Enter an array of shapes with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).
#include <iostream>
using namespace Std;
#define N 1000
void Main () {
int b[n+1];
int max[n], max1,n;
int j,k,a,i,g;
max[2000] = 0;max1 = 0;
cout<< "randomly generated 1000 numbers:" <<endl;
for (j = 0;j<n;j++) {
A = rand ()%2;
if (a = = 0) B[j] = 0-rand ()%1000;
if (a = = 1) b[j] = rand ()%1000;
cout<<b[j]<< "";
}
for (j = 0;j < n;j++) {
A = 0;
for (k = j;k < n;k++) {
a+= B[k];
if (A>max[j]) {
MAX[J] = A;
n = k;
}
}
if (MAX[J]>MAX1) {
Max1 = Max[j];
i = j;
}
}
cout<<endl;
cout<< "Continuous array:" <<endl;
for (g = I;g < n+1;g++)
cout<<b[g]<< "";
cout<<endl;
cout<< "Continuous array of the largest and as" <<max1<<endl;
}
Improved can never change O (N2) to 0 (n) but I'm going to make improvements in the future.
Title: Returns the and of the largest sub-array in an integer array.