Problem: If ‘n’ is the positive number and ‘a’ is an array of integers of length n1 containing elements from 1 to n. Then find the missing number in ‘a’ in the range from 1 to n. Occurrence of each element is only once. i.e ‘a’ does not contain duplicates.
Example :
If n = 8, then array ‘a’ will have 7 elements in the range from 1 to 8. For example {1, 4, 5, 3, 7, 8, 6}. One number will be missing in ‘a’ (2 in this case). You have to find out that missing number.
Solution :
Step 1 : First we find out sum of ‘n’ numbers by using formula n*(n+1)/2.
Step 2 : Then we will find sum of all elements of array ‘a’.
Step 3 : Missing_Number = (Sum of 1 to ‘n’ numbers) – (Sum of elements of array ‘a’)
This solution is feasible for finding if any single number is missing from the array. What if multiple numbers are missing from our array. So how can we find? Thanks in advance Kindly help
priyanshi bhardwaj wrote:Ok wt if we remove this unique condition and reframe the question as
find the missing numbers within a range from 1 to n (inclusive)?
The simple, brute force approach would be to sort the array and then count through the numbers in a for loop. If the number at the index of the counter is not the same as the counter add it to your return list.
Zach
priyanshi bhardwaj wrote:thanks, I did the same. I have taken an array list now the problem is only how to convert an array list to an array? please help I haven't used ArrayList before and I have to submit this code in my lab exercise tomorrow, I just mean it's urgent. Thanks in advance.
I Mean the return type of my function is an array
I have attached the error i am facingpriyanshi bhardwaj wrote:
priyanshi bhardwaj wrote:thanks, I did the same. I have taken an array list now the problem is only how to convert an array list to an array? please help I haven't used ArrayList before and I have to submit this code in my lab exercise tomorrow, I just mean it's urgent. Thanks in advance.
I Mean the return type of my function is an array
For the others, you'll need to show your code.
[OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
 1
I would declare ms like this: ...then return ms. But you would have to change the return type of your method and modify how the class uses the method.
All things are lawful, but not all things are profitable.
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJIDEA
