给定n个元素的数组arr [] ,任务是用其立方体替换所有奇数位置的元素,并用其正方形替换偶数位置的元素,即结果数组必须为{arr [0] 3 ,arr [1] 2 ,arr [2] 3 ,arr [3] 2 ,…} 。
例子:
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
方法:对于数组arr [i]的任何元素,仅当(i + 1)为奇数时才是奇数位置,因为索引从0开始。现在,遍历数组,将所有奇数定位的元素替换为它们的立方体,甚至将定位的元素替换为其正方形。
下面是上述方法的实现:
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[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
Javascript
输出:
8 9 64 25 216
时间复杂度: O(n)
辅助空间: O(1)
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。