0000 Finds odd and even elements from the array and stored in two separate arrays | MyCareerwise

FOR FREE CONTENT

Finds odd and even elements from the array and stored in two separate arrays

Back to Programming

Description

The elements of the array are taken as input. The program finds out the odd and even elements from the array and stores in two separate arrays.

Algorithm

INPUT: Array elements
OUTPUT: the two arrays containing odd and even numbers separately
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
Step 2: [separating the odd and even elements into two separate arrays]
	Set o<-0
	Set e<-0
	For i=0 to n-1 repeat
		If a[i] mod 2=0 then
			Set even[e]<-a[i]
			Set e<-e+1
		else
			Set odd[o]<-a[i]
			Set o<-o+1
		[End of ‘if’]
	[End of ‘for’ loop]
	For i=0 to e-1 repeat
		Print even[i]
	[End of ‘for’ loop]
	For i=0 to o-1 repeat
		Print odd[i]
	[End of ‘for’ loop]
Step 3: Stop.

Code

COMPLEXITY:

The time complexity to take the number of elements is O(n) and the time complexity to separate the odd and even elements is also O(n).

Therefore, the total time complexity of this program is O(n).