insigniya
BAN USER- 0of 0 votes
AnswersFind the number of ways of placing 3 balls in 3 buckets. buckets are of diff capacity.
- insigniya in India
bucket1 can hold 2 balls
bucket2 - 3 balls
bucket3 - 2 balls.
ex. 1 1 1 is valid
0 3 0 is valid| Report Duplicate | Flag | PURGE
Microsoft Software Engineer / Developer Algorithm
If I have understood the question clearly.. here is the code to convert IPv4 to number.
static uint Transform(string input)
{
string[] str = input.Split('.');
uint final = 0;
uint number = 0;
for (int i = 0; i < str.Length;i++ )
{
number = Convert.ToUInt32(str[i]);
number = number << ((str.Length - i -1) * 8);
final = final | number;
Console.WriteLine();
}
return final;
}
1. We can find the SUM of array, MAX and MIN all in a single pass of the array.
2. If the array contains consecutive numbers then they should be in Arithematic Progression.
3. Find the sum of A.P = n(MIN + MAX)/2.
4. If sum of AP == SUM of array then elements are consecutive otherwise not.
1. For each edge of rectangle1 try to find if the verteces of rectangle1 and rectangle2 are on the opposite sides.
2. If any such edge exists then the rectangles dont intersect.
Note : It wont work if rectangles are concentric. May be we'll have to handle that seperately.
2 = "abc"
3 = "def"
4 = "ghi"
5 = "jkl"
6 = "mno"
7 = "pqrs"
8 = "tuv"
9 = "wxyz"
void Encode(int a[], int length, int count, char *c)
{
static string str[] ={"0","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
if(count == length)
{
for(int i = 0 ; i < length ; i++)
cout<<c[i];
cout<<endl;
return;
}
for(int i = 0; i < str[a[count]].length() ; i++)
{
c[count] = str[a[count]][i];
Encode(a, length, count+1, c);
}
}
int CountNumberOfLeaves(Node *node)
{
if(node == NULL)
return 0;
if(node->left == NULL && node->right == NULL)
return 1;
int numberOfLeafNodes = 0;
numberOfLeafNodes = numberOfLeafNodes+CountNumberOfLeaves(node->left) + CountNumberOfLeaves(node->right);
return numberOfLeafNodes;
}
Here is the code :
int FindRotationIndex(int a[], int start, int end)
{
if(start > end || a[start] < a[end])
return -1;
int mid = (start+end)/2;
if(mid > start && a[mid] < a[mid - 1])
return mid-1;
else if(mid < end && a[mid] > a[mid+1])
return mid;
if(a[start] >= a[mid])
return FindRotationIndex(a, start, mid-1);
if(a[mid] >= a[end])
return FindRotationIndex(a, mid+1, end);
}
a variation of binary search can be used over here :
static int BinarySearch(int[] a, int start, int end, int target)
{
if (start > end)
return -1;
int mid = (start + end) / 2;
if (a[mid] == target) return mid;
if (a[start] < a[mid])
{
if (target > a[mid]) start = mid + 1;
else if (target > a[start]) end = mid - 1;
else start = mid + 1;
}
else
{
if (target < a[mid]) end = mid - 1;
else if (target <= a[end]) start = mid + 1;
else end = mid - 1;
}
return BinarySearch(a, start, end, target);
}
All subsets can be easily generated by generating all the binary numbers.
initially bitArray contains all zeros.
void PrintAllSubset(int bitArray[], char set[], int length, int pos)
{
if(pos == length)
{
for(int i = 0 ; i < length ; i++)
if(bitArray[i]!=0)
cout<<set[i]<<" ";
cout<<endl;
return;
}
for(int i = 0 ; i<= 1 ; i++)
{
bitArray[pos] = i;
PrintAllSubset(bitArray, set, length, pos+1);
}
}
Here is the actual code :
void SplitArrays(int a[], int length)
{
int *a1 = new int[length];
int *a2 = new int[length];
int i1 = 0;
int i2 = 0;
int sum1 = 0;
int sum2 = 0;
int i = 0;
while (i<length)
{
if(sum1<=sum2)
{
a1[i1] = a[i];
sum1 += a[i];
i1++;
}
else
{
a2[i2] = a[i];
sum2+=a[i];
i2++;
}
i++;
}
cout<<"first array : "<<sum1<<endl;
for (i = 0 ; i <i1 ; i++)
cout<<a1[i]<< " " ;
cout<<"\nSecond array : "<<sum2<<endl;
for (i = 0 ; i <i2 ; i++)
cout<<a2[i]<< " " ;
}
array : 78,43,34,32,31,24,12,6,2
output :
78,31,24 -- SUM = 133
43,34,32,12,6,2 ---SUM = 129
diff = 4
it was an algo question.
- insigniya October 02, 2011