Print alternate elements of an array

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 5Explanation: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
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to print// Alternate elements// of the given arrayvoid 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 Codeint main(){    int arr[] = { 1, 2, 3, 4, 5 };    int N = sizeof(arr) / sizeof(arr[0]);    printAlter(arr, N);}
// C program to implement// the above approach #include <stdio.h> // Function to print// Alternate elements// of the given arrayvoid 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 Codeint main(){    int arr[] = { 1, 2, 3, 4, 5 };    int N = sizeof(arr) / sizeof(arr[0]);    printAlter(arr, N);}
// Java program to implement// the above approachimport java.io.*; class GFG{ // Function to print// Alternate elements// of the given arraystatic 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 Codepublic 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 program to implement# the above approach # Function to print# Alternate elements# of the given arraydef 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 Codeif __name__ == "__main__":         arr = [ 1, 2, 3, 4, 5 ]    N = len(arr)         printAlter(arr, N) # This code is contributed by akhilsaini
// C# program to implement// the above approachusing System; class GFG{ // Function to print// Alternate elements// of the given arraystatic 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 Codepublic static void Main(){    int[] arr = { 1, 2, 3, 4, 5 };    int N = arr.Length;         printAlter(arr, N);}} // This code is contributed by akhilsaini
// javascript program to implement// the above approach // Function to print// Alternate elements// of the given arrayfunction 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.
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
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to print// Alternate elements// of the given arrayvoid 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 Codeint main(){    int arr[] = { 1, 2, 3, 4, 5 };    int N = sizeof(arr) / sizeof(arr[0]);    printAlter(arr, N);}
// C program to implement// the above approach #include <stdio.h> // Function to print// Alternate elements// of the given arrayvoid 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 Codeint main(){    int arr[] = { 1, 2, 3, 4, 5 };    int N = sizeof(arr) / sizeof(arr[0]);    printAlter(arr, N);}
// Java program to implement// the above approachimport java.io.*; class GFG{ // Function to print// Alternate elements// of the given arraystatic 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 Codepublic 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 program to implement# the above approach # Function to print# Alternate elements# of the given arraydef printAlter(arr, N):         # Print elements    # at odd positions    for currIndex in range(0, N, 2):                 # Print elements of array        print(arr[currIndex], end = " ") # Driver Codeif __name__ == "__main__":     arr = [ 1, 2, 3, 4, 5 ]    N = len(arr)         printAlter(arr, N) # This code is contributed by akhilsaini
// C# program to implement// the above approachusing System; class GFG{ // Function to print// Alternate elements// of the given arraystatic 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 Codepublic static void Main(){    int[] arr = { 1, 2, 3, 4, 5 };    int N = arr.Length;         printAlter(arr, N);}} // This code is contributed by akhilsaini
<script> // Function to print// Alternate elements// of the given arrayfunction printAlter(arr, N){    // Print elements    // at odd positions    for (var currIndex = 0;        currIndex < N; currIndex += 2)        {         // Print elements of array        document.write( arr[currIndex] + " ");    }} var arr= [ 1, 2, 3, 4, 5 ];    var N = 5;    printAlter(arr, N);  </script>
1 3 5
 
Time Complexity: O(N)Auxiliary Space: O(1)
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 arraydef printAlter(arr, N):     # Print elements    # at odd positions by using slicing    # we use * to print with spaces    print(*arr[::2])  # Driver Codeif __name__ == "__main__":     arr = [1, 2, 3, 4, 5]    N = len(arr)     printAlter(arr, N) # This code is contributed by vikkycirus
1 3 5
Time Complexity: O(N)
Space Complexity: O(1)
akhilsaini
vikkycirus
bunnyram19
akshitsaxenaa09
clintra
sweetyty
Arrays
School Programming
Searching
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.