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 <bits/stdc++.h>using namespace std;// Function to print// Alternate elemnts// 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
// C program to implement// the above approach#include <stdio.h>// Function to print// Alternate elemnts// 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
// Java program to implement// the above approachimport java.io.*;class GFG{// Function to print// Alternate elemnts// 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
# Python3 program to implement# the above approach# Function to print# Alternate elemnts# 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#
// C# program to implement// the above approachusing System;class GFG{// Function to print// Alternate elemnts// 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 |
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 <bits/stdc++.h>using namespace std;// Function to print// Alternate elemnts// 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
// C program to implement// the above approach#include <stdio.h>// Function to print// Alternate elemnts// 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
// Java program to implement// the above approachimport java.io.*;class GFG{// Function to print// Alternate elemnts// 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
# Python3 program to implement# the above approach# Function to print# Alternate elemnts# 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#
// C# program to implement// the above approachusing System;class GFG{// Function to print// Alternate elemnts// 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 |
1 3 5
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print alternate nodes from all levels of a Binary Tree
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Sum of alternate elements of a N x N matrix
- Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence
- Alternate XOR operations on sorted array
- Lexicographical smallest alternate Array
- Generate an alternate increasing and decreasing Array
- Find the Maximum Alternate Subsequence Sum from a given array
- Position after taking N steps to the right and left in an alternate manner
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Swap Alternate Boundary Pairs
- Print elements of an array according to the order defined by another array | set 2
- Print X array elements closest to the Kth smallest element in the array
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Map elements of an array to elements of another array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Check if Array elements can be maximized upto M by adding all elements from another array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count array elements that can be represented as sum of at least two consecutive array elements
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.
Improved By : akhilsaini

