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 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[0]); printAlter(arr, N); } |
C
// C program to implement // the above approach #include <stdio.h> // Function to print // Alternate elemnts // 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[0]); printAlter(arr, N); } |
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to print // Alternate elemnts // 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 elemnts # 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 elemnts // 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 |
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 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[0]); printAlter(arr, N); } |
C
// C program to implement // the above approach #include <stdio.h> // Function to print // Alternate elemnts // 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[0]); printAlter(arr, N); } |
Java
// Java program to implement // the above approach import java.io.*; class GFG{ // Function to print // Alternate elemnts // 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 elemnts # 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 elemnts // 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 |
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.