How to increment array index in javascript

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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. Print the contents of the updated array in the end.

    Below is the implementation of the above approach:  

    C++

    #include <bits/stdc++.h>

    using namespace std;

    void printArr(int arr[], int n)

    {

        for (int i = 0; i < n; i++)

            cout << arr[i] << " ";

    }

    void updateArr(int arr[], int n)

    {

        for (int i = 0; i < n; i++)

            if ((i + 1) % 2 == 1)

                arr[i]++;

            else

                arr[i]--;

        printArr(arr, n);

    }

    int main()

    {

        int arr[] = { 3, 6, 8 };

        int n = sizeof(arr) / sizeof(arr[0]);

        updateArr(arr, n);

        return 0;

    }

    Java

    class GfG

    {

    static void printArr(int arr[], int n)

    {

        for (int i = 0; i < n; i++)

            System.out.print(arr[i] + " ");

    }

    static void updateArr(int arr[], int n)

    {

        for (int i = 0; i < n; i++)

            if ((i + 1) % 2 == 1)

                arr[i]++;

            else

                arr[i]--;

        printArr(arr, n);

    }

    public static void main(String[] args)

    {

        int arr[] = { 3, 6, 8 };

        int n = arr.length;

        updateArr(arr, n);

    }

    }

    Python3

    def printArr(arr, n):

        for i in range(0, n):

            print(arr[i], end = " ");

    def updateArr(arr, n):

        for i in range(0, n):

            if ((i + 1) % 2 == 1):

                arr[i] += 1;

            else:

                arr[i] -= 1;

        printArr(arr, n);

    if __name__ == '__main__':

        arr = [3, 6, 8];

        n = len(arr);

        updateArr(arr, n);

    C#

    class GfG

    {

    static void printArr(int []arr, int n)

    {

        for (int i = 0; i < n; i++)

            System.Console.Write(arr[i] + " ");

    }

    static void updateArr(int []arr, int n)

    {

        for (int i = 0; i < n; i++)

            if ((i + 1) % 2 == 1)

                arr[i]++;

            else

                arr[i]--;

        printArr(arr, n);

    }

    static void Main()

    {

        int []arr = { 3, 6, 8 };

        int n = arr.Length;

        updateArr(arr, n);

    }

    }

    PHP

    <?php

    function printArr($arr, $n)

    {

        for ($i = 0; $i < $n; $i++)

            echo $arr[$i] . " ";

    }

    function updateArr($arr, $n)

    {

        for ($i = 0; $i < $n; $i++)

            if (($i + 1) % 2 == 1)

                $arr[$i]++;

            else

                $arr[$i]--;

        printArr($arr, $n);

    }

    $arr = array( 3, 6, 8 );

    $n = count($arr);

    updateArr($arr, $n);

    ?>

    Javascript

    <script>

    function printArr(arr, n)

    {

         var i;

        for (i = 0; i < n; i++)

          document.write(arr[i] + " ");

    }

    function updateArr(arr, n)

    {

        var i;

        for (i = 0; i < n; i++)

            if ((i + 1) % 2 == 1)

                arr[i]++;

            else

                arr[i]--;

        printArr(arr, n);

    }

        var arr = [3, 6, 8];

        var n = arr.length;

        updateArr(arr, n);

    </script>

    Time Complexity: O(n)

    Auxiliary Space: O(1)


    Can we increment the array?

    No, it's not OK to increment an array. Although arrays are freely convertible to pointers, they are not pointers.

    How do you increase the index of an array in Java?

    Increase the Array Size Using the Arrays. copyOf() Method in Java. Java has a built-in copyOf() method that can create a new array of a larger size and copy our old array elements into the new one. The copyOf() function belongs to the Arrays class.

    How do you increment an array in a for loop?

    First, Initializing the array arr[] with pre-defined values, and after that, the length of the array should be calculated. Then use a loop, to perform the operation that is to increment the values one by one with the help of for loop.

    How do you expand an array in JavaScript?

    To extend an existing array in JavaScript, use the Array. concat() method.