Skip to main content

Wave Array | Convert an array to a wave array

 You have given a sorted array a[ ] of distinct integers and you have to sort the array in a wave-like pattern. It will be arranged like a[0]>=a[1]<=a[2]>=a[3]<=a[4]... This question has been asked in Google, Microsoft, Adobe, Amazon, Goldman Sachs, Paytm, and FactSet.

Input:
n=5
a[ ] = {1, 2, 3, 4, 5}
Output: 2 1 4 3 5

So how can we solve this problem?

If our array is already sorted then we have to just swap the array at the alternate elements.
Like for a[ ] = {1, 2, 3, 4, 5}
Loop1: i=0, a[ ] = {2, 1, 3, 4, 5}
Loop2: i=2, a[ ] = {2, 1, 4, 3, 5}
for Loop 3: i=4 which is not less than n-1 i.e. 4.
So the loop ends here.

The array generated is forming a wave pattern: {2, 1, 4, 3, 5}.


Cpp code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int i = 0; i < n - 1; i += 2)
    {
        swap(a[i], a[i + 1]);
    }
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
    cout << endl;
    return 0;
}

Please ask your queries in the comment section.

Comments

Popular posts from this blog

Reddit raises $250 million

 Reddit has raised $250 Million in a new funding round. This is the series E round of financing for Reddit. Reddit Reddit has become more popular and gained more public attention after the GameStop stock incident. The subreddit r/WallStreetBets played a big role in the rise of the value of GameStop stock.  Reddit has completed 16 years and it has raised around $800 million to date. Reddit has raised $300 million in February 2019 in series D round from Tencent. Reddit has said that this financing is from existing and new investors.  Reddit has posted announcement through a blog post which you can read here . They have also given insights about the future plan as they have said, We decided that now was the right opportunity to make strategic investments in Reddit including video, advertising, consumer  products, and expanding into international markets. Reddit Super Bowl Ad Reddit also ran a 5-second Super Bowl ad consisting of a single static image that looked like a ...

Parallel Computer Structeres

Parallel computers are those computers which emphasize parallel processing and that's very much obvious. Parallel computers are divided into 3 architectural configurations: Pipeline computers (do overlapped computations to use temporal parallelism)  Array processors (use multiple synchronized ALUs to do spatial parallelism) Multiprocessor system (asynchronous parallelism through interactive processors with shared resources) Instruction Fetch consists of four major steps: Instruction Fetch(IF) from the main memory; Instruction Decoding(ID) identifying the operation to be performed; Operand Fetch(OF); Execution(EX) of decoded arithmetic logic operation.  In a pipelined computer these steps are executed in overlapped function while in nonpipelined first these 4 steps must be completed then next instruction will be performed.  A k-stage linear pipeline processor could be almost k times faster. speedup = Nonpipeline processor/pipeline processor        ...

3 hacks to wake up early in the morning

So this is what everyone says that we have to wake up early in the morning. Yes, it's good to wake up early and this could be the best decision you will take in your life.   I have read this somewhere that " If you want to change your life, do nothing just start waking up early in the morning and things will start happening ". So yes waking up early in the morning is going to help you a lot and it will surely help in changing your life. Now the question that arises is how we can wake up early in the morning? We all want to wake up early at 5 or 6 Am in the morning but when the clock hits 5 and your alarm makes the sound, your mind says just 5 minutes more and these 5 minutes end up at 1 hour.  So the question is how can we make it to wake up early in the morning. So in this article, I'm sharing 3 hacks that can surely help you in achieving your goal of waking up early in the morning.  The secret of waking up early doesn't lie in the process of waking up early in ...