Dada una array arr[] de n elementos, la tarea es reemplazar todos los elementos con posiciones impares con sus cubos y los elementos con posiciones pares con sus cuadrados, es decir, la array resultante debe ser {arr[0] 3 , arr[1] 2 , arr [2] 3 , arr[3] 2 , …} .
Ejemplos:
Entrada: arr[]= {2, 3, 4, 5}
Salida: 8 9 64 25 La
array actualizada será {2 3 , 3 2 , 4 3 , 5 2 } -> {8, 9, 64, 25}
Entrada : arr[] = {3, 4, 5, 2}
Salida: 27 16 125 4
Enfoque: Para cualquier elemento de la array arr[i] , se posiciona impar solo si (i + 1) es impar ya que la indexación comienza desde 0 . Ahora, recorra la array y reemplace todos los elementos en posiciones impares con sus cubos y los elementos en posiciones pares con sus cuadrados.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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[0]); 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
<?php // PHP implementation of the approach // Utility function to print // the contents of an array function printArr($arr, $n) { for ($i = 0; $i < $n; $i++) echo $arr[$i] . " "; } // Function to update the array function updateArr($arr, $n) { for ($i = 0; $i < $n; $i++) { // 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 = array( 2, 3, 4, 5, 6 ); $n = count($arr); updateArr($arr, $n); // This code is contributed by mits ?>
Javascript
<script> // javascript implementation of the approach // Utility function to print // the contents of an array function printArr(arr , n) { for (i = 0; i < n; i++) document.write(arr[i] + " "); } // Function to update the array function updateArr(arr , n) { for (i = 0; i < n; i++) { // In case of even positioned element if ((i + 1) % 2 == 0) arr[i] = parseInt( Math.pow(arr[i], 2)); // Odd positioned element else arr[i] = parseInt( Math.pow(arr[i], 3)); } // Print the updated array printArr(arr, n); } // Driver code var arr = [ 2, 3, 4, 5, 6 ]; var n = arr.length; updateArr(arr, n); // This code contributed by gauravrajput1 </script>
8 9 64 25 216
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por avinash1605 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA