Java Program to Print the Elements of an Array Present on Odd Position
Last Updated :
27 Nov, 2020
An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1".
The odd positions in the array are those with even indexing and vice versa.
Example:
Input : arr[] = { 10,20,30,40,50 }
Output : 10 30 50
Input : arr1[] = { -5,0,2,5,76,9 }
Output : -5 2 76
We will be printing elements at an odd position by two approaches:
- By checking if the position is odd i.e divisible by 2 or not(since the indexing starts from 0), then print the elements.
- By maintaining a flag pointer that will be initialized to 1 and start iterating over the array. If the flag is 1, print that element and change the flag to 0, else if the flag is 0, make the flag to 1.
Approach 1:
By checking if the position is odd i.e divisible by 2 or not(since the indexing starts from 0 in the array), then print the elements.
Java
// Java program to print the elements at odd positions
import java.util.*;
public class PrintOddElementsInArray {
public static void main(String[] args)
{
// Initialized array
int inputArray[] = new int[] { 100, -500, 450, -200,
1000, -213, 750 };
System.out.println("Existing array elements ..");
for (int i = 0; i < inputArray.length; i++) {
System.out.println(inputArray[i]);
}
System.out.println(
"Array elements at odd position..");
// Though the logic looks like taking even position,
// if 10,20,30,40,50 are elements we need to get
// 10,30,50. So followed the logic like this
for (int i = 0; i < inputArray.length; i++) {
if (i % 2 == 0) {
System.out.println(inputArray[i]);
}
}
}
}
OutputExisting array elements ..
100
-500
450
-200
1000
-213
750
Array elements at odd position..
100
450
1000
750
- Time Complexity: O(n)
- Space Complexity: O(1)
Approach 2:
By maintaining a flag pointer that will be initialized to 1 and start iterating over the array. If the flag is 1, print that element and change the flag to 0, else if the flag is 0, make the flag to 1.
Java
// Java program to print the elements at odd positions
import java.util.*;
public class PrintOddElementsInArray {
public static void main(String[] args)
{
// Initialized array
int inputArray[] = new int[] { 100, -500, 450, -200,
1000, -213, 750 };
System.out.println("Existing array elements ..");
for (int i = 0; i < inputArray.length; i++) {
System.out.println(inputArray[i]);
}
System.out.println(
"Array elements at odd position..");
int flag = 1;
for (int i = 0; i < inputArray.length; i++) {
if (flag == 1) {
System.out.print(inputArray[i] + " ");
flag = 0;
}
else
flag = 1;
}
}
}
OutputExisting array elements ..
100
-500
450
-200
1000
-213
750
Array elements at odd position..
100 450 1000 750
- Time Complexity: O(n)
- Space Complexity: O(1)
Similar Reads
Java Program to Print the Elements of an Array Present on Even Position The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr
2 min read
Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem
6 min read
Java Program to Return the Elements at Odd Positions in a List Given a List, the task is to return the elements at Odd positions in a list. Let's consider the following list.Clearly, we can see that elements 20, 40, 60 are at Odd Positions as the index of the list is zero-based. Now we should return these elements.Approach 1:Initialize a temporary value with ze
3 min read
Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print
2 min read
Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value
3 min read
Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort()
2 min read