Related Articles

# Print alternate elements of an array

• Difficulty Level : Basic
• Last Updated : 08 Jun, 2021

Given an array, arr[] of size N, the task is to print the elements of the given array present at odd indices (1-based indexing).

Examples:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 1 3 5
Explanation:
Array element present at odd positions are: {1, 3, 5}.
Therefore, the required output is 1 3 5.

Input: arr[] = {-5, 1, 4, 2, 12}
Output: -5 4 12

Naive Approach: The simplest approach to solve this problem is to traverse the given array and check if the position of the current element is odd or not. If found to be true, then print the current element.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to print``// Alternate elements``// of the given array``void` `printAlter(``int` `arr[], ``int` `N)``{``    ``// Print elements``    ``// at odd positions``    ``for` `(``int` `currIndex = 0;``         ``currIndex < N; currIndex++) {` `        ``// If currIndex stores even index``        ``// or odd position``        ``if` `(currIndex % 2 == 0) {``            ``cout << arr[currIndex] << ``" "``;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``printAlter(arr, N);``}`

## C

 `// C program to implement``// the above approach` `#include ` `// Function to print``// Alternate elements``// of the given array``void` `printAlter(``int` `arr[], ``int` `N)``{``    ``// Print elements``    ``// at odd positions``    ``for` `(``int` `currIndex = 0;``         ``currIndex < N; currIndex++) {` `        ``// If currIndex stores even index``        ``// or odd position``        ``if` `(currIndex % 2 == 0) {``            ``printf``(``"%d "``, arr[currIndex]);``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``printAlter(arr, N);``}`

## Java

 `// Java program to implement``// the above approach``import` `java.io.*;` `class` `GFG{` `// Function to print``// Alternate elements``// of the given array``static` `void` `printAlter(``int``[] arr, ``int` `N)``{``    ` `    ``// Print elements``    ``// at odd positions``    ``for``(``int` `currIndex = ``0``;``            ``currIndex < N;``            ``currIndex++)``    ``{``        ` `        ``// If currIndex stores even index``        ``// or odd position``        ``if` `(currIndex % ``2` `== ``0``)``        ``{``            ``System.out.print(arr[currIndex] + ``" "``);``        ``}``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[] arr = { ``1``, ``2``, ``3``, ``4``, ``5` `};``    ``int` `N = arr.length;``    ` `    ``printAlter(arr, N);``}``}` `// This code is contributed by akhilsaini`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to print``# Alternate elements``# of the given array``def` `printAlter(arr, N):``    ` `    ``# Print elements``    ``# at odd positions``    ``for` `currIndex ``in` `range``(``0``, N):``        ` `        ``# If currIndex stores even index``        ``# or odd position``        ``if` `(currIndex ``%` `2` `=``=` `0``):``            ``print``(arr[currIndex], end ``=` `" "``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ` `    ``arr ``=` `[ ``1``, ``2``, ``3``, ``4``, ``5` `]``    ``N ``=` `len``(arr)``    ` `    ``printAlter(arr, N)` `# This code is contributed by akhilsaini`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG{` `// Function to print``// Alternate elements``// of the given array``static` `void` `printAlter(``int``[] arr, ``int` `N)``{``    ` `    ``// Print elements``    ``// at odd positions``    ``for``(``int` `currIndex = 0;``            ``currIndex < N;``            ``currIndex++)``    ``{``        ` `        ``// If currIndex stores even index``        ``// or odd position``        ``if` `(currIndex % 2 == 0)``        ``{``            ``Console.Write(arr[currIndex] + ``" "``);``        ``}``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] arr = { 1, 2, 3, 4, 5 };``    ``int` `N = arr.Length;``    ` `    ``printAlter(arr, N);``}``}` `// This code is contributed by akhilsaini`

## Javascript

 `// javascript program to implement``// the above approach` `// Function to print``// Alternate elements``// of the given array``function` `printAlter(arr, N)``{``     ` `    ``// Print elements``    ``// at odd positions``    ``for``(``var` `currIndex = 0; currIndex < N; currIndex++)``    ``{``         ` `        ``// If currIndex stores even index``        ``// or odd position``        ``if` `(currIndex % 2 == 0)``        ``{``            ``document.write(arr[currIndex] + ``" "``);``        ``}``    ``}``}`` ` `// Driver Code` `    ``var` `arr = [ 1, 2, 3, 4, 5 ]``    ``var` `N = arr.length;``     ` `    ``printAlter(arr, N);` ` ``// This code is contributed by bunnyram19.`
