Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Removing duplicates in a sorted array

Write the code to remove duplicates in a sorted array.

Answer:

Method 1[1]


int removeDuplicates(int a[], int array_size)
{
int i, j;

j = 0;


// Print old array...
printf("\n\nOLD : ");
for(i = 0; i < array_size; i++)
{
printf("[%d] ", a[i]);
}


// Remove the duplicates ...
for (i = 1; i < array_size; i++)
{
if (a[i] != a[j])
{
j++;
a[j] = a[i]; // Move it to the front
}
}

// The new array size..
array_size = (j + 1);


// Print new array...
printf("\n\nNEW : ");
for(i = 0; i< array_size; i++)
{
printf("[%d] ", a[i]);
}
printf("\n\n");



// Return the new size...
return(j + 1);
}



Method 2:
Use the algorithm unique.[2]

Strongly, weakly, Dynamic, static typed languages

Explain Strongly, weakly, Dynamic, static typed languages

Answer:[1] [2][3]

placing 0s and 1s in even and odd positions

you are given an array of integers containing only 0s and 1s. you have to place all the 0s in even position and 1s in odd position and if suppose no if 0s excedd no. of 1s or vice versa then keep them untouched. Do that in one pass without extra memory.
Ex:
input array: {0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 }
output array: {0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 }

n similar elements in an array

Given an array of 2n elements of which n elements are same and the remaining n elements are all different. Write a C program to find out the value which is present n times in the array.
(In linear time, constant extra space)

Array subset with sum closest to given number

Given an array, Design an algorithm to find the sub vector with the sum closest to zero.
extenstion: What if we wished to find the sub vector with the sum closest to a given real number t?

Find the repeating number

You are given a sequence of numbers from 1 to n-1 with one of the numbers repeating only once. (example: 1 2 3 3 4 5). how can you find the repeating number? what if i give you the constraint that you can't use a dynamic amount of memory (i.e. the amount of memory you use can't be related to n)?
what if there are two repeating numbers (and the same memory constraint?)

Majority Element of an Array

A majority element in an array A, of size N is an element that appears more than N/2 times (and hence there is atmost one such element)

Write a function which takes an array and emits the majority element (if it exists), otherwise prints NONE as follows:

I/P : 3 3 4 2 4 4 2 4 4
O/P : 4

I/P : 3 3 4 2 4 4 2 4
O/P : NONE

Array Rotation

Rotate an n-element vector left by i positions in time proportional to n with just a dozen bytes of extra space. For instance, with n=8 and i=3, the vector abcdefgh is rotated to defghabc

unique elements in an array

Implement an algorithm to take an array and return one with only unique elements in it.

Multiplication of numbers

There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n).