Techno Blender
Digitally Yours.

Check if Array can be sorted by swapping adjacent elements having odd sum

0 47


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given an array arr[], the task is to check if the array can be sorted using the given operation any number of times. In one operation, you can swap any two adjacent elements if their sum is odd.

Examples:

Input: arr[] = [1, 6, 31, 14]
Output: Yes
Explanation: Swap 31 and 14 (31 + 14 = 45 which is odd). The array will be [1, 6, 14, 31] which is sorted.

Input: arr[] = [4, 2]
Output: No
Explanation: Here no swap is possible. Hence the array can not be made sorted using the given operation.

Approach: To solve the problem follow the below idea:

Adjacent elements can only be swapped if they are of different parity. The given array can not be sorted if any element that is greater and of the same parity comes earlier in the array. So, If either the order of even elements or the order of odd elements is not non-decreasing, then it is impossible to sort the given array. We can made two separate arrays for storing even and odd elements and then can check either both of the arrays are in non-decreasing order or not separately.

Below are the steps for the above approach:

  • Initialize two arrays/vectors say, even[] and odd[] that store even and odd elements respectively.
  • Iterate the given array and check if arr[i] % 2 == 0, push the current element in the array even[], else push the current element in the array odd[].
  • Check if both the arrays are separately in non-decreasing order, return true, else return false.

Below is the code for the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

bool check(vector<int>& arr, int n)

{

  

    

    

    vector<int> even;

    vector<int> odd;

  

    for (int i = 0; i < arr.size(); i++) {

  

        

        

        if (arr[i] % 2 == 0) {

            even.push_back(arr[i]);

        }

  

        

        else {

            odd.push_back(arr[i]);

        }

    }

  

    

    

    

    for (int i = 1; i < even.size(); i++) {

        if (even[i - 1] > even[i]) {

            return false;

        }

    }

  

    

    

    

    for (int i = 1; i < odd.size(); i++) {

        if (odd[i - 1] > odd[i]) {

            return false;

        }

    }

  

    

    

    return true;

}

  

int main()

{

    vector<int> arr = { 1, 6, 31, 14 };

    int n = arr.size();

  

    

    bool flag = check(arr, n);

  

    if (flag == true)

        cout << "Yes" << endl;

    else

        cout << "No" << endl;

}

Time Complexity: O(N)
Auxiliary Space: O(N)


Improve Article

Save Article

Like Article

Improve Article

Save Article

Given an array arr[], the task is to check if the array can be sorted using the given operation any number of times. In one operation, you can swap any two adjacent elements if their sum is odd.

Examples:

Input: arr[] = [1, 6, 31, 14]
Output: Yes
Explanation: Swap 31 and 14 (31 + 14 = 45 which is odd). The array will be [1, 6, 14, 31] which is sorted.

Input: arr[] = [4, 2]
Output: No
Explanation: Here no swap is possible. Hence the array can not be made sorted using the given operation.

Approach: To solve the problem follow the below idea:

Adjacent elements can only be swapped if they are of different parity. The given array can not be sorted if any element that is greater and of the same parity comes earlier in the array. So, If either the order of even elements or the order of odd elements is not non-decreasing, then it is impossible to sort the given array. We can made two separate arrays for storing even and odd elements and then can check either both of the arrays are in non-decreasing order or not separately.

Below are the steps for the above approach:

  • Initialize two arrays/vectors say, even[] and odd[] that store even and odd elements respectively.
  • Iterate the given array and check if arr[i] % 2 == 0, push the current element in the array even[], else push the current element in the array odd[].
  • Check if both the arrays are separately in non-decreasing order, return true, else return false.

Below is the code for the above approach:

C++

#include <bits/stdc++.h>

using namespace std;

  

bool check(vector<int>& arr, int n)

{

  

    

    

    vector<int> even;

    vector<int> odd;

  

    for (int i = 0; i < arr.size(); i++) {

  

        

        

        if (arr[i] % 2 == 0) {

            even.push_back(arr[i]);

        }

  

        

        else {

            odd.push_back(arr[i]);

        }

    }

  

    

    

    

    for (int i = 1; i < even.size(); i++) {

        if (even[i - 1] > even[i]) {

            return false;

        }

    }

  

    

    

    

    for (int i = 1; i < odd.size(); i++) {

        if (odd[i - 1] > odd[i]) {

            return false;

        }

    }

  

    

    

    return true;

}

  

int main()

{

    vector<int> arr = { 1, 6, 31, 14 };

    int n = arr.size();

  

    

    bool flag = check(arr, n);

  

    if (flag == true)

        cout << "Yes" << endl;

    else

        cout << "No" << endl;

}

Time Complexity: O(N)
Auxiliary Space: O(N)

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment