Push_back(3) Challenge first ITGUY30

Question link: Challenge First

My Solution (Considered WA): Solution

The question is to rearrange the given array as:

  • arr[i] > arr[i+1] < arr[i+2] > arr[i+3] …
  • also arr[i] < arr[i+2] and arr[i+1] < arr[i+3]

The testcase and the solution given:

4 1 6 3        ->         3 1 6 4
4 5 1 6 3      ->         3 1 5 4 6

My solution to above testcases:

4 1 6 3        ->         4 1 6 3
4 5 1 6 3      ->         4 1 5 3 6

My solution is passing both the conditions asked in the question. But I am getting WA verdict.

Can someone help me regarding this…

@references

@sridattaks I don’t know the way you are solving but I can explain my solution.

  • First of all sort the array in increasing order and after that just print pairs in reverse order.

  • Suppose if array after sorting looks likes this:
    arr[0], arr[1], arr[2], arr[3], arr[4]

  • Then you just have to print the array in this order:
    arr[1], arr[0], arr[3], arr[2], arr[4]

Code:

void solve(){
    int n;
    cin >> n;
    vector<int> vec(n);
    for(auto &x : vec){
        cin >> x;
    }
    sort(vec.begin(),vec.end());
    int i = 0;
    while(i < n){
        if(i+1 < n){
            cout << vec[i+1] << " ";
        }
        if(i < n){
            cout << vec[i] << " ";
        }
        i += 2;
    }
    cout << '\n';
}
1 Like

@gkalyan Thanks for your time.

Yeah, you are right. And the setter also has given the solution in the same format as you said.

My solution is bit different, and I have used this format to give the solution:

first sort the array in increasing order.
if n is odd
    arr[n//2], arr[0], arr[n//2 +1], arr[1], arr[n//2 +2], arr[2], arr[n//2 +3], arr[3] . . .  , arr[n-1] 
else if n is even
    arr[n//2], arr[0], arr[n//2 +1], arr[1], arr[n//2 +2], arr[2], arr[n//2 +3], arr[3] . . . 

example testcase:

2
16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

My solution:

8 0 9 1 10 2 11 3 12 4 13 5 14 6 15 7
7 0 8 1 9 2 10 3 11 4 12 5 13 6 14

All the above said constraints are met as asked in question!!! But I am getting WA!!!

@aryan12 @vijju123
please have a look into it… I have explained the question and my logic in the comment above… I am not able to figure out the reason for WA verdict…

just sort array in ascending order and print odd index element first and even index element afterward

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

#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long int
#define list vector<ll>
#define pb push_back
#define llist vector<vector<ll>>
#define len size()
#define queue queue<ll>
#define pqueue priority_queue<ll>
#define mod 1000000007
#define endl '\n'

int main(int argc, char const *argv[])
{
    IOS
    int t;
    cin>>t;
    while (t--){
        int n;
        cin>>n;
        int arr[n];
        for (int i=0; i<n; i++) cin>>arr[i];
        sort(arr, arr+n);
        int index=1;
        while (index<n){
            cout<<arr[index]<<" "<<arr[index-1]<<" ";
            index+= 2;
        }
        if (n%2!=0) cout<<arr[n-1];
        cout<<endl;
    }

    return 0;
}
1 Like

The condition arr[i] < arr[i+3] fails in your second testcase.
arr[0] = 4 and arr[3] = 3. arr[0] is not < arr[0 + 3]

1 Like

@manandahiya Thanks a lot… I had skipped the third condition saying,
arr[i] < arr[i+3]
Thanks everyone for your time.