Algorithm and Flowchart to check whether an array is mirror inverse or not

[1721 views]




Inverse of an Array:

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 index 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 }

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

Mirror Inverse Array:

If the inverse or mirror image of an array is equal to the original array, the array is known as mirror inverse.

Let us consider an example:
Input array: arr[] = { 3, 4, 2, 0, 1 }
Inverse array: inverse[] = { 3, 4, 2, 0, 1 }
As both the arrays are same, arr[] is a mirror inverse array.

Algorithm to check whether a given array is mirror inverse or not.:

Step 1: Start Step 2: Read the size of the array from the user, say ‘n’ Step 3: Declare an array of size n, say a[n] Step 4: Declare another array of size n to store the inverse, say inv[n] Step 5: Initialize loop variable, i = 0 Step 6: Repeat while i < n: 6.1: Read the array element at position ‘i’ 6.2: Increment i by 1 Step 7: Initialize loop variable, i = 0 Step 8: Repeat while i < n: 8.1: value = arr[i] 8.2: inv[value] = i 8.3: Increment i by 1 Step 9: Initialize flag = true Step 10: Initialize loop variable, i = 0 Step 11: Repeat While i < n: 11.1: If a[i] != inv[i], then: 11.1.a: flag = false 11.1.b: break 11.2: Increment i by 1 Step 12: If flag = true, then: 12.1: Display “Mirror Inverse” Step 13: Else: 13.1: Display “Not Mirror Inverse” Step 14: Stop

Explanation:

To check whether an array is mirror inverse, we need to find the inverse of the given array. We take 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 which 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 checked.

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 ith 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[ ].

Now, we need to compare the a[] and inv[] arrays. To do this, we start a loop with an initial value of 0 and compare each element of both the arrays one by one. If an element does not match, the flag becomes false and the loop terminates. We then check whether the flag is true. If yes, the array is mirror inverse, else, it is not a mirror inverse.

Flowchart to check whether an array is mirror inverse or not:

Algorithm and flowchart to check whether an array is mirror inverse or not
Remove WaterMark from Above Flowchart

                 



Want to Learn How to write own Algorithm and Flowcharts



Want to test your logical skills in Algorithms?




Comments










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags

    Algorithm to check whether an array is mirror inverse or not

    Flowchart to check whether an array is mirror inverse or not

    Pseudocode for identifying if array is mirror inverse