From e262ad925808a2f112edd8115bba4b7704baa862 Mon Sep 17 00:00:00 2001 From: Tanmay <927tanmay@gmail.com> Date: Tue, 29 Oct 2019 12:54:15 +0530 Subject: [PATCH 1/2] Initial --- Arrays/Repeat_and_Missing_Number_Array.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Arrays/Repeat_and_Missing_Number_Array.cpp diff --git a/Arrays/Repeat_and_Missing_Number_Array.cpp b/Arrays/Repeat_and_Missing_Number_Array.cpp new file mode 100644 index 0000000..cf3707b --- /dev/null +++ b/Arrays/Repeat_and_Missing_Number_Array.cpp @@ -0,0 +1,18 @@ +vector Solution::repeatedNumber(const vector &A) { + int n =A.size(); + long long int sq1=0,sum1=0,sum2=0,sq2=0,x,a; + vector v(2,0); + int i; + for(i=0;i Date: Tue, 29 Oct 2019 19:26:09 +0530 Subject: [PATCH 2/2] second --- Arrays/Repeat_and_Missing_Number_Array.cpp | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Arrays/Repeat_and_Missing_Number_Array.cpp b/Arrays/Repeat_and_Missing_Number_Array.cpp index cf3707b..fd4351f 100644 --- a/Arrays/Repeat_and_Missing_Number_Array.cpp +++ b/Arrays/Repeat_and_Missing_Number_Array.cpp @@ -1,3 +1,37 @@ +/*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." + +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 Solution::repeatedNumber(const vector &A) { int n =A.size(); long long int sq1=0,sum1=0,sum2=0,sq2=0,x,a;