Given an array of N positive integers, print the positive integers that have both the adjacent element values as odd or even.Boundary Condition(s):3 <= N <= 1000Input Format:The first line contains N.The second line contains N elements separated by space(s).Output Format:The first line contains the elements (which have both the adjacent element values as odd or even) separated by a space.Example Input/Output 1:Input:710 21 20 33 98 66 29Output:21 20 33Example Input/Output 2:Input:511 21 30 99 52Output:30 99
Question
Given an array of N positive integers, print the positive integers that have both the adjacent element values as odd or even.Boundary Condition(s):3 <= N <= 1000Input Format:The first line contains N.The second line contains N elements separated by space(s).Output Format:The first line contains the elements (which have both the adjacent element values as odd or even) separated by a space.Example Input/Output 1:Input:710 21 20 33 98 66 29Output:21 20 33Example Input/Output 2:Input:511 21 30 99 52Output:30 99
Solution
Sure, here is a Python solution for the problem:
def find_elements(n, arr):
# Initialize an empty list to store the result
result = []
# Iterate over the array from the second element to the second last element
for i in range(1, n-1):
# Check if both the adjacent elements are either odd or even
if (arr[i-1] % 2 == arr[i+1] % 2):
# If yes, append the current element to the result list
result.append(arr[i])
# Print the result list
return result
n = int(input())
arr = list(map(int, input().split()))
print(*find_elements(n, arr))
This Python script works by iterating over the array and checking if the adjacent elements of the current element are both odd or both even. If they are, it adds the current element to the result list. Finally, it prints the elements of the result list.
Similar Questions
Write a program to print the odd values in the given rangeInput FormatTwo space separated integers - 'start' and 'end'Output FormatDisplay all the odd numbers between 'start' and 'end'Sample Input10 20Sample Output11 13 15 17 19
Complete the function count_odd_even() , which receives number of elements and array as input. Function should count number of even numbers and odd numbers in the array and print it.Input Format:No Need to read any input, as it is predefined.Output Format:Print the count of odd and even as: Odd = ODD_COUNTEven = EVEN_COUNTConstraints:1<=size<=10^9 1<=arr[ind]<=10^15Sample Input 1:1047 41 87 88 95 46 35 66 63 93Sample Output 1:Odd = 7Even = 3Sample Input 2:81 2 3 4 5 6 7 8Sample Output 2:Odd = 4Even = 4
The program must accept N integers as the input. The program must sort the N integers using selection sort and print all the iterations of the selection sorting process as the output.Boundary Condition(s):2 <= N <= 1001 <= Each integer value <= 1000Input Format:The first line contains N.The second line contains N integers separated by a space.Output Format:The lines containing all the stages of the selection sort.Example Input/Output 1:Input:512 6 15 9 10Output:6 12 15 9 106 9 15 12 106 9 10 12 156 9 10 12 15
Write a program to accept an integer N and print the sum of first N odd numbersInput Format:An integer range is given inputOutput Format:Print only calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:2Sample Output 1:4Sample Input 2:10Sample Output 2:100
Write a program to print only the even numbers till given NInput FormatA single integer 'N'Output FormatDisplay all the even numbers between 0 and NSample Input10Sample Output0 2 4 6 8 10
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.