Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array

Given an array arr[], the task is increment all the odd positioned elements by 1 and decrement all the even positioned elements by 1.

Examples:

Input: arr[] = {3, 6, 8}
Output: 4 5 9

Input: arr[] = {9, 7, 3}
Output: 10 6 4

Approach: Traverse the array element by element and if the current element’s position is odd then increment it by 1 else decrement it by 1. Prin the contents of the updated array in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

Output:

4 5 9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.