ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip       Ask Questions on ANYTHING, that arise in your Daily Life at     FORUM9.COM
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
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.
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
  Re: 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
guys..sort the array using quicksort..and check adjacent
elements..u can know there are duplicates or
not..complexity-O(nlogn)+n-1
 
Is This Answer Correct ?    5 Yes 4 No
Raghuram
 
  Re: 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
# 2
Get the summation. If it is not equal to (2N + 1)/2, you
surely have duplicates.
 
Is This Answer Correct ?    7 Yes 13 No
Purnank
 
 
 
  Re: 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
# 3
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
 
Is This Answer Correct ?    8 Yes 0 No
Monika
 
  Re: 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
# 4
You can use intermediate step for count sort.

Take a new array B of size N, initialize all values to 0.
and then 
for each i from 1 to N,
if((++B[A[i]]) > 1) - there are duplicates.

--- Time complexity O(n)
 
Is This Answer Correct ?    8 Yes 5 No
Ashutosh K
 
  Re: 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
# 5
#include<stdio.h>
void main()
{
int a[3],j,i,n;
clrscr();
printf("enter n");
scanf("%d",&n);
printf("enter els");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
printf("elements %d and %d are same",i+1,j+1);
}
}
}
 
Is This Answer Correct ?    1 Yes 2 No
Shri
 
  Re: 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
# 6
We need not sort this array and we need save space.
{2, 4, 5, 1, 3} m=2;
{2, 2, 5, 1, 3} m=4;
{2, 2, 5, 4, 3} m=1;
{1, 2, 5, 4, 3} m=2;
--------------------
{1, 2, 5, 4, 3} m=5;
{1, 2, 5, 4, 5} m=3;
{1, 2, 3, 4, 5} m=5;
--------------------
 
Is This Answer Correct ?    2 Yes 1 No
Ronstar Luan
 
  Re: 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
# 7
I have tried it with c#. It is of O(2n)~O(n)
 public bool tellIfDuplicate(params int[] nums)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] != i + 1)
                {
                    int temp = nums[i];
                    nums[i] = nums[nums[i] - 1];
                    nums[nums[i] - 1] = temp;
                }
            }
            for(int i=0;i<nums.Length;i++)
                if(nums[i]!=i+1)
                    return true;
            return false;
        }
 
Is This Answer Correct ?    1 Yes 6 No
Yegullelew
 
  Re: 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
# 8
The following algorithm works. It is O(n) and no extra 
storage required
        public static bool HaveDuplicate(int[] nums)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                int tmp = nums[i];
                if (tmp < 0)
                    tmp = -tmp;

                if (nums[tmp - 1] < 0)
                {
                    return true;
                }
                else
                {
                    nums[tmp - 1] = -nums[tmp - 1];
                }
            }
            return false;
        }
    }
 
Is This Answer Correct ?    18 Yes 1 No
Zhixingren
 
  Re: 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
# 9
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.
 
Is This Answer Correct ?    8 Yes 4 No
Miraj
 
  Re: 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
# 10
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...
 
Is This Answer Correct ?    2 Yes 4 No
Anyone
 
  Re: 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
# 11
hey scrubs, use a hash map

loop through array {
if(hash[x] == x] return true// is dupe
else hash.add(x,x);
}

max O(n)
 
Is This Answer Correct ?    3 Yes 1 No
Michael
 
  Re: 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
# 12
@Michael

Moron you are using extra space there.that is obvious.using
extra space many tough problems can be solved.without extra
space tell some better methods.
 
Is This Answer Correct ?    2 Yes 0 No
Somebody
 
  Re: 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
# 13
just add as many n(length of array) to an index as much it
has duplicates i.e. if array has size 7 and no. 5 is written
3 times then add 7 three times.so that after first loop
completion, you can just take division (a[5-1]/7 in this
case.so u got 3.)


//array is a[]
//len=length of array


for(i=0;i<len;i++)
{
b=a[i]%len;
a[b-1]=a[b-1]+7;
}
	//printing output
for(i=0;i<len;i++)
{
cout<<i+1<<" occurs: "<<(a[i]/7)<<endl;
}
 
Is This Answer Correct ?    0 Yes 0 No
Ashish
 
  Re: 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
# 14
srry.....few mistakes in above soln.....


just add as many n(length of array) to an index as much it
has duplicates i.e. if array has size 7 and no. 5 is written
3 times then add 7 three times.so that after first loop
completion, you can just take division (a[5-1]/7 in this
case.so u got 3.also original data at a[5-1] cant be loss
bcoz u can just tak (a[5-1]%len) nd u get the original data)


//array is a[]
//len=length of array


for(i=0;i<len;i++)
{
b=a[i]%len;
a[b-1]=a[b-1]+len;
}
	//printing output
for(i=0;i<len;i++)
{
cout<<i+1<<" occurs: "<<(a[i]/len)<<endl;
}
 
Is This Answer Correct ?    0 Yes 1 No
Ashish
 
  Re: 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
# 15
If there are no duplicates, Sum of numbers should be equal
to (N(N+1))/2.

Otherwise, duplicate is present.

No duplicate:
------------

Say N = 5

{1,2,3,4,5}

5(6)/2 = 15

1+2+3+4+5 = 15

Therefore no duplicate.

Duplicate:
---------

{1,2,3,4,1} 

Sum = 11

which is not equal to 15 (N(N+1)/2)

Therefore duplicate present.
 
Is This Answer Correct ?    0 Yes 0 No
Murugesh
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.  5
main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above HCL1
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);  1
What are segment and offset addresses? Infosys1
main( ) { int a[ ] = {10,20,30,40,50},j,*p; for(j=0; j<5; j++) { printf(“%d” ,*a); a++; } p = a; for(j=0; j<5; j++) { printf(“%d ” ,*p); p++; } }  1
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. Wipro2
main() { unsigned int i=65000; while(i++!=0); printf("%d",i); }  1
How to read a directory in a C program?  4
How will you print % character? a. printf(“\%”) b. printf(“\\%”) c. printf(“%%”) d. printf(“\%%”) HCL1
#define FALSE -1 #define TRUE 1 #define NULL 0 main() { if(NULL) puts("NULL"); else if(FALSE) puts("TRUE"); else puts("FALSE"); }  1
How we print the table of 3 using for loop in c programing?  3
main() { int i, j, *p; i = 25; j = 100; p = &i; // Address of i is assigned to pointer p printf("%f", i/(*p) ); // i is divided by pointer p } a. Runtime error. b. 1.00000 c. Compile error d. 0.00000 HCL1
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }  1
Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. Microsoft4
Finding a number multiplication of 8 with out using arithmetic operator NetApp8
print numbers till we want without using loops or condition statements like specifically(for,do while, while swiches, if etc)!  7
#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }  1
int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }  1
#if something == 0 int some=0; #endif main() { int thing = 0; printf("%d %d\n", some ,thing); }  1
main() { int i=5,j=10; i=i&=j&&10; printf("%d %d",i,j); }  1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com