-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Repeat And Missing Number Array #13
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*There are certain problems which are asked in the interview to also check how you take care of overflows in your problem. | ||
This is one of those problems. | ||
Please take extra care to make sure that you are type-casting your ints to long properly and at all places. Try to verify if your solution works if number of elements is as large as 105 | ||
|
||
Food for thought : | ||
|
||
"Even though it might not be required in this problem, in some cases, you might be required to order the operations cleverly so that the numbers do not overflow. | ||
For example, if you need to calculate n! / k! where n! is factorial(n), one approach is to calculate factorial(n), factorial(k) and then divide them. | ||
Another approach is to only multiple numbers from k + 1 ... n to calculate the result. | ||
Obviously approach 1 is more susceptible to overflows." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrap all these comments to new lines so that it does not trails the editor view. |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File name must be |
||
You are given a read only array of n integers from 1 to n. | ||
|
||
Each integer appears exactly once except A which appears twice and B which is missing. | ||
|
||
Return A and B. | ||
|
||
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? | ||
|
||
Note that in your output A should precede B. | ||
|
||
Example: | ||
|
||
Input:[3 1 2 5 3] | ||
|
||
Output:[3, 4] | ||
|
||
A = 3, B = 4 | ||
|
||
https://www.interviewbit.com/problems/repeat-and-missing-number-array/ | ||
|
||
*/ | ||
|
||
|
||
vector<int> Solution::repeatedNumber(const vector<int> &A) { | ||
int n =A.size(); | ||
long long int sq1=0,sum1=0,sum2=0,sq2=0,x,a; | ||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Give 1 space before and after each assignment operator.
|
||
vector <int> v(2,0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
int i; | ||
for(i=0;i<n;i++){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
sum2=sum2+long(i+1); | ||
sq2=sq2+long(i+1)*long(i+1); | ||
sum1=sum1+long(A[i]); | ||
sq1=sq1+long(A[i])*long(A[i]); | ||
} | ||
|
||
x=(sq1-sq2)/(sum1-sum2); | ||
a=(x+(sum1-sum2))/2; | ||
v[0]=a; | ||
v[1]=x-a; | ||
Comment on lines
+41
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return v; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Begin problem statement on a newline after the block comment.