📌  相关文章
📜  在数组中将奇数位置的元素增加1,而偶数位置的元素减少1

📅  最后修改于: 2021-05-04 09:48:44             🧑  作者: Mango

给定数组arr [] ,任务是将所有奇数定位的元素1,并将所有偶数定位的元素1
例子:

方法:逐元素遍历数组元素,如果当前元素的位置是奇数,则将其递增1,否则递减1。最后将更新后的数组的内容删除。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print
// the contents of an array
void printArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to increment all the odd
// positioned elements by 1 and decrement
// all the even positioned elements by 1
void updateArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
 
        // If current element is odd positioned
        if ((i + 1) % 2 == 1)
            arr[i]++;
 
        // If even positioned
        else
            arr[i]--;
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
int main()
{
    int arr[] = { 3, 6, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    updateArr(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
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 increment all the odd
// positioned elements by 1 and decrement
// all the even positioned elements by 1
static void updateArr(int arr[], int n)
{
    for (int i = 0; i < n; i++)
 
        // If current element is odd positioned
        if ((i + 1) % 2 == 1)
            arr[i]++;
 
        // If even positioned
        else
            arr[i]--;
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 6, 8 };
    int n = arr.length;
    updateArr(arr, n);
 
}
}
 
// This code is contributed by Prerna Saini


Python3
# Python3 implementation of the approach
 
# Utility function to print
# the contents of an array
def printArr(arr, n):
    for i in range(0, n):
        print(arr[i], end = " ");
 
# Function to increment all the odd
# positioned elements by 1 and decrement
# all the even positioned elements by 1
def updateArr(arr, n):
    for i in range(0, n):
 
        # If current element is odd positioned
        if ((i + 1) % 2 == 1):
            arr[i] += 1;
 
        # If even positioned
        else:
            arr[i] -= 1;
 
    # Print the updated array
    printArr(arr, n);
 
# Driver code
if __name__ == '__main__':
    arr = [3, 6, 8];
    n = len(arr);
    updateArr(arr, n);
 
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach
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.Console.Write(arr[i] + " ");
}
 
// Function to increment all the odd
// positioned elements by 1 and decrement
// all the even positioned elements by 1
static void updateArr(int []arr, int n)
{
    for (int i = 0; i < n; i++)
 
        // If current element is odd positioned
        if ((i + 1) % 2 == 1)
            arr[i]++;
 
        // If even positioned
        else
            arr[i]--;
 
    // Print the updated array
    printArr(arr, n);
}
 
// Driver code
static void Main()
{
    int []arr = { 3, 6, 8 };
    int n = arr.Length;
    updateArr(arr, n);
 
}
}
 
// This code is contributed by mits


PHP


输出:
4 5 9

时间复杂度: O(n)

辅助空间: O(1)