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++ program to implement // the above approach #include <bits/stdc++.h> 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[0]);
printAlter(arr, N);
} |
// C program to implement // the above approach #include <stdio.h> // 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[0]);
printAlter(arr, N);
} |
// 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 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# 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 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.
|
1 3 5
Time Complexity: O(N)
Auxiliary Space: O(1)
Recursive Approach : Here is the recursive solution of the same idea, to print the alternate elements, we have to increase the index with +2.
Following is the implementation.
// C++ code for the above approach #include <bits/stdc++.h> using namespace std;
// Function to print Alternate elements // of the given array void solve( int arr[], int N, int i)
{ // if the array has been traversed
if (i == N) {
return ;
}
// Checking if last element
if (i == N - 1) {
cout << arr[i] << " " ;
return ;
}
// if it's not the
// last element, print it
cout << arr[i] << " " ;
// call on the subproblem
solve(arr, N, i + 2);
} void printAlter( int arr[], int N)
{ // Print elements at odd positions
solve(arr, N, 0);
} // Driver Code int main()
{ int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
printAlter(arr, N);
} // this code is contributed by rajdeep999 |
import java.util.*;
public class Main {
// Function to print Alternate elements
// of the given array
static void solve( int arr[], int N, int i)
{
// if the array has been traversed
if (i == N) {
return ;
}
// Checking if last element
if (i == N - 1 ) {
System.out.print(arr[i] + " " );
return ;
}
// if it's not the
// last element, print it
System.out.print(arr[i] + " " );
// call on the subproblem
solve(arr, N, i + 2 );
}
static void printAlter( int arr[], int N)
{
// Print elements at odd positions
solve(arr, N, 0 );
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
int N = arr.length;
printAlter(arr, N);
}
} |
def solve(arr, N, i):
# if the array has been traversed
if i = = N:
return
# Checking if last element
if i = = N - 1 :
print (arr[i], end = " " )
return
# if it's not the last element, print it
print (arr[i], end = " " )
# call on the subproblem
solve(arr, N, i + 2 )
def printAlter(arr, N):
# Print elements at odd positions
solve(arr, N, 0 )
# Driver code arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
N = len (arr)
printAlter(arr, N) |
// C# code for the above approach using System;
class Program {
// Function to print Alternate elements
// of the given array
static void solve( int [] arr, int N, int i)
{
// if the array has been traversed
if (i == N) {
return ;
}
// Checking if last element
if (i == N - 1) {
Console.Write(arr[i] + " " );
return ;
}
// if it's not the
// last element, print it
Console.Write(arr[i] + " " );
// call on the subproblem
solve(arr, N, i + 2);
}
static void printAlter( int [] arr, int N)
{
// Print elements at odd positions
solve(arr, N, 0);
}
// Driver Code
static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6, 7 };
int N = arr.Length;
printAlter(arr, N);
}
} |
// Javascript program for the above approach function solve(arr, N, i) {
// if the array has been traversed if (i == N) {
return ;
}
// Checking if last element
if (i == N - 1) {
ans.push(arr[i]);
return ;
}
// if it's not the last element, print it
ans.push(arr[i]);
// call on the subproblem
solve(arr, N, i + 2);
} function printAlter(arr, N) {
// Print elements at odd positions
solve(arr, N, 0);
} // Driver code let arr = [1, 2, 3, 4, 5, 6, 7]; let N = arr.length; let ans = []; printAlter(arr, N); console.log(ans.join( " " ));
// This code is contributed by rishab |
1 3 5 7
Time Complexity: O(N)
Auxiliary Space: O(N), for recursive call stack
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++ program to implement // the above approach #include <bits/stdc++.h> 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[0]);
printAlter(arr, N);
} |
// C program to implement // the above approach #include <stdio.h> // 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[0]);
printAlter(arr, N);
} |
// 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 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# 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 |
<script> // 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 += 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)
Method 3: Using Slicing in Python:
Slice by using python list slicing by setting step value to 2.
Below is the implementation:
#include <iostream> using namespace std;
// Function to print alternate elements of the given array void PrintAlter( int arr[], int N)
{ // Print elements at odd positions by using slicing
// We use a loop to iterate over the array and print
// the elements at odd positions
for ( int i = 0; i < N; i += 2) {
cout << arr[i] << " " ;
}
} // Driver Code int main()
{ int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
PrintAlter(arr, N);
return 0;
} // This code is contributed by shiv1o43g |
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 by using slicing
// We use a loop to iterate over the array and print
// the elements at odd positions
for ( int i = 0 ; i < N; i += 2 ) {
System.out.print(arr[i] + " " );
}
}
// Driver Code
public static void main (String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
PrintAlter(arr, N);
}
} |
# 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 |
using System;
class Program {
// Function to print
// Alternate elements
// of the given array
static void PrintAlter( int [] arr, int N)
{
// Print elements at odd positions by using slicing
// We use a loop to iterate over the array and print
// the elements at odd positions
for ( int i = 0; i < N; i += 2) {
Console.Write(arr[i] + " " );
}
}
// Driver Code
static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
PrintAlter(arr, N);
}
} // This code is contributed by sarojmcy2e |
// 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 by using slicing
// we use spread operator to print with spaces
console.log(...arr.filter((_, i) => i % 2 === 0));
} // Driver Code const arr = [1, 2, 3, 4, 5]; const N = arr.length; printAlter(arr, N); |
1 3 5
Time Complexity: O(N)
Space Complexity: O(1)