Print Alternate Elements of an Array

Print Alternate Elements of an Array” is elementary problem of array data structure. Here, we are given an array of size ‘n’ and our task is to print alternate elements of an array. Alternate elements defines, we are not supposed to print two subsequent elements.

Example (Print Alternate Elements of an Array):

INPUT:
Arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
OUTPUT:
1 ,3, 5, 7, 9
OR
2, 4, 6, 8, 10

The steps required to print alternate elements of an array are as follows:

  • Scan the size of the array from user.
  • Scan the array elements from the user.
  • To print alternate element of the array, we have two choices:
  • Start from array element with index 0 (arr[0]) and then iterate with index + 2. So, here all the even index elements will be printed.
  • Start from array element with index 1(arr[1]) and then iterate with index +2. So, here all the odd index elements will be printed.

C++ Program to print alternate elements of an array is as follows:

/* C++ Program to print alternate elements of an array */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan size of array */  
    int n;  
    cout<<"Enter the size of the array:";  
    cin>>n;  
      
    /*Scan array elements */  
    int arr[n];  
    cout<<"\nEnter the array elements:";  
    for(int i = 0; i < n; i++)  
    {  
        cin>>arr[i];  
    }  
      
    /*print alternate elements */  
    cout<<"\nThe alternate elements of the array are:\n";  
    for(int i = 0; i < n; i = i+2)  
    {  
        cout<<arr[i]<<" ";  
    }  
}  

OUTPUT:
Enter the size of the array: 9
Enter the array elements: 11 22 33 44 55 66 77 88 99
The alternate elements of the array are:
11 33 55 77 99
Time Complexity:
O(n), where n is the size of the array

Related Posts:

  1. Program to swap kth element from beginning to kth element from end in an Array.
  2. Program to print all possible subarrays of the given array.
  3. Program to print kth smallest and kth largest element of an Array.
  4. Program to find equilibrium index of an Array.
  5. Program to find majority element of an Array.
  6. Program to find mean of the Array.
  7. Program to sort an Array of 0s and 1s.
  8. Program to Reverse an Array.
  9. Program to count number of odd numbers and even numbers in an array.
  10. Program to find absolute difference between sum of odd index elements and even index elements.
  11. Program to find smallest and largest element of the array.
  12. Program to count occurrences of a particular element in an array.
  13. Program to search an element in an Array (Linear Search).
  14. Program to Rotate Array Elements by ‘D’ positions.
  15. Program to find most frequent element in an array.
  16. Program to find pair in an array with given sum.
  17. Program to find first repeating element of an array.
  18. Program to merge two sorted arrays.
  19. Program to find missing number in an array.
  20. Program to sort if array is sorted.

You may also like...