Both Adjacent Elements - Odd or Even

 Coding Problem Keys 

 Both Adjacent Elements - Odd or Even 

 Problem Statement 

The program must accept N integers as the input. The program must print the integers that have both the adjacent integer values as odd or even. At least one integer will have both the adjacent values as odd or even among the N integers. (The first and last integers have only one integer adjacent to them. So consider only the single adjacent integer for them).

 Boundary Condition(S) 

3 <= N <=100
-10 <= Each integer value <= 10

 Input Format 

The First line contains the value of N.
The Second line contains the N integers separated by space(s).

 Output Format 

The First line contains the integers (which have both the adjacent element values as odd or even) separated by a space.

 Example Input/Output 1 

 Input 

7
10 21 20 33 98 66 29

 Output 

10 21 20 33 29

 Example Input/Output 2 

 Input 

5
-11 21 30 -99 52

 Output 

-11 30 -99 52

 Note: Max Execution Time Limit: 4000 millisecs 

 Solution: 

 Programming Language: C Language 

#include <stdio.h>
#include <stdlib.h>
int main() {
    int n; scanf("%d", &n);
    int arr[n];
    for(int i=0; i<n; i++){
        scanf("%d", &arr[i]);
    }
    printf("%d ",arr[0]);
    for(int i=1;i<n-1;i++){
        if((arr[i-1]%2==0 && arr[i+1]%2==0) || (arr[i-1]%2 && arr[i+1]%2)){
            printf("%d ",arr[i]);
        }
    }
    printf("%d", arr[n-1]);
}
//Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Cut Down & Plant - Trees

Shortest Path in Tree

Sum of Digits - Largest Integer

Unique Element

K'th Largest Number

Number of Profitable Days