[2856 views]

An array is a data structure that contains elements of the same data type. These elements are stored in contiguous locations in the memory. We can access the elements of an array through the index of the element. For example if we want to access the fifth element of an array, we can write: arr[4]. Here the index the required variable in the array ‘arr’ is 4.

**
Note: The index of an array always starts with 0.
**

To find the inverse of an array, we swap the array elements with the corresponding indices. For example, if 4 is present at index 1 in the input array, in the inverse array, 1 will be present at index 4. The only constraint on the input values of the array is that the array elements should be in the range between 0 to n without repetition, where ‘n’ is the size of the array. This is the most important constraint while finding out the inverse because there should not be two different values for one index and also, we cannot leave one or more indexes null.

Let us consider an example for better understanding:Input array: arr[] = { 4, 2, 0, 1, 3}

Inverse array: inverse[] = { 2, 3, 1, 4, 0 }

We start the algorithm by taking the size of the array as input from the user and store it in a variable, say ‘n’. We declare two arrays of size ‘n’. One, to store the array given as input: arr[ ] and another to store the inverse of the arr[ ] array: inv[ ]. Then, we start a loop that will run ‘n’ times and take each element of the array as input one by one. Once this loop completes its execution, we have the array arr[ ] that needs to be inverted.

Now, we start another loop that will run ‘n’ number of times. The loop variable ‘i’ is initialized as 0 and is incremented after each iteration. We find out the value present at i^{th} index of the array and store it in the variable ‘value’ with the help of the statement: value = arr[i]. This value represents an index for the inverse array where ‘i’ needs to be stored as data. We store the value i at the position ‘value’ of array inverse (inv). This process is repeated for all the elements of the array. Once the loop completes its execution, we have the inverse of the array in inv[ ]. The inv array is displayed.

- Java IDE Online
- Python IDE Online
- JSON Formatter/Minifier Online
- Case Converter
- Reverse String
- HTML Encoder
- HTML Decoder
- URL Encoder
- URL Decoder
- Decimal To Binary
- Binary To Decimal
- Decimal To Octal
- Binary To Octal
- Decimal To Hexadecimal
- Hexadecimal To Decimal
- Hexadecimal To Binary
- Octal To Decimal
- Octal to Hexadecimal
- Octal to Binary
- Calculate String Length
- Remove Spaces
- Remove Line Breaks
- Remove Empty Lines
- Remove Duplicate Lines
- Word Counter
- Replace Space with hyphen
- Check Armstrong number
- Text to URL

- Algorithm and Flowchart to calculate the frequency of punctuation marks in a given string
- Algorithm to perform Binary to Decimal Conversion
- Rat in A Maze Problem Algorithm and Flowchart
- Algorithm and Flowchart to find the smallest element in an array
- Algorithm and Flowchart to Calculate Fibonacci series up to n