Cheergo
BAN USER
Comments (3)
Reputation -5
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
Just few lines of code.
17 bool hasSequence(vector<int> &A)
18 {
19 int minVal, maxVal;
20 minVal = *std::min_element(A.begin(), A.end());
21 maxVal = *std::max_element(A.begin(), A.end());
22
23 if ( maxVal - minVal >= A.size() ) return false;
24
25 for ( size_t i = 0; i < A.size(); i++ )
26 {
27 if ( A[i] == i + minVal ) continue;
28 if ( A[A[i] - minVal] == A[i] ) A[i] = INT_MIN;
29 else swap(A[i], A[A[i] - minVal]);
30 //display(A);
31 }
32
33 return true;
34 }
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Using DP may solve this problem.
- Cheergo September 14, 2012