Banana-Leopard/TASTY huggingface dataset:
// C++ Program to search an element
// in a sorted and pivoted array
 
#include <bits/stdc++.h>
using namespace std;
 
// Standard Binary Search function
int binarySearch(int arr[], int low, int high, int key)
{
    if (high < low)
        return -1;
 
    int mid = (low + high) / 2;
    if (key == arr[mid])
        return mid;
 
    if (key > arr[mid])
        return binarySearch(arr, (mid + 1), high, key);
 
    return binarySearch(arr, low, (mid - 1), key);
}
 
// Function to get pivot. For array 3, 4, 5, 6, 1, 2
// it returns 3 (index of 6)
int findPivot(int arr[], int low, int high)
{
    // Base cases
    if (high < low)
        return -1;
    if (high == low)
        return low;
 
    // low + (high - low)/2;
    int mid = (low + high) / 2;
    if (mid < high && arr[mid] > arr[mid + 1])
        return mid;
 
    if (mid > low && arr[mid] < arr[mid - 1])
        return (mid - 1);
 
    if (arr[low] >= arr[mid])
        return findPivot(arr, low, mid - 1);
 
    return findPivot(arr, mid + 1, high);
}
 
// Searches an element key in a pivoted
// sorted array arr[] of size n
int pivotedBinarySearch(int arr[], int n, int key)
{
    int pivot = findPivot(arr, 0, n - 1);
 
    // If we didn't find a pivot,
    // then array is not rotated at all
    if (pivot == -1)
        return binarySearch(arr, 0, n - 1, key);
 
    // If we found a pivot, then first compare with pivot
    // and then search in two subarrays around pivot
    if (arr[pivot] == key)
        return pivot;
 
    if (arr[0] <= key)
        return binarySearch(arr, 0, pivot - 1, key);
 
    return binarySearch(arr, pivot + 1, n - 1, key);
}
 
// Driver program to check above functions
int main()
{
    // Let us search 3 in below array
    int arr1[] = { 5, 6, 7, 8, 9, 10, 1, 2, 3 };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int key = 3;
 
    // Function calling
    cout << "Index of the element is : "
         << pivotedBinarySearch(arr1, n, key);
 
    return 0;
}

Time complexity: logn
Space complexity: constant
// Search an element in sorted and rotated
// array using single pass of Binary Search
#include <bits/stdc++.h>
using namespace std;
 
// Returns index of key in arr[l..h] if
// key is present, otherwise returns -1
int search(int arr[], int l, int h, int key)
{
    if (l > h)
        return -1;
 
    int mid = (l + h) / 2;
    if (arr[mid] == key)
        return mid;
 
    /* If arr[l...mid] is sorted */
    if (arr[l] <= arr[mid]) {
        /* As this subarray is sorted, we can quickly
        check if key lies in half or other half */
        if (key >= arr[l] && key <= arr[mid])
            return search(arr, l, mid - 1, key);
        /*If key not lies in first half subarray,
           Divide other half  into two subarrays,
           such that we can quickly check if key lies
           in other half */
        return search(arr, mid + 1, h, key);
    }
 
    /* If arr[l..mid] first subarray is not sorted, then
    arr[mid... h] must be sorted subarray */
    if (key >= arr[mid] && key <= arr[h])
        return search(arr, mid + 1, h, key);
 
    return search(arr, l, mid - 1, key);
}
 
