
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Odd Elements at Odd Index in Java
In this article, we will learn how to find and print the odd numbers that are present at odd index positions in an array. First we will understand the problem with the help of an example, and then we will learn three approaches to implement it in Java.
Example
Consider the array = [2, 1, 4, 4, 5, 7].
The odd numbers at odd indices are 1 and 7.
Explanation
The indexing of an array starts from 0. Here the element 1, which is an odd number, is present at index 1, which is an odd index. In the same way, the element 7, which is an odd number, is present at odd index 5.
Approach 1: Using For-loop
This approach involves iterating through the array using a for loop and checking each element at odd index positions if they are odd numbers or not.
Steps for implementation
Step 1: Initialize a for loop that starts from index 1 and increments by 2 in each step to check only the odd positions.
Step 2 : check if each number at odd positions is an odd number by using the modulus operator.
Step 3 : Print the number if it is odd.
Implementation code
Below is the implementation using a standard for loop to check for odd numbers at odd indices.
public class Odd_index { public static void main(String[] args) { int[] arr = {2,11,4,8,5,7,9}; System.out.print("Odd numbers at odd indices are: "); for (int i = 1; i < arr.length; i += 2) { if (arr[i] % 2 != 0) { System.out.print(arr[i] + " "); } } System.out.println(); } }
Output
Odd numbers at odd indices are: 11 7
Time complexity
O(n), where n equals array length, because we check half of array elements at most. O(n/2) will be O(n) after removing constants.
Space complexity
O(1), because we only use a fixed amount of extra space.
Approach 2: Using a For-each Loop
In this approach we use a for-each loop instead of a regular for loop.
Implementation code
The code below uses a for-each loop while maintaining an index variable to track odd positions.
public class Odd_index { public static void main(String[] args) { int[] arr = {5,3,7,4,15,1}; int index = 0; System.out.print("Odd numbers at odd indices are: "); for (int num : arr) { if (index % 2!= 0 && num % 2!= 0) { System.out.print(num + " "); } index++; } System.out.println(); } }
Output
Odd numbers at odd indices are: 3 1
Time complexity will be O(n), and space complexity will be O(1), which is the same as the previous approach.
Approach 3: Using Java Streams
In this third approach, we use Java Streams to filter the array and print the specific elements.
Steps for implementation
To implement this approach, follow the steps below ?
- Step 1: Use IntStream.range(0, arr.length) to create a stream of indices.
- Step 2: Use a filter (i % 2 != 0) to keep only the odd index positions.
- Step 3: Map each index to corresponding array elements to get the elements at those indices.
- Step 4: Use another filter (num % 2 != 0) to keep only odd numbers.
- Step 5: Print the final output.
Implementation code
The following code implements Java Streams to find and print odd numbers at odd indices.
import java.util.Arrays; import java.util.stream.IntStream; public class Odd_index { public static void main(String[] args) { int[] arr = {5,7,2,3,9,1,3,4}; System.out.print("Odd numbers at odd indices: "); IntStream.range(0, arr.length) .filter(i -> i % 2!= 0) .map(i -> arr[i]) .filter(num -> num % 2!= 0) .forEach(num -> System.out.print(num + " ")); System.out.println(); } }
Output
Odd numbers at odd indices: 7 3 1
Time Complexity
O(n), as stream operations iterate through the array once.
Space Complexity
O(n), as Java Streams use extra memory in the worst-case scenario.
Conclusion
We have explored 3 approaches to print the odd numbers at odd index positions in an array. The first approach using the for loop is simple and straightforward. The second approach uses the for-each loop, which is similar to the first approach. The third approach uses Java streams, which can be complex for beginners.