zero_cool
BAN USERHere is working code with few minor changes...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int find_len( int sortedArray[], int len) {
int length = len;
//int length = sortedArray.length;
int length_longest_sequence = 0;
int temp_length_longest_sequence = 0;
int min = 0;
for(int idx = 1; idx < length; idx++) {
if(sortedArray[min] == sortedArray[idx]) {
temp_length_longest_sequence++;
continue;
}
if (sortedArray[min] +1 == sortedArray[idx]) {
temp_length_longest_sequence++;
} else if (sortedArray[min] + 1 < sortedArray[idx]) {
if(temp_length_longest_sequence > length_longest_sequence) {
length_longest_sequence = temp_length_longest_sequence;
}
min = idx;
temp_length_longest_sequence = 0;
}
}
if(temp_length_longest_sequence > length_longest_sequence) {
length_longest_sequence = temp_length_longest_sequence;
}
return length_longest_sequence +1;
}
void printArray(vector<int> v) {
vector<int>::iterator iter;
cout << "\n{ ";
for(iter=v.begin(); iter != v.end(); ++iter) {
cout << *iter << " " ;
}
cout << "}" << endl;
}
int main()
{
int len;
vector<int> array;
array = {2,3,4,5,5,3,3,7,1,2};
sort(array.begin(),array.end());
printArray(array);
len = find_len(&array[0],array.size());
cout << len << endl;
return 0;
}
char *p = "abc" <--- This is immutable. It's a constant. You cannot change it. If you try in a simple program you will get segfault; if you try to p[0] = 'k';
char p[3] = "abc" --> You declared an array and initialized it. You are allowed to changed array elements.
Following from A book on C:
You need to understand that in C array, pointers and Strings are closely related.
Array name can be used as a pointer. (It's a constant pointer you cannot initialized it)
p[i] and * (p + i) are equivalent
s[i] and *(s + i) are equivalent
++p is valid for increment
++s is wrong --> cannot change s
use size_t instead of int to get rid of compilation warning...
- zero_cool May 27, 2014also I found somewhere it's better to use ++i in for loop
for(size_t i=0; i < tuples.size(); ++i)