Weak Learner
BAN USER#include <iostream>
#include <iostream>
using namespace std;
int arr[] = {1,2,-1,1,3,-2,2};
void reverse(int i, int j){
while(i<j){
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
i++; j--;
}
}
void rotatelist(int i, int j, int k){
reverse(i,j-1);
reverse(j, k);
reverse(i,k);
}
int transform(int N){
/****
arr 1 2 -1 1 3 -2 2
index 0 1 2 3 4 5 6
k = 5
j = 5
i = 3
such that j<= x <=k , arr[x] is negative number
such that i<= x <j , arr[x] is positive number
***/
int k = N-1, j, i;
while(k>=0 && arr[k]>=0)
k--;
j = k;
do {
while(j>=0 && arr[j]<0)
j--;
j++;
i = j-1;
while(i>=0 && arr[i]>=0) i--;
i++;
rotatelist(i, j, k);
k = i + (k-j);
j = i-1;
}while(j>0);
}
int main(){
int N = sizeof(arr) / sizeof(arr[0]);
for(int i=0;i<N;i++)
cout << arr[i] << ' ' ;
cout << endl;
transform(N);
for(int i=0;i<N;i++)
cout << arr[i] << ' ' ;
cout << endl;
}
Reprudystake, Game Programmer at BrowserStack
I am Rudy , a physical therapist with extensive knowledge of sports injuries and therapy . Strong background in athletic coaching and ...
Repjeansboylan698, Associate at 247quickbookshelp
JeanBoylan, and I am a Financial examiner and I love my work. Apart from this, Nowadays I am doing new ...
It can be done in O(N logN) using two binary indexed tree or segment tree.
- Weak Learner November 18, 2013