Incrementa los elementos posicionados impares en 1 y disminuye los elementos posicionados pares en 1 en una array

Dada una array arr[] , la tarea es incrementar todos los elementos impares en 1 y disminuir todos los pares en 1 .

Ejemplos: 

Entrada: arr[] = {3, 6, 8} 
Salida: 4 5 9

Entrada: arr[] = {9, 7, 3} 
Salida: 10 6 4

Enfoque: recorra la array elemento por elemento y, si la posición del elemento actual es impar, increméntela en 1; de lo contrario, disminuya en 1. Imprima el contenido de la array actualizada al final.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

<?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 increment all the odd
// positioned elements by 1 and decrement
// all the even positioned elements by 1
function updateArr($arr, $n)
{
    for ($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
$arr = array( 3, 6, 8 );
$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)
{
     var i;
    for (i = 0; i < n; i++)
      document.write(arr[i] + " ");
}
 
// Function to increment all the odd
// positioned elements by 1 and decrement
// all the even positioned elements by 1
function updateArr(arr, n)
{
    var i;
    for (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
    var arr = [3, 6, 8];
    var n = arr.length;
    updateArr(arr, n);
     
</script>
Producción: 

4 5 9

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por ShobhangiSarkar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *