GFG Question Link: https://www.geeksforgeeks.org/problems/k-th-element-of-two-sorted-array1317/1
Here is my code:
class Solution {
public:
int kthElement(int k, vector<int>& arr1, vector<int>& arr2) {
int i = 0;
int j = 0;
int count = 0;
int ans = 0;
while(i < arr1.size() && j < arr2.size()){
if(arr1[i] <= arr2[j]){
count++;
if(count == k){
return arr1[i];
}
i++;
}
else{
count++;
if(count == k){
return arr2[j];
}
j++;
}
}
while(i < arr1.size()){
count++;
if(count == k){
return arr1[i];
}
i++;
}
while(j < arr2.size()){
count++;
if(count == k){
return arr2[j];
}
j++;
}
}
};
Input:
10
4 5 6 6
4 4 5 6 6 7 8 9 11 12 13 13
Output:
7
I am not getting the desired output its giving 0 in both expected output and your output, but it should give output as 7.
I have dry run it many times but in dry run its working completely as expected.
Can anyone help me correct my code and tell me the changes made in this code so it can work.
Priyanka Soni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.