Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std; //who cares right
- //"Find the length of the longest non-decreasing sequence"
- int main()
- {
- int ArraySize;
- cout << "Size of the array: " << endl;
- cin >> ArraySize;
- if (ArraySize <= 0 || ArraySize >= 10000)
- {
- cout << "Invalid array size, can't continue." << endl;
- return 1;
- }
- int* MyValues = new int[ArraySize];
- for (int i=0; i<ArraySize; i++)
- MyValues[i] = rand() % 100;
- int MaxLength = 0;
- int Len = 0;
- for (int i=1; i<ArraySize; i++)
- {
- if (MyValues[i] >= MyValues[i-1])
- {
- // The sequence is still increasing
- Len++;
- }
- else
- {
- // The sequence stops
- if (Len > MaxLength) // Check for MaxLength
- MaxLength = Len;
- Len = 0;
- }
- }
- cout << "The length of the longest non-decreasing sequence should be " << MaxLength << endl;
- delete [] MyValues;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement