Table of Contents
Let’s look at various input and output scenarios for this situation - h2>
The method used in the following program is as follows
Example
Output
Home Backend Development C++ Rearrange an array so that 'arr' becomes 'i' if 'arr' is 'j' in C++

Rearrange an array so that 'arr' becomes 'i' if 'arr' is 'j' in C++

Sep 18, 2023 am 10:29 AM
array c rearrange become

Rearrange an array so that arr becomes i if arr is j in C++

We are given an array of positive integer type, assuming it is arr[], its size can be given arbitrarily, and the value of the elements in the array should be greater than 0 but less than the size of the array. The task is to rearrange An array, if arr[j] is "j", then arr[j] becomes "i" and the final result is printed.

Let’s look at various input and output scenarios for this situation - h2>

Input− int arr[] = {3, 4, 1, 2, 0}

Output− Array before sorting: 3 4 1 2 0 Rearrange the array so that arr[j] becomes i. If arr[i] is j, it is: 4 2 3 0 1

Explanation− We get an integer of size Array 6 and all elements in the array with values ​​less than 6. Now, we will rearrange the array, i.e. arr[1] is 4, arr[4] = 1; arr[2] is 1, arr[1] = 2; arr[3] is 2. arr[2] = 3; arr[4] is 0, arr[0]=4. Therefore, the final array is 4 2 3 0 1.

Input t− int arr[] = {2, 0, 1, 3}

Output− Array before arrangement: 2 0 1 3 Rearrange the array so that arr[j] becomes i, if arr[i] is j, then: 1 2 0 3

Explanation− We get an integer of size 6 array and the value of all elements in the array is less than 6. Now, we will rearrange the array, i.e. arr[0] is 2, arr[2] = 0; arr[1] is 0, arr[0] = 1; arr[2] is 1, arr[1] = 2; arr[3] is 3, arr[3] = 3. Therefore, the final array is 1 2 0 3.

The method used in the following program is as follows

  • Input an array of integer type elements and calculate the size of the array.

  • Print the array before arranging it and call the function Rearrangement(arr, size)

  • In the function Rearrangement(arr, size)

    • Create an array ptr[] of integer type values ​​with the same size as the array arr[].

    • Start looping FOR from i to 0 until i is less than size. Inside the loop, set ptr[arr[i]] to i.

    • Start looping FOR from i to 0 until i is less than size. Inside the loop, set arr[i] to ptr[i].

  • #Print the rearranged array.

Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(int arr[], int size){
   int ptr[size];
   for(int i = 0; i < size; i++){
      ptr[arr[i]] = i;
   }
   for(int i = 0; i < size; i++){
      arr[i] = ptr[i];
   }
}
int main(){
   //input an array
   int arr[] = {3, 4, 1, 2, 0};
   int size = sizeof(arr) / sizeof(arr[0]);
   //print the original Array
   cout<<"Array before Arrangement: ";
   for (int i = 0; i < size; i++){
      cout << arr[i] << " ";
   }
   //calling the function to rearrange the array
   Rearrangement(arr, size);
   //print the array after rearranging the values
   cout<<"\nRearrangement of an array such that &lsquo;arr[j]&rsquo; becomes &lsquo;i&rsquo; if &lsquo;arr[i]&rsquo; is &lsquo;j&rsquo; is: ";
   for(int i = 0; i < size; i++){
      cout<< arr[i] << " ";
   }
   return 0;
}
Copy after login

Output

If we run the above code it will generate the following output

Array before Arrangement: 3 4 1 2 0
Rearrangement of an array such that &lsquo;arr[j]&rsquo; becomes &lsquo;i&rsquo; if &lsquo;arr[i]&rsquo; is &lsquo;j&rsquo; is: 4 2 3 0 1
Copy after login

The above is the detailed content of Rearrange an array so that 'arr' becomes 'i' if 'arr' is 'j' in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

Exploring the complexity of PHP array deduplication algorithms Exploring the complexity of PHP array deduplication algorithms Apr 28, 2024 pm 05:54 PM

Complexity of PHP array deduplication algorithm: array_unique(): O(n) array_flip()+array_keys(): O(n) foreach loop: O(n^2)

See all articles