C
C#morry329#

✅ Comprehension questions (LeetCode Binary Search)

Like the title says it's about this puzzle https://leetcode.com/problems/binary-search/description/ I am analysing a couple of solutions submitted on the site One of the things that caught my attention is as follows: in an iterative solution there is no return statement in the if-block like this:
while(l<=r){
int m=l+(r-l)/2;
if(nums[m]==target)
return m; //returns only here
else if(nums[m]>target)
r=m-1; //no return statement in this block
else
l=m+1; //here no return either. why??
}
while(l<=r){
int m=l+(r-l)/2;
if(nums[m]==target)
return m; //returns only here
else if(nums[m]>target)
r=m-1; //no return statement in this block
else
l=m+1; //here no return either. why??
}
` Any recursive solution I saw does return the recursion method however:
if (min > max)
{
return -1;
}
else
{
int mid = (min + max) / 2;
if (target == inputArray[mid])
{
return mid;
}
else if (target < inputArray[mid])
{
return BinarySearchRecursive(inputArray, target, min, mid - 1); //returns the recursive method. why?
}
else
{
return BinarySearchRecursive(inputArray, target, mid + 1, max); //here also. why?
}
}
if (min > max)
{
return -1;
}
else
{
int mid = (min + max) / 2;
if (target == inputArray[mid])
{
return mid;
}
else if (target < inputArray[mid])
{
return BinarySearchRecursive(inputArray, target, min, mid - 1); //returns the recursive method. why?
}
else
{
return BinarySearchRecursive(inputArray, target, mid + 1, max); //here also. why?
}
}
` Could anyone kindly tell me why it is like this?
LeetCode
Binary Search - LeetCode
Can you solve this real interview question? Binary Search - Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.   Example 1: Input: num...
A
Angius397d ago
A recursive method has to use its own return value, so it has to return something
M
morry329#397d ago
Ok I got it 🙂 And why does an iterative method have to return nothing ? You see, I have some gaps in understanding this 😅
A
Angius397d ago
Well, it might need to return something at the end But it could also Console.Write() the result Or use an out parameter, or a ref, or any number of other things
A
Anton397d ago
it returns the value at the current index after the loop, supposedly ah no, it returns in the loop see the second statement in the loop if it doesn't return, it loops again and again, until it does if you returned later too, the loop would've been pointless
M
morry329#396d ago
Ahh I got it! Thanks for the clarification @AntonC @Angius
Want results from more Discord servers?
Add your server
More Posts
❔ How to create a cubic function calculator that also calculates the minimum and maximum value```c Console.WriteLine("Please enter a, b, c, d for the cubic function ax^3+bx^2+cx+d"); ❔ how to listen to the output of a virtual audio device on windows using C#?how to listen to the output of a virtual audio device on windows using C# to save the raw audio stre❔ How to wait for process to completei want to make program opens async but instead of giving 'hard coded' delays but i don't know how to❔ Problem with including C DLL in a C# console app project.So I have this C DLL imported into my console app (.NET 5). I simply cannot get to call it correctly✅ Win Form Event Handlers questionHi, I was just wondering if there is an event handler when the win form is idle that I can then run ❔ Problem with Dictionary<ulong, Image> (Strange work of memory access)Concept of my system: A system that draws a picture with stickers, each of the stickers can be moved❔ How do I refactor this?I am working with a grid of tiles, each tile has an int, which is made up of 4 bytes which represent❔ [AvaloniaUI] Visibility of item in ListView based on conditionAn Avalonia UI application is used to manage an evidence of items. It allows the user to add items. ✅ How to structure this login/signup page layoutSo I have this mockup of the layout I want for a login/signup page in my Blazor WASM app. Pretty sta❔ I am need of help before I give up and run my code on a server instead of a serverless solution.I have create an azure function locally and i've used the selenuim webdriver package for taking scre✅ .Net Core 6 Asymmetric Encryption with custom public and private keysHello all! How can i use the `Asymmetric Encryption` in .Net 6 but choosing/importing private and pu❔ No Design in C#Instances of this bug (1) 1. View Call Stack at System.Runtime.InteropServices.Marshal.ThrowExce❔ having issues on a reloading script on unity to reload weaponsthe code is used in a youtube video and i have pretty much copied to to get it to work but it doesnt❔ help with an exerciseI've encountered a weird problem that idk how to fix. Say we've got a string "31131123521" how do i ❔ Accessing HTTP Context at DbCommandInterceptor [.NET 7]Hi! I'm having some issues trying to access the HTTP Context at my DbCommand Interceptor class. What❔ XMLAttributeCollection -> Dictionary with LINQ?It's a confusing class. It only allows turning it into a Queryable, but I have no experience with th❔ MS SQL Reporting Server URL Being RedirectedWe have an application from a vendor that was written in C# and we recently upgraded the MS SQL data❔ dataset memory leakI found memory leak and I don't understand how to fix it Test case: ```cpp public class MemoryLeaksT❔ WebAPI .NET 7.0 - Encrypt connectionstring in appsettings.jsonguys, I'm writing some WebAPI using .NET 7.0, and I'd like to secure the connectionstring with encry❔ how to read strings correctly with System.Data.SQLitei m trying read turkish characters from database but i can't see correctly