You are given an array A of size N. You need to print elements of A in alternate order (starting from index 0).
Example 1:
Input:
N = 4
A[] = {1, 2, 3, 4}
Output:
1 3
Example 2:
Input:
N = 5
A[] = {1, 2, 3, 4, 5}
Output:
1 3 5
Your Task:
Since this is a function problem, you just need to complete the provided function print() which takes A and n as input parameters and print the resultant array in the function itself. You have to print the final number of array ending with a space as Printing of newline character is taken care in driver code itself.
Constraints:
1 <= N <= 10^{5}
1 <= A_{i }<= 10^{5}
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



# arr is the array

# n is the number of elements in array

def printAl(arr,n):

    # your code here

Solutions:
def printAl(arr, n):
	for i in range(0, n, 2):
		print(arr[i], end=' ')


def printAl(arr, n):
	for i in arr[::2]:
		print(i, end=' ')


def printAl(arr, n):
	if n % 2 == 0:
		del arr[-1]
	altarr = []
	for i in range(0, n):
		if i % 2 == 0:
			altarr.append(arr[i])
	for i in altarr:
		print(i, end=' ')


import math

def printAl(arr, n):
	for i in range(n):
		if i % 2 == 0:
			print(arr[i], end=' ')


def printAl(arr, n):
	for (idx, i) in enumerate(arr):
		if idx % 2 == 0:
			print(i, end=' ')


def printAl(arr, n):
	a = arr[0:n:2]
	for i in a:
		print(i, end=' ')


from typing import List

def printAl(arr: List, n: int) -> List:
	a = arr[0:n:2]
	for i in a:
		print(i, end=' ')


def printAl(arr, n):
	for (i, a) in enumerate(arr):
		if i % 2 == 0:
			print(a, end=' ')
		else:
			continue


def printAl(arr, n):
	w = []
	for i in range(0, len(arr)):
		if i % 2 == 0:
			print(arr[i], end=' ')


def printAl(arr, n):
	arr2 = []
	flip = True
	for i in arr:
		if flip:
			print(i, end=' ')
			flip = False
		else:
			flip = True


def printAl(arr, n):
	i = 0
	list = []
	while i < n:
		list.append(arr[i])
		i = i + 2
	for i in list:
		print(i, end=' ')


def printAl(arr, n):
	A = []
	for i in range(n):
		if i % 2 == 0:
			A.append(arr[i])
	for i in A:
		print(i, end=' ')


def printAl(arr, n):
	i = 0
	n = len(arr)
	while i < n:
		print(arr[i], end=' ')
		i = i + 2


def printAl(arr, n):
	for i in range(len(arr)):
		if i == 0:
			print(arr[i], end=' ')
		elif i % 2 == 0:
			print(arr[i], end=' ')
		else:
			continue


def printAl(arr, n):
	res = []
	for i in range(len(arr)):
		if i % 2 == 0:
			res.append(arr[i])
	for i in res:
		print(i, end=' ')


def printAl(arr, n):
	result = []
	i = 0
	while i < len(arr):
		result.append(arr[i])
		i += 2
	for j in result:
		print(j, end=' ')


def printAl(arr, n):
	ans = []
	for i in range(n):
		if i % 2 == 0:
			ans.append(arr[i])
	for j in range(len(ans)):
		print(ans[j], end=' ')


def printAl(arr, n):
	for x in range(n):
		if x % 2 == 0:
			print(arr[x], end=' ')


def printAl(arr, n):
	do_print = True
	str_join = []
	for i in range(n):
		if do_print:
			print(arr[i], end=' ')
			do_print = False
		else:
			do_print = True