DSC-IIITL / GitGrind

8 stars 59 forks source link

Added solution for fix_bugs -> missing_numbers #39

Closed GaganMishra305 closed 11 months ago

GaganMishra305 commented 11 months ago

Solved the missing_numbers in fix_bugs #16

Illuminati9 commented 11 months ago

Hey @beforeIkillYou , Explain me about the PR you have made and provide some screenshots. Explain the code clearly about the PR you have made.

akhilender-bongirwar commented 11 months ago

@beforeIkillYou, make the PR description more clear by specifying the exact changes you have made and explaining why you made those particular changes. Additionally, clarify whether these changes resolve the issue, or if there are alternative approaches that you considered.

GaganMishra305 commented 11 months ago

Okay @Illuminati9 @akhilender-bongirwar @shishiro26 @PavanaSakethaRam this is my approach. for the given problem i have found the missing number in the given array by using hashing. I have gone through the entire array and saved the frequency of its elements by using a hash and then I have returned the first element with hash value (frequency = 0).

Step 1 Implementation of hash. Screenshot 2023-10-19 174111

Step 2 Finding and returning the first missing element that is the first element with hash value of 0 and if no number is missing from range 1 to length of array then we return -1. Screenshot 2023-10-19 174843

Step 3 Finally we print the first missing number and if its -1 then we print that no number is missing from the given range. Screenshot 2023-10-19 174542

Hope this clears my line of approach.

GaganMishra305 commented 11 months ago

Hey @PavanaSakethaRam @akhilender-bongirwar , sorry for the misinterpretation of the problem on my part. Here i have fixed the bugs with the following procedure-

Bugs in original code- Screenshot 2023-10-19 200343 It is not able to perform its operation when there is repetition of numbers within the array and it also returns the same number if there is repetition on last element or if there is no repetition at all. The main bug is due to the presumption that elements of an sorted array will be in coherence with the indices and the first non coherent element is the missing one. This presumption gets shattered when there is repetition of elements.

So in order to resolve the issue of not being able to handle repetition of number I have used an unordered set to keep track of existing numbers and in a case where there is repetition and the whole remaining array gets offsetted by the number of repeated occurences it can look up to the elements there are and then decide if the given index is off a missing element or not. Screenshot 2023-10-19 195622 My code uses an ordered set to resolve the aforementioned issue.