# Replace the odd positioned elements with their cubes and even positioned elements with their squares

Given an array arr[] of n elements, the task is to replace all the odd positioned elements with their cubes and even positioned elements with their squares i.e. the resultant array must be {arr3, arr2, arr3, arr2, …}.

Examples:

Input: arr[]= {2, 3, 4, 5}
Output: 8 9 64 25
Updated array will be {23, 32, 43, 52} -> {8, 9, 64, 25}

Input: arr[] = {3, 4, 5, 2}
Output: 27 16 125 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: For any element of the array arr[i], it is odd positioned only if (i + 1) is odd as the indexing starts from 0. Now, traverse the array and replace all the odd positioned elements with their cubes and even positioned elements with their squares.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define ll long long int ` ` `  `// Utility function to print ` `// the contents of an array ` `void` `printArr(ll arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Function to update the array ` `void` `updateArr(ll arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// In case of even positioned element ` `        ``if` `((i + 1) % 2 == 0) ` `            ``arr[i] = (ll)``pow``(arr[i], 2); ` ` `  `        ``// Odd positioned element ` `        ``else` `            ``arr[i] = (ll)``pow``(arr[i], 3); ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``ll arr[] = { 2, 3, 4, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``updateArr(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.lang.Math; ` ` `  `class` `GFG ` `{ ` `     `  `// Utility function to print ` `// the contents of an array ` `static` `void` `printArr(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``System.out.print(arr[i] + ``" "``); ` `} ` ` `  `// Function to update the array ` `static` `void` `updateArr(``int` `arr[], ``int` `n) ` `{ ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// In case of even positioned element ` `        ``if` `((i + ``1``) % ``2` `== ``0``) ` `            ``arr[i] = (``int``)Math.pow(arr[i], ``2``); ` ` `  `        ``// Odd positioned element ` `        ``else` `            ``arr[i] = (``int``)Math.pow(arr[i], ``3``); ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``2``, ``3``, ``4``, ``5``, ``6` `}; ` `    ``int` `n = arr.length; ` ` `  `    ``updateArr(arr, n); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Code_Mech. `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Utility function to print ` `# the contents of an array ` `def` `printArr(arr,n): ` `    ``for` `i ``in` `range``(n): ` `        ``print``(arr[i], end ``=` `" "``) ` ` `  `# Function to update the array ` `def` `updateArr(arr, n): ` `    ``for` `i ``in` `range``(n): ` ` `  `        ``# In case of even positioned element ` `        ``if` `((i ``+` `1``) ``%` `2` `=``=` `0``): ` `            ``arr[i] ``=` `pow``(arr[i], ``2``) ` ` `  `        ``# Odd positioned element ` `        ``else``: ` `            ``arr[i] ``=` `pow``(arr[i], ``3``) ` `     `  `    ``# Print the updated array ` `    ``printArr(arr, n) ` ` `  `# Driver code ` `arr ``=` `[ ``2``, ``3``, ``4``, ``5``, ``6` `] ` `n ``=` `len``(arr) ` ` `  `updateArr(arr, n) ` ` `  `# This code is contributed ` `# by mohit kumar `

## C#

 `// C# implementation of the approach ` `using` `System; ` `     `  `class` `GFG ` `{ ` `     `  `// Utility function to print ` `// the contents of an array ` `static` `void` `printArr(``int` `[]arr, ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``Console.Write(arr[i] + ``" "``); ` `} ` ` `  `// Function to update the array ` `static` `void` `updateArr(``int` `[]arr, ``int` `n) ` `{ ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` ` `  `        ``// In case of even positioned element ` `        ``if` `((i + 1) % 2 == 0) ` `            ``arr[i] = (``int``)Math.Pow(arr[i], 2); ` ` `  `        ``// Odd positioned element ` `        ``else` `            ``arr[i] = (``int``)Math.Pow(arr[i], 3); ` `    ``} ` ` `  `    ``// Print the updated array ` `    ``printArr(arr, n); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]arr = { 2, 3, 4, 5, 6 }; ` `    ``int` `n = arr.Length; ` ` `  `    ``updateArr(arr, n); ` `} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## PHP

 ` `

Output:

```8 9 64 25 216
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up An Machine learner enthusiastic who is presently pursuing Bachelors in Computer Science and System Engineering from KIIT , Deemed to be University , Bhubaneswar , Odisha Seeking an entry level position at a respected organization, enhance my educational and professional skills and gain practical exp

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.