Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n;
- cin >> n;
- int arr[n];
- for(int i=0;i<n;i++){
- cin >> arr[i];
- }
- int i=0;
- while(i<n){
- // element is already processed nothing to do
- if(arr[i]<=0){
- i++;
- continue;
- }
- int e=arr[i]-1;
- if(arr[e] > 0){
- arr[i] = arr[e];
- arr[e] = -1;
- }
- else{
- arr[e]--;
- // initialise it bcoz the element i+1 is not seen so far
- arr[i] = 0;
- i++;
- }
- }
- for(int i=0;i<n;i++){
- arr[i] = abs(arr[i]);
- cout << arr[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement