# CHFWV - Editorials

Practice

Contest

Author: rahul_ojha_07

DIFFICULTY:

BEGINNER

PREREQUISITES:

Knowledge of Array

PROBLEM:

Given an array we have to sort it in wave form then find the longest sequence of even numbers.

QUICK EXPLANATION:

First Sort the array of integers and then swap all the even indexed elements with its adjacent element.Then by traversing the array find the longest sequence of even integers

EXPLANATION:

Given the array of integers we’ll sort the array in ascending order,then for the wave like sequence we’ll swap all the even indexed element with its adjecent element.
Example:
2 4 3 6 1 9
After Sorting
1 2 3 4 6 9
Swaping even indexed
2 1 4 3 9 6

Now for finding the longest even sequence, we have a pseudo code here

count=0

max=0

for i=0 to n do

``````if array[i] is even then

count=count+1

else

if max<count then

max=count

count=0
``````

we have created two variables max and count and initiated it with 0, then while traversing throuth the we’re checking if the ith element is even or not if the element is even then we’ll increase the count by 1 else we’re checking if the the count if greater than max if yes then assign the max with the value of count else the max value will not change and then set the value of count as 0. by repeting this process for N element we can find the length of the longest even sequence in the form of max variable.

Author’s solution can be found here.

1 Like