Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
using namespace std;

class Solution{
public:
    int binarysearch(int arr[], int n, int k){
        // code here
        int start=0;
        int end=n-1;
        
        int mid=start+(end-start/2);
        
        while(start<=end)
        {
            if(arr[mid]==k)
            {
                return mid;
            }
            
            else if(k<arr[mid])
            {
                end=mid-1;
            }
            
            else
            {
                start=mid+1;
            }
        }
        
        mid=start+(end-start/2);
    }
    
};


What I have tried:

little bit i know is it can cause by if else statements but im not sure about it so how can i resolve this error.
Posted
Updated 28-Mar-22 20:13pm
v3
Comments
Patrice T 29-Mar-22 0:30am    
your code look corrupted in question.
Paste the code again between the <pre> tag to get it correct.
Use Improve question to update your question.

As a start, replace every occurrence of
Quote:
mid=start+(end-start/2);

with
C++
mid = start + (end - start) / 2;

That is
mid = (start + end) / 2;
(may overflow)

You may have a look at an existing implementation, for instance: Binary Search - GeeksforGeeks[^].
 
Share this answer
 
v2
Comments
Sanjana Alam 29-Mar-22 2:15am    
@CPallini thank you for ans but it is updated mid formula used to overcome overflow condition and i want to solve with that formula only, so do you have any other solution?
CPallini 29-Mar-22 2:47am    
I've updated my solution.
Quote:
getting runtime error.

And you plan to share with us ?
Please exact error message and position.

What is the return value if searched value is not found in array ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Sanjana Alam 29-Mar-22 2:19am    
Im not getting output at all and The exact error is

Runtime Error
Time Limit Exceeded


Your program took more time than expected.Time Limit Exceeded
Expected Time Limit 0.00sec
Hint : Please optimize your code and submit again.
Patrice T 29-Mar-22 2:47am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900