- Joined
- Aug 21, 2022
- Messages
- 1
- Reaction score
- 0
code studio Problem :
the code i submitted is:
int pivot(vector<int>& arr, int n){
int s = 0;
int e = n-1;
int mid = s + (e-s)/2;
while(s<e){
if(arr[mid] >= arr[0]){
s = mid + 1;
}
else{
e = mid;
}
mid = s + (e-s)/2;
}
return s;
}
int search(vector<int>& arr, int k, int s, int e){
while(s<=e){
int mid = s + ((e-s)/2);
if(k == arr[mid]){
return mid;
}
else if(k>arr[mid]){
s=mid+1;
}
else if(k<arr[mid]){
e = mid-1;
}
}
return -1;
}
int findPosition(vector<int>& arr, int n, int k)
{
// Write your code here.
// Return the position of K in ARR else return -1.
int p = pivot(arr,n);
int s;
if(k>=arr[p] && k<=arr[n-1]){
s = search(arr,k,p,n-1);
}
else{
s = search(arr,k,0,p-1);
}
return s;
}
//condition applied in findposition function here is
if(k>=arr[p] && k<=arr[n-1]){
s = search(arr,k,p,n-1);
}
else{
s = search(arr,k,0,p-1);
}
but before it when i wrote code in vscode i used the condition
if(k == arr[p]){
s = p;
}
else if(k>arr[0]){
s = search(arr,n,k,0,p-1);
}
else if(k<arr[0]){
s = search(arr,n,k,p+1,n-1);
}
this condition was properly working there for every case i checked even the sample cases of this problem were working
but here in code studio this was not showing correct ans with this condition
can some one explain why
Search In Rotated Sorted Array
the code i submitted is:
int pivot(vector<int>& arr, int n){
int s = 0;
int e = n-1;
int mid = s + (e-s)/2;
while(s<e){
if(arr[mid] >= arr[0]){
s = mid + 1;
}
else{
e = mid;
}
mid = s + (e-s)/2;
}
return s;
}
int search(vector<int>& arr, int k, int s, int e){
while(s<=e){
int mid = s + ((e-s)/2);
if(k == arr[mid]){
return mid;
}
else if(k>arr[mid]){
s=mid+1;
}
else if(k<arr[mid]){
e = mid-1;
}
}
return -1;
}
int findPosition(vector<int>& arr, int n, int k)
{
// Write your code here.
// Return the position of K in ARR else return -1.
int p = pivot(arr,n);
int s;
if(k>=arr[p] && k<=arr[n-1]){
s = search(arr,k,p,n-1);
}
else{
s = search(arr,k,0,p-1);
}
return s;
}
//condition applied in findposition function here is
if(k>=arr[p] && k<=arr[n-1]){
s = search(arr,k,p,n-1);
}
else{
s = search(arr,k,0,p-1);
}
but before it when i wrote code in vscode i used the condition
if(k == arr[p]){
s = p;
}
else if(k>arr[0]){
s = search(arr,n,k,0,p-1);
}
else if(k<arr[0]){
s = search(arr,n,k,p+1,n-1);
}
this condition was properly working there for every case i checked even the sample cases of this problem were working
but here in code studio this was not showing correct ans with this condition
can some one explain why