Find the missing element in the array

Post new topic   Reply to topic

View previous topic View next topic Go down

Find the missing element in the array

Post  Admin on Sun Aug 03, 2008 12:03 pm

Suppose we have an array A containing elements from 1 to n. An element is made zero and the resulting array is called B. Find the element that was made zero. Consider all the test cases and boundary conditions. Try to post multiple solutions. (This question was asked in Adobe written exam)

Admin
Admin

Posts: 39
Join date: 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

Re: Find the missing element in the array

Post  mnnit.rahul on Mon Aug 11, 2008 9:14 am

# include<iostream>
using namespace std;
main()
{int n,i,s1=0,s2=0;
cin>>n;
int a[n];
for(i=0;i<n;++i)
{cin>>a[i];
s2+=a[i];
}
s1=(n*(n+1))/2;
cout<<s1-s2;
system("pause");
}

mnnit.rahul

Posts: 16
Join date: 2008-08-10

View user profile

Back to top Go down

Using Part of Count Sort

Post  shivang on Mon Aug 11, 2008 1:19 pm

for i <--- 1 to n
{do C[i] <- 0
}
for j <--- 1 to n
{do C[A[j]] <--- C[A[j]] + 1
}
for i <--- 1 to n
{if C[i] == 0
then Break
}
outpu i

shivang

Posts: 22
Join date: 2008-08-11
Age: 20
Location: Tondon 174

View user profile

Back to top Go down

faster version

Post  ankurgutpa on Mon Aug 11, 2008 5:31 pm

missing_element=0
for i=1 to n{
missing_element^=(i^a[i]);
}
print missing element cheers

ankurgutpa

Posts: 44
Join date: 2008-08-10
Age: 22
Location: Tandon 72

View user profile

Back to top Go down

@Ankur

Post  shivang on Tue Aug 12, 2008 3:48 am

How come this is a faster version???

shivang

Posts: 22
Join date: 2008-08-11
Age: 20
Location: Tondon 174

View user profile

Back to top Go down

Re: Find the missing element in the array

Post  ankurgutpa on Tue Aug 12, 2008 7:51 am

faster not in terms of asymptotic notation but it will take less time as
bitwise operations are faster than addition/subtraction are faster than multiply/divide are faster than comparison. cheers

ankurgutpa

Posts: 44
Join date: 2008-08-10
Age: 22
Location: Tandon 72

View user profile

Back to top Go down

mnnit.rahul

Post  bhagalpur on Tue Aug 12, 2008 7:55 am

What if n(n+1)/2 exceeds the limit of integer ???

bhagalpur

Posts: 3
Join date: 2008-08-03

View user profile

Back to top Go down

shivang

Post  bhagalpur on Tue Aug 12, 2008 7:56 am

Re think of your solution and try to give algo of the solution

bhagalpur

Posts: 3
Join date: 2008-08-03

View user profile

Back to top Go down

Re: Find the missing element in the array

Post  Dee2306 on Tue Aug 12, 2008 4:03 pm

ankurgutpa wrote:missing_element=0
for i=1 to n{
missing_element^=(i^a[i]);
}
print missing element cheers


Smile Too Good.. Basketball

Dee2306

Posts: 7
Join date: 2008-08-12
Age: 21

View user profile

Back to top Go down

Re: Find the missing element in the array

Post  shivang on Tue Aug 12, 2008 7:02 pm

ya der was one problem.....the 0 wud hav created the problem.....
thanx.....
The algo tat'll work


for i <--- 0 to n
{do C[i] <- 0
}
for j <--- 1 to n
{do C[A[j]] <--- C[A[j]] + 1
}
for i <--- 0 to n
{if C[i] == 0
then Break
}
output i

shivang

Posts: 22
Join date: 2008-08-11
Age: 20
Location: Tondon 174

View user profile

Back to top Go down

Re: Find the missing element in the array

Post  ankurgutpa on Tue Aug 12, 2008 9:33 pm

question is same but try to do it by following constraint
you can only fetch jth bit of an element of array(assume in constant time) and nothing else
O(nlogn) solution exist for this problem but try to do it in O(n) ( as stated in CLRS 4-2) .

ankurgutpa

Posts: 44
Join date: 2008-08-10
Age: 22
Location: Tandon 72

View user profile

Back to top Go down

View previous topic View next topic Back to top


Permissions of this forum:
You cannot reply to topics in this forum