// Driver program
int main()
{
    int arr[] = { 4, 5, 6, 7, 8, 9, 1, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 3;
    int i = search(arr, 0, n - 1, key);
 
    if (i != -1)
        cout << "Index: " << i << endl;
    else
        cout << "Key not found";
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: logn
Space complexity: constant
// C++ code to implement the approach
  
#include <bits/stdc++.h>
using namespace std;
  
// This function returns true if arr[0..n-1]
// has a pair with sum equals to x.
bool pairInSortedRotated(int arr[], int n, int x)
{
    // Find the pivot element
    int i;
    for (i = 0; i < n - 1; i++)
        if (arr[i] > arr[i + 1])
            break;
  
    // l is now index of smallest element
    int l = (i + 1) % n;
  
    // r is now index of largest element
    int r = i;
  
    // Keep moving either l or r till they meet
    while (l != r) {
  
        // If we find a pair with sum x,
        // we return true
        if (arr[l] + arr[r] == x)
            return true;
  
        // If current pair sum is less,
        // move to the higher sum
        if (arr[l] + arr[r] < x)
            l = (l + 1) % n;
  
        // Move to the lower sum side
        else
            r = (n + r - 1) % n;
    }
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 11, 15, 6, 8, 9, 10 };
    int X = 16;
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    if (pairInSortedRotated(arr, N, X))
        cout << "true";
    else
        cout << "false";
  
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find max value of i*arr[i]
#include <iostream>
using namespace std;
 
// Returns max possible value of i*arr[i]
int maxSum(int arr[], int n)
{
    // Find array sum and i*arr[i] with no rotation
    int arrSum = 0; // Stores sum of arr[i]
    int currVal = 0; // Stores sum of i*arr[i]
    for (int i = 0; i < n; i++) {
        arrSum = arrSum + arr[i];
        currVal = currVal + (i * arr[i]);
    }
 
    // Initialize result as 0 rotation sum
    int maxVal = currVal;
 
    // Try all rotations one by one and find
    // the maximum rotation sum.
    for (int j = 1; j < n; j++) {
        currVal = currVal + arrSum - n * arr[n - j];
        if (currVal > maxVal)
            maxVal = currVal;
    }
 
    // Return result
    return maxVal;
}
 
// Driver program
int main(void)
{
    int arr[] = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "\nMax sum is " << maxSum(arr, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// A Naive C++ program to find maximum sum rotation
#include<bits/stdc++.h>
 
using namespace std;
 
// Returns maximum value of i*arr[i]
int maxSum(int arr[], int n)
{
   // Initialize result
   int res = INT_MIN;
 
   // Consider rotation beginning with i
   // for all possible values of i.
   for (int i=0; i<n; i++)
   {
 
     // Initialize sum of current rotation
     int curr_sum = 0;
 
     // Compute sum of all values. We don't
     // actually rotate the array, instead of that we compute the
     // sum by finding indexes when arr[i] is
     // first element
     for (int j=0; j<n; j++)
     {
         int index = (i+j)%n;
         curr_sum += j*arr[index];
     }
 
     // Update result if required
     res = max(res, curr_sum);
   }
 
   return res;
}
 
// Driver code
int main()
{
    int arr[] = {8, 3, 1, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << maxSum(arr, n) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// An efficient C++ program to compute
// maximum sum of i*arr[i]
#include<bits/stdc++.h>
 
using namespace std;
 
int maxSum(int arr[], int n)
{
    // Compute sum of all array elements
    int cum_sum = 0;
    for (int i=0; i<n; i++)
        cum_sum += arr[i];
 
    // Compute sum of i*arr[i] for initial
    // configuration.
    int curr_val = 0;
    for (int i=0; i<n; i++)
        curr_val += i*arr[i];
 
    // Initialize result
    int res = curr_val;
 
    // Compute values for other iterations
    for (int i=1; i<n; i++)
    {
        // Compute next value using previous
        // value in O(1) time
        int next_val = curr_val - (cum_sum - arr[i-1])
                       + arr[i-1] * (n-1);
 
        // Update current value
        curr_val = next_val;
 
        // Update result if required
        res = max(res, next_val);
    }
 
    return res;
}
 
// Driver code
int main()
{
    int arr[] = {8, 3, 1, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << maxSum(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find maximum sum of all
// rotation of i*arr[i] using pivot.
#include <iostream>
using namespace std;
 
// fun declaration
int maxSum(int arr[], int n);
int findPivot(int arr[], int n);
 
// function definition
int maxSum(int arr[], int n)
{
    int sum = 0;
    int i;
    int pivot = findPivot(arr, n);
 
    // difference in pivot and index of
    // last element of array
    int diff = n - 1 - pivot;
    for(i = 0; i < n; i++)
    {
        sum = sum + ((i + diff) % n) * arr[i];
    }
    return sum;
}
 
// function to find pivot
int findPivot(int arr[], int n)
{
    int i;
    for(i = 0; i < n; i++)
    {
        if(arr[i] > arr[(i + 1) % n])
            return i;
    }
}
 
// Driver code
int main(void)
{
     
    // rotated input array
    int arr[] = {8, 13, 1, 2};
    int n = sizeof(arr) / sizeof(int);
    int max = maxSum(arr, n);
    cout << max;
    return 0;
}
 
// This code is contributed by Shubhamsingh10

Time complexity: linear
Space complexity: constant
// C++ program for the above approach
#include <iostream>
using namespace std;
int* rotateArray(int A[], int start, int end)
{
    while (start < end) {
        int temp = A[start];
        A[start] = A[end];
        A[end] = temp;
        start++;
        end--;
    }
    return A;
}
void leftRotate(int A[], int a, int k)
{
    // if the value of k ever exceeds the length of the
    // array
    int c = k % a;
 
    // initializing array D so that we always
    // have a clone of the original array to rotate
    int D[a];
    for (int i = 0; i < a; i++)
        D[i] = A[i];
 
    rotateArray(D, 0, c - 1);
    rotateArray(D, c, a - 1);
    rotateArray(D, 0, a - 1);
 
    // printing the rotated array
    for (int i = 0; i < a; i++)
        cout << D[i] << " ";
    cout << "\n";
}
int main()
{
    int A[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(A) / sizeof(A[0]);
 
    int k = 2;
    leftRotate(A, n, k);
 
    k = 3;
    leftRotate(A, n, k);
 
    k = 4;
    leftRotate(A, n, k);
    return 0;
}
// this code is contributed by aditya942003patil

Time complexity: linear
Space complexity: linear
// CPP implementation of left rotation of
// an array K number of times
#include <bits/stdc++.h>
using namespace std;
 
// Fills temp[] with two copies of arr[]
void preprocess(int arr[], int n, int temp[])
{
    // Store arr[] elements at i and i + n
    for (int i = 0; i < n; i++)
        temp[i] = temp[i + n] = arr[i];
}
 
// Function to left rotate an array k times
void leftRotate(int arr[], int n, int k, int temp[])
{
    // Starting position of array after k
    // rotations in temp[] will be k % n
    int start = k % n;
 
    // Print array after k rotations
    for (int i = start; i < start + n; i++)
        cout << temp[i] << " ";
 
    cout << endl;
}
 
// Driver program
int main()
{
    int arr[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int temp[2 * n];
    preprocess(arr, n, temp);
 
    int k = 2;
    leftRotate(arr, n, k, temp);
 
    k = 3;
    leftRotate(arr, n, k, temp);
 
    k = 4;
    leftRotate(arr, n, k, temp);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP implementation of left rotation of
// an array K number of times
#include <bits/stdc++.h>
using namespace std;
 
// Function to left rotate an array k times
void leftRotate(int arr[], int n, int k)
{
    // Print array after k rotations
    for (int i = k; i < k + n; i++)
        cout << arr[i % n] << " ";
}
 
// Driver program
int main()
{
    int arr[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int k = 2;
    leftRotate(arr, n, k);
    cout << endl;
 
    k = 3;
    leftRotate(arr, n, k);
    cout << endl;
 
    k = 4;
    leftRotate(arr, n, k);
    cout << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ code  to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum value
int findMin(int arr[], int n)
{
    int min_ele = arr[0];
 
    // Traversing over array to
    // find minimum element
    for (int i = 0; i < n; i++) {
        if (arr[i] < min_ele) {
            min_ele = arr[i];
        }
    }
 
    return min_ele;
}
 
// Driver code
int main()
{
    int arr[] = { 5, 6, 1, 2, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << findMin(arr, N) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find minimum
// element in a sorted and rotated array
 
#include <bits/stdc++.h>
using namespace std;
 
int findMin(int arr[], int low, int high)
{
    // This condition is needed to
    // handle the case when array is not
    // rotated at all
    if (high < low)
        return arr[0];
 
    // If there is only one element left
    if (high == low)
        return arr[low];
 
    // Find mid
    int mid = low + (high - low) / 2; /*(low + high)/2;*/
 
    // Check if element (mid+1) is minimum element. Consider
    // the cases like {3, 4, 5, 1, 2}
    if (mid < high && arr[mid + 1] < arr[mid])
        return arr[mid + 1];
 
    // Check if mid itself is minimum element
    if (mid > low && arr[mid] < arr[mid - 1])
        return arr[mid];
 
    // Decide whether we need to go to left half or right
    // half
    if (arr[high] > arr[mid])
        return findMin(arr, low, mid - 1);
    return findMin(arr, mid + 1, high);
}
 
// Driver program to test above functions
int main()
{
    int arr[] = { 5, 6, 1, 2, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    cout << "The minimum element is "
         << findMin(arr, 0, N - 1) << endl;
 
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: logn
Space complexity: constant
// C++ program for right rotation of
// an array (Reversal Algorithm)
#include <bits/stdc++.h>
 
/*Function to reverse arr[]
from index start to end*/
void reverseArray(int arr[], int start,
                            int end)
{
    while (start < end)
    {
        std::swap(arr[start], arr[end]);
        start++;
        end--;
    }
}
 
/* Function to right rotate arr[]
of size n by d */
void rightRotate(int arr[], int d, int n)
{
    // if in case d>n,this will give segmentation fault.
    d=d%n;
    reverseArray(arr, 0, n-1);
    reverseArray(arr, 0, d-1);
    reverseArray(arr, d, n-1);
}
 
/* function to print an array */
void printArray(int arr[], int size)
{
    for (int i = 0; i < size; i++)
        std::cout << arr[i] << " ";
}
 
// driver code
int main()
{
    int arr[] = {1, 2, 3, 4, 5,
                6, 7, 8, 9, 10};
     
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 3;
     
    rightRotate(arr, k, n);
    printArray(arr, n);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ Program to solve queries on Left and Right
// Circular shift on array
#include <bits/stdc++.h>
using namespace std;
 
// Function to solve query of type 1 x.
void querytype1(int* toRotate, int times, int n)
{
    // Decreasing the absolute rotation
    (*toRotate) = ((*toRotate) - times) % n;
}
 
// Function to solve query of type 2 y.
void querytype2(int* toRotate, int times, int n)
{
    // Increasing the absolute rotation.
    (*toRotate) = ((*toRotate) + times) % n;
}
 
// Function to solve queries of type 3 l r.
void querytype3(int toRotate, int l, int r, int preSum[],
                int n)
{
    // Finding absolute l and r.
    l = (l + toRotate + n) % n;
    r = (r + toRotate + n) % n;
 
    // if l is before r.
    if (l <= r)
        cout << (preSum[r + 1] - preSum[l]) << endl;
 
    // If r is before l.
    else
        cout << (preSum[n] + preSum[r + 1] - preSum[l])
             << endl;
}
 
// Wrapper Function solve all queries.
void wrapper(int a[], int n)
{
    int preSum[n + 1];
    preSum[0] = 0;
 
    // Finding Prefix sum
    for (int i = 1; i <= n; i++)
        preSum[i] = preSum[i - 1] + a[i - 1];
 
    int toRotate = 0;
 
    // Solving each query
    querytype1(&toRotate, 3, n);
    querytype3(toRotate, 0, 2, preSum, n);
    querytype2(&toRotate, 1, n);
    querytype3(toRotate, 1, 4, preSum, n);
}
 
// Driver Program
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    wrapper(arr, N);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation of left rotation of
// an array K number of times
#include <bits/stdc++.h>
using namespace std;
 
// Function to leftRotate array multiple times
void leftRotate(int arr[], int n, int k)
{
    /* To get the starting point of rotated array */
    int mod = k % n;
 
    // Prints the rotated array from start position
    for (int i = 0; i < n; i++)
        cout << (arr[(mod + i) % n]) << " ";
 
    cout << "\n";
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int k = 2;
   
      // Function Call
    leftRotate(arr, n, k);
 
    k = 3;
   
      // Function Call
    leftRotate(arr, n, k);
 
    k = 4;
   
      // Function Call
    leftRotate(arr, n, k);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
int findElement(vector<int> arr, int ranges[][2],
                int rotations, int index)
{
 
  // Track of the rotation number
  int n1 = 1;
 
  // Track of the row index for the ranges[][] array
  int i = 0;
 
  // Initialize the left side of the ranges[][] array
  int leftRange = 0;
 
  // Initialize the right side of the ranges[][] array
  int rightRange = 0;
 
  // Initialize the key variable
  int key = 0;
 
  while (n1 <= rotations) {
 
    leftRange = ranges[i][0];
    rightRange = ranges[i][1];
    key = arr[rightRange];
    for (int j = rightRange; j >= leftRange + 1; j--) {
      arr[j] = arr[j - 1];
    }
 
    arr[leftRange] = key;
 
    n1++;
    i++;
  }
 
  // Return the element after all the rotations
  return arr[index];
}
 
int main()
{
  vector<int> arr{ 1, 2, 3, 4, 5 };
 
  // No. of rotations
  int rotations = 2;
 
  // Ranges according to 0-based indexing
  int ranges[][2] = { { 0, 2 }, { 0, 3 } };
 
  int index = 1;
  cout << (findElement(arr, ranges, rotations, index));
}
 
// This code is contributed by garg28harsh.

Time complexity: quadratic
Space complexity: constant
// CPP code to rotate an array
// and answer the index query
#include <bits/stdc++.h>
using namespace std;
 
// Function to compute the element at
// given index
int findElement(int arr[], int ranges[][2], int rotations,
                int index)
{
    for (int i = rotations - 1; i >= 0; i--) {
 
        // Range[left...right]
        int left = ranges[i][0];
        int right = ranges[i][1];
 
        // Rotation will not have any effect
        if (left <= index && right >= index) {
            if (index == left)
                index = right;
            else
                index--;
        }
    }
 
    // Returning new element
    return arr[index];
}
 
// Driver
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
 
    // No. of rotations
    int rotations = 2;
 
    // Ranges according to 0-based indexing
    int ranges[rotations][2] = { { 0, 2 }, { 0, 3 } };
 
    int index = 1;
 
    cout << findElement(arr, ranges, rotations, index);
 
    return 0;
}

Time complexity: constant
Space complexity: constant
// CPP program to split array and move first
// part to end.
#include <iostream>
using namespace std;
 
void splitArr(int arr[], int n, int k)
{
    for (int i = 0; i < k; i++) {
 
        // Rotate array by 1.
        int x = arr[0];
        for (int j = 0; j < n - 1; ++j)
            arr[j] = arr[j + 1];
        arr[n - 1] = x;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 12, 10, 5, 6, 52, 36 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int position = 2;
 
    splitArr(arr, n, position);
 
    for (int i = 0; i < n; ++i)
        cout <<" "<< arr[i];
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

Time complexity: quadratic
Space complexity: constant
// CPP program to split array and move first
// part to end.
#include <bits/stdc++.h>
using namespace std;
 
// Function to split array and
// move first part to end
void splitArr(int arr[], int length, int rotation)
{
    int tmp[length * 2] = {0};
 
    for(int i = 0; i < length; i++)
    {
        tmp[i] = arr[i];
        tmp[i + length] = arr[i];
    }
 
    for(int i = rotation; i < rotation + length; i++)
    {
        arr[i - rotation] = tmp[i];
    }
}
 
// Driver code
int main()
{
    int arr[] = { 12, 10, 5, 6, 52, 36 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int position = 2;
 
    splitArr(arr, n, position);
 
    for (int i = 0; i < n; ++i)
        printf("%d ", arr[i]);
 
    return 0;
}
 
// This code is contributed by YashKhandelwal8

Time complexity: linear
Space complexity: linear
// C++ program for above approach
#include <iostream>
using namespace std;
 
// Function to transform the array
void fixArray(int ar[], int n)
{
    int i, j, temp;
 
    // Iterate over the array
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            // Check is any ar[j]
            // exists such that
            // ar[j] is equal to i
            if (ar[j] == i) {
                temp = ar[j];
                ar[j] = ar[i];
                ar[i] = temp;
                break;
            }
        }
    }
 
    // Iterate over array
    for (i = 0; i < n; i++)
    {
        // If not present
        if (ar[i] != i)
        {
            ar[i] = -1;
        }
    }
 
    // Print the output
    cout << "Array after Rearranging" << endl;
    for (i = 0; i < n; i++) {
        cout << ar[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int n, ar[] = { -1, -1, 6, 1, 9, 3, 2, -1, 4, -1 };
    n = sizeof(ar) / sizeof(ar[0]);
 
    // Function Call
    fixArray(ar, n);
}
 
// Code BY Tanmay Anand

Time complexity: quadratic
Space complexity: constant
// C++ program for rearrange an
// array such that arr[i] = i.
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to rearrange an array
// such that arr[i] = i.
void fixArray(int A[], int len)
{
    for (int i = 0; i < len; i++)
    {
        if (A[i] != -1 && A[i] != i)
        {
            int x = A[i];
 
            // check if desired place
            // is not vacate
            while (A[x] != -1 && A[x] != x)
            {
                // store the value from
                // desired place
                int y = A[x];
 
                // place the x to its correct
                // position
                A[x] = x;
 
                // now y will become x, now
                // search the place for x
                x = y;
            }
 
            // place the x to its correct
            // position
            A[x] = x;
 
            // check if while loop hasn't
            // set the correct value at A[i]
            if (A[i] != i)
            {
                // if not then put -1 at
                // the vacated place
                A[i] = -1;
            }
        }
    }
}
 
// Driver code
int main()
{
    int A[] = { -1, -1, 6, 1, 9,
               3, 2, -1, 4, -1 };
 
    int len = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    fixArray(A, len);
 
    // Print the output
    for (int i = 0; i < len; i++)
        cout << A[i] << " ";
}
 
// This code is contributed by Smitha Dinesh Semwal

Time complexity: linear
Space complexity: constant
#include <iostream>
#include <unordered_set>
 
using namespace std;
 
void fixArray(int arr[], int n)
{
  // a set
  unordered_set<int> s;
   
  // Enter each element which is not -1 in set
  for(int i=0; i<n; i++)
  {
    if(arr[i] != -1)
      s.insert(arr[i]);
  }
   
  // Navigate through array,
  // and put A[i] = i,
  // if i is present in set
  for(int i=0; i<n; i++)
  {
    // if i(index) is found in hmap
    if(s.find(i) != s.end())
    {
      arr[i] = i;
    }
    // if i not found
    else
    {
      arr[i] = -1;
    }
  }
   
}
 
// Driver Code
int main() {
   
  // Array initialization
  int arr[] {-1, -1, 6, 1, 9,
          3, 2, -1, 4,-1};
  int n = sizeof(arr) / sizeof(arr[0]);
   
  // Function Call
  fixArray(arr, n);
   
  // Print output
  for(int i=0; i<n; i++)
    cout << arr[i] << ' ';
   
  return 0;
}
 
// this code is contributed by dev chaudhary

Time complexity: linear
Space complexity: linear