Delivered instantly as a PDF via email. For Software Engineers & SDETs
150 programming interview questions and answers
5 proven approaches to crack algorithm questions
10 mistakes candidates make, and how to avoid them.
How to prepare for technical and behavioral questions without wasting your time!
"The BEST book for acing your interview. It helped me land my Microsoft job, and it was worth every penny!" - Ravi (Accepted at Microsoft, Amazon and Facebook)
30 Day Money Back Guarantee: Don't love the book? We'll give you your money back!More Info
Saturday, April 24, 2010
Given an array of size n. It contains numbers in the range 1 to n. Find the numbers which aren’t present.
int[] counterArray = new int[givenArray.Length + 1]; for (int iCounter = 0; iCounter < givenArray.Length; iCounter++) { counterArray[givenArray[iCounter]]++; }
for (int iCounter = 0; iCounter < counterArray.Length; iCounter++) { if (counterArray[iCounter] == 0 && iCounter != 0) { Console.WriteLine("The missing Number is: {0}", iCounter); Console.WriteLine("The number that is repeated: {0}", givenArray[iCounter-1]); } }
int[] counterArray = new int[givenArray.Length + 1];
ReplyDeletefor (int iCounter = 0; iCounter < givenArray.Length; iCounter++)
{
counterArray[givenArray[iCounter]]++;
}
for (int iCounter = 0; iCounter < counterArray.Length; iCounter++)
{
if (counterArray[iCounter] == 0 && iCounter != 0)
{
Console.WriteLine("The missing Number is: {0}", iCounter);
Console.WriteLine("The number that is repeated: {0}", givenArray[iCounter-1]);
}
}
do a quick sort in n* log(n) time.
ReplyDeleteloop through from 1 N looking for missing elements.