Output:
`1 3 5`

Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to traverse only those elements of the given array which are present at odd positions. Follow the steps below to solve the problem:

• Iterate a loop with a loop variable currIndex from 0 to N.
• Print the value of arr[currIndex] and increment the value of currIndex by 2 until currIndex exceeds N.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to print``// Alternate elements``// of the given array``void` `printAlter(``int` `arr[], ``int` `N)``{``    ``// Print elements``    ``// at odd positions``    ``for` `(``int` `currIndex = 0;``         ``currIndex < N; currIndex += 2) {` `        ``// Print elements of array``        ``cout << arr[currIndex] << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``printAlter(arr, N);``}`

## C

 `// C program to implement``// the above approach` `#include ` `// Function to print``// Alternate elements``// of the given array``void` `printAlter(``int` `arr[], ``int` `N)``{``    ``// Print elements``    ``// at odd positions``    ``for` `(``int` `currIndex = 0;``         ``currIndex < N; currIndex += 2) {` `        ``// Print elements of array``        ``printf``(``"%d "``, arr[currIndex]);``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 4, 5 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);``    ``printAlter(arr, N);``}`

## Java

 `// Java program to implement``// the above approach``import` `java.io.*;` `class` `GFG{` `// Function to print``// Alternate elements``// of the given array``static` `void` `printAlter(``int``[] arr, ``int` `N)``{``    ` `    ``// Print elements``    ``// at odd positions``    ``for``(``int` `currIndex = ``0``;``            ``currIndex < N;``            ``currIndex += ``2``)``    ``{``        ` `        ``// Print elements of array``        ``System.out.print(arr[currIndex] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[] arr = { ``1``, ``2``, ``3``, ``4``, ``5` `};``    ``int` `N = arr.length;``    ` `    ``printAlter(arr, N);``}``}` `// This code is contributed by akhilsaini`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to print``# Alternate elements``# of the given array``def` `printAlter(arr, N):``    ` `    ``# Print elements``    ``# at odd positions``    ``for` `currIndex ``in` `range``(``0``, N, ``2``):``        ` `        ``# Print elements of array``        ``print``(arr[currIndex], end ``=` `" "``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[ ``1``, ``2``, ``3``, ``4``, ``5` `]``    ``N ``=` `len``(arr)``    ` `    ``printAlter(arr, N)` `# This code is contributed by akhilsaini`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG{` `// Function to print``// Alternate elements``// of the given array``static` `void` `printAlter(``int``[] arr, ``int` `N)``{``    ` `    ``// Print elements``    ``// at odd positions``    ``for``(``int` `currIndex = 0;``            ``currIndex < N;``            ``currIndex += 2)``    ``{``        ` `        ``// Print elements of array``        ``Console.Write(arr[currIndex] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int``[] arr = { 1, 2, 3, 4, 5 };``    ``int` `N = arr.Length;``    ` `    ``printAlter(arr, N);``}``}` `// This code is contributed by akhilsaini`

## Javascript

 ``
Output:
`1 3 5`

Time Complexity: O(N)
Auxiliary Space: O(1)

#### Method 3: Using Slicing in Python:

Slice by using python list slicing by setting step value to 2.

Below is the implementation:

## Python3

 `# Python3 program to implement``# the above approach` `# Function to print``# Alternate elements``# of the given array``def` `printAlter(arr, N):` `    ``# Print elements``    ``# at odd positions by using slicing``    ``# we use * to print with spaces``    ``print``(``*``arr[::``2``])`  `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[``1``, ``2``, ``3``, ``4``, ``5``]``    ``N ``=` `len``(arr)` `    ``printAlter(arr, N)` `# This code is contributed by vikkycirus`

#### Output:

`1 3 5`

Time Complexity: O(N)

Space Complexity: O(1)

Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students

My Personal Notes arrow_drop_up