EMC Interview Question
Software Engineer in TestsTeam: RSA
Country: India
Interview Type: Written Test
take min and max element of the second array and make array of make an array of (max-min) size u can also use bit array if max -min is ahuge number because of space complexity
then traverse the second array and according to each digit set bits on bit array after that traverse first array and in which, which bit not set that is a missing number ...
public static ArrayList<Integer> getMissingNumbers(int[] array1, int[] array2) {
HashSet<Integer> set2 = new HashSet<Integer>();
for(int num:array2){
set2.add(num);
}
ArrayList<Integer> missingNumbers = new ArrayList<Integer>();
for(int num:array1){
if (set2.add(num)) {
missingNumbers.add(num);
}
}
return missingNumbers;
}
Employing 2 loops:
#include <p30fxxxx.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/*Write a program to find the missing element in second array(Array2):
Array1:
5 15 2 20 30 40 8 1
Array2:
2 20 15 30 1 40 0 8 */
5 0 */
volatile int ara[8] = {5, 15, 2, 20, 30, 40, 8, 1};
volatile int arb[8] = {2, 20, 15, 30, 1, 40, 0, 8};
volatile int x;
int main (void)
{
int *ptr;
volatile int i, j, count;
for (i=0; i<8; i++)
{ count =0x0;
for (j=0; j<8; j++)
{
if (arb[i]== ara[j])
count++;
}
if (!count)
printf ("Missing element is %d\n",arb[i]);
}
x++;
return 0;
}
Add both arrays and substract them.
- DashDash June 02, 2012