Created
September 28, 2016 00:23
-
-
Save bakkiraju/69dd7d3e7b5d268997ff24b244cd3103 to your computer and use it in GitHub Desktop.
Given two sorted arrays, return single sorted array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| int main(int argc, char *argv[]) { | |
| vector <int> A = {10,20,30,40,50,60,70,80}; | |
| int i = A.size()-1; | |
| vector <int> B = {11,22,33,44}; | |
| A.resize(A.size()+B.size()); | |
| int k = A.size() -1; | |
| int j=B.size()-1; | |
| while (i >=0 && j >= 0) { | |
| if (A[i] > B[j]) { | |
| A[k] = A[i]; | |
| k--; i--; | |
| } else { | |
| A[k] = B[j]; | |
| k--; j--; | |
| } | |
| } | |
| while (j >= 0) { | |
| A[k--] = B[j--]; | |
| } | |
| for (int i=0; i<A.size();i++) { | |
| cout << A[i] << " "; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment