## Chronus Interview Question

Software Engineer / Developersimport java.util.*;

public class NewClass {

public static void main(String[] args)

{

int [] arr = { 1, 2, 3, 3, 5, 6, 7};

int redundant = 0 ;

int arrSum = 0;

int peg = 0;

for (int i = 0; i< arr.length; i++)

{

if (arr[i] != i +1)

{

redundant = redundant + arr[i];

peg = i+1 - arr[i];

}

arrSum = arrSum + arr[i];

}

int realSum = arrSum - redundant;

int actualSum = (arr.length *(arr.length +1))/2;

int diff = actualSum - realSum;

System.out.println(String.format("Actual Sum = %d:: Real Sum = %d",actualSum, realSum));

System.out.println(String.format("Missing Number = %d",diff));

}

}

#include<stdio.h>

main( )

{

int arr[]= {2,1,8,5,4},arr_size,sub[100],i,ans;

arr_size=(sizeof(arr))/4;

for(i=0; i<arr_size; i++)

sub[i]=0;

ans=fun(&arr, &sub,arr_size);

printf("%d",ans);

}

int fun(int arr[ ], int sub[ ],int arr_size)

{

int val,i;

for(i=0; i<arr_size; i++)

{

val=arr[i];

if(((sub[val-1])==0)&&(val<=arr_size))

{

sub[val-1]=val;

}

else

{

for(i=0; i<arr_size; i++)

{

if(sub[i]==0)

return(i+1);

}

}

}

}

you want to find first missing number.

- Rahul D August 04, 2011Negate the element on the index for current element. in the second loop check for positive number on an index& index is solution

for i=0;i<n;i++

a[a[i]-1] = -a[a[i]-1];

for i=0;i<n;++i

if(a[i] > 0)

printf("missing number is %d", i+1);