
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sort Array by Swapping Adjacent Elements from Indices Containing '1' in a Given String
Sorting an array means ordering all the elements of the array in increasing order. Sorting an array by swapping adjacent elements means we can only swap elements that are adjacent to each other but we can swap adjacent elements any number of times. We will be given a binary string that will contain only two types of characters ?0' and ?1'. If any character is ?0' in the given string then we cannot swap the element present at that index of the array with adjacent elements.
Sample Examples
Input 1
Given array: {1, 4, 3, 2, 5, 7, 6}
Given string: 0111011
Output: Yes
Explanation ? In the given array 1 is at its position and we can swap elements from the index1 to index3. We can swap the index1 element with the next index and then swap index2 with index3 and then again index1 with index2 making 2,3,4 in the sorted form. For the last two elements, we can swap them and get the required sorted array.
Input 2
Given array: {1, 2, 4, 3, 5, 6}
Given string: 001011
Output: No
Explanation ? In the given array only 3 and 4 are not at the place where they are sorted and index3 cannot be swapped which means it's not possible to sort the array by swapping.
Naive Approach
In this approach, we will traverse over the array and find the sections which are sortable and will sort them. After sorting them will check for the complete array whether it is sorted or not. If sorted then we will print yes otherwise no.
Example
#include <bits/stdc++.h> using namespace std; // function to check if the given array is sorted or not bool checkSorted(int arr[], int n){ // traversing over the array for(int i=1; i<n; i++){ if(arr[i] < arr[i-1]){ // means the current element is smaller as compared to the previous return false; } } return true; } // function to sort the array bool isSorted(int arr[], int n, string str){ // traversing over the given array for(int i=0; i<n; i++){ if(str[i] == '0'){ // current index is not allowed to swap continue; } else if(arr[i] == i+1){ // if the current index element is at the required position // move to the next element continue; } else{ int j = i+1; // variable to traverse over the string while(j < n && str[j] == '1'){ j++; } // sorting the sortable elements sort(arr+i,arr+j); i = j-1; // updating the value of i } } return checkSorted(arr,n); } int main(){ int arr[] = {1, 4, 3, 2, 5, 7, 6}; // given array int n = 7; // size of array string s = "0111011"; // calling the function if(isSorted(arr,n,s)){ cout<<"Yes, it is possible to sort the given array by swapping the allowed indexes"; } else{ cout<<"No, it is not possible to sort the given array by swapping the allowed indexes"; } return 0; }
Output
Yes, it is possible to sort the given array by swapping the allowed indexes
Time and Space Complexity
The time complexity of the above code is O(N*log(N)), where N is the size of the array. Here log factor is due to the sorting of the sections using the sort function.
The space complexity of the above code is O(1), as we are not using any extra space here.
Efficient Approach
In this approach, we will only check if all the elements present in the sortable range are of the current range only rather than performing actual sorting. With the help of this approach, we can save time, and time complexity will become linear.
Example
#include <bits/stdc++.h> using namespace std; // function to find the solution bool isSorted(int arr[], int n, string str){ // traversing over the given array for(int i=0; i<n; i++){ if(arr[i] == i+1){ // if the current index element is at the required position // move to the next element continue; } else if(str[i] == '0'){ // the current index element is not at the required position // its not moveable also so return false return false; } else{ int j = i+1; // variable to traverse over the string while(j < n && str[j] == '1'){ j++; } // we have limit now check if all the elements in the sortable // limit is in the required final range for(int k = i; k<j; k++){ if(arr[k] <= i || arr[k] > j){ return false; } } i = j-1; // updating the value of i } } return true; } int main(){ int arr[] = {1, 4, 3, 2, 5, 7, 6}; // given array int n = 7; // size of array string s = "0111011"; // calling the function if(isSorted(arr,n,s)){ cout<<"Yes, it is possible to sort the given array by swapping the allowed indexes"; } else{ cout<<"No, it is not possible to sort the given array by swapping the allowed indexes"; } return 0; }
Output
Yes, it is possible to sort the given array by swapping the allowed indexes
Time and Space Complexity
The time complexity of the above code is O(N), where N is the size of the array.
The space complexity of the above code is O(1), as we are not using any extra space here.
Conclusion
In this tutorial, we have implemented a program to check if the given current array can be sorted or not if the sorting is done by swapping the adjacent elements only. Also, a binary string is given which indicates which index element can be sorted and which is not possible to sort. We have implemented two approaches one with the O(N*log(N)) time complexity and another with linear time complexity.