×

# CHFWV - Editorials

 1 PROBLEM LINK: 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 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. This question is marked "community wiki". asked 13 Sep, 23:43 1●2 accept rate: 0% 0★admin ♦♦ 19.6k●349●497●539
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×833