kdirishkumar
BAN USERCode looks good but it does not handle, Integer max and min value cases,
Suppose in C# int max value is 2147483647, suppose if i give "2147483648" it fails.
It gives wrong result, if i give 39, it produces 'MA'. Supposed to be AM. If we reverse the string after its construction. It may give correct result.
- kdirishkumar June 01, 2014Here is C# Code
public static void VelocityJump()
{
bool[] just = { true, true, false, true, false, true, true, false, false, true, false };
int velocity = 1;
for (int i = 0; i < just.Length; i += velocity)
{
if (i == just.Length - 1 || i + velocity == just.Length - 1 || i + velocity + 1 == just.Length - 1)
{
Console.WriteLine("Rabbit can cross");
break;
}
if (!just[i] || i + (velocity - 1) >= just.Length || i + velocity >= just.Length)
{
Console.WriteLine("Rabbit can not jump");
break;
}
if ((i+velocity+1 < just.Length) && (just[i + velocity + 1]))
velocity++;
else if ((i+velocity<just.Length) && !just[i+velocity])
{
if (i != 0) velocity--;
}
}
}
Here is c# code but easily convert into C code too.
public static string SimultaneousCharRemove(char[] s)
{
int length = s.Length; // Can easily write finding out length of the string
int NoOfRemovedChar = 0;
for (int i = 0; i < length - NoOfRemovedChar; i++)
{
if (s[i] == '0' && s[i+1] == '0')
{
int count = 0;
for (int j = i + 1; s[j] == '0' && j < length; j++)
{
count++;NoOfRemovedChar++;//find the no of extra simultaneous occurances
}
for (int k = i + 1; k < length - NoOfRemovedChar; k++)
s[k] = s[k + count]; // shift the elements to left
i = i + count;
}
}
for (int i = length - 1; i >= length - NoOfRemovedChar; i--)
{
s[i] = '\0'; //Make null at String end for NoOfRemovedchar elements
}
return new string(s);
}
Please correct me if i understood question wrongly.
1. Find the sum of elements in given array
2. Find the index (Split Index) where sum of leftside elements reaches half of the sum of all elements.
3. So that we can split the array at above index which solves our problem
public static int GetSplitIndex(int[] GivenArray)
{
int SumOfTotal = Sum(GivenArray);
int checkPoint = 0, splitIndex = 0;
for (int i = 0; i < GivenArray.Length; i++)
{
checkPoint += GivenArray[i];
if (checkPoint > SumOfTotal / 2)
{
splitIndex = i - 1;
break;
}
}
return splitIndex;
}
Here is C# Code
public static void FindAdditionCombinations(int number)
{
RecursivePrintWays("1", number);
Console.WriteLine(number);
}
public static void RecursivePrintWays(string s, int number)
{
if (number == 2)
{
if (s.IndexOf('2') == -1)
{
s += " 1";
Console.WriteLine(s);
}
return;
}
for (int i = 1; i <= number/2; i++)
{
RecursivePrintWays(s + " " + i, number - i);
}
Console.WriteLine(s + " " + (number - 1));
}
Awesome
- kdirishkumar July 02, 2015