You are not logged in. Please login at www.codechef.com to post your questions!

×

CHFWV - Editorials

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<count then<br=""> 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

rahul_ojha_07's gravatar image

3★rahul_ojha_07
12
accept rate: 0%

edited 11 Oct, 17:14

admin's gravatar image

0★admin ♦♦
19.6k349497539

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • 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

question asked: 13 Sep, 23:43

question was seen: 77 times

last updated: 11 Oct, 17:14