1.What is difference between the following 2 lines….
int temp = (int)(0×00);
int temp = (0×00int);
----------------------------
Answer:
the 2nd declaration will generate an error because of the word int inside the parenthesis. The 1st is correct because it cast the value to int that is the data type of the variable temp
--------------------------------
2.Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like.
------------------------
Answer:
(1)
Get the summation. If it is not equal to (2N + 1)/2, you
surely have duplicates.
---
problem: the formula given for summation shud be (n*(n+1))/2
but it will solve the problem only if we have one duplicate.
if we have many duplicates then we can still get the
correct summation
for ex:
1 2 2 5 5
it has two duplicates but still total is 5*(5+1)/2=15
(2)
When its given you are allowed to destroy the array..do
think in that direction..
Read the first element go to that position, read that
position and mark it zero.. keep doing so...
If at any place we find zero.. that means array contains
duplicate.
---
problem:
If you mark places with zero, you will lose the original
value in that place which, eventually, leads to errors...
Assume an array starting as: 1-5-5-...
After first step array would be: 1-0-5-...
You will get stuck at the second step since every element
that you marked with zero will lead to the position 0.
Converting to negative makes more sense...