ashu
BAN USER/* The logic here is first create scale of range lying from min year to max year from the
* given set of input where animals lived.
* Then loop through the scale and find out during each years how many animals lived
* and yes before that create list of key Value pair so that u can set key as the year
* and value stating the no of animals lived in that year and then get the max no of animals.
* Based on the max no of animals you got, again loop through the scale of given input
* duration and find out from which year to which year those many max no of animals
* lived and u ll get the answer.*/
static void Main(string[] args)
{
var list = new List<KeyValuePair<int, int>>();
List<int[]> data = new List<int[]>() { new int[] { 5, 11 }
, new int[] { 6, 18 }
, new int[] { 2, 5 }
, new int[] { 3, 12 } };
GetMaxAndMinYear(data);
for (int year = minYear; year <= maxYear; year++)
{
for (int i = 0; i < data.Count; i++)
{
if (data[i].Length > 0)
{
if (year >= data[i][0] && year <= data[i][data[i].Length - 1])
{
int index = isKeyExists(list, year);
if (index == -1)
list.Add(new KeyValuePair<int, int>(year, 1));
}
}
}
}
int maxNoOfAnimals = 0;
foreach (var element in list)
if (maxNoOfAnimals < element.Value) maxNoOfAnimals = element.Value;
bool isFirstYear = false;
foreach (var element in list)
{
if (!isFirstYear && element.Value == maxNoOfAnimals)
{
minYear = element.Key;
maxYear = element.Key;
isFirstYear = true;
}
if (isFirstYear && element.Value == maxNoOfAnimals)
maxYear = element.Key;
}
Console.WriteLine("the max no of animals lived in the years {0} to {1}", minYear, maxYear);
}
static int maxYear;
static int minYear;
public static void GetMaxAndMinYear(List<int[]> data)
{
for (int i = 0; i < data.Count; i++)
{
if (data[i].Length > 0)
{
for (int j = 0; j < data[i].Length; j++)
{
if (maxYear < data[i][j]) maxYear = data[i][j];
if (minYear == 0 || minYear > data[i][j]) minYear = data[i][j];
}
}
}
}
public static int isKeyExists(List<KeyValuePair<int, int>> data, int key)
{
int index = -1;
int value;
foreach(var element in data)
{
index++;
if (element.Key.Equals(key))
{
value=element.Value;
data.Remove(element);
data.Add(new KeyValuePair<int, int>(key, value + 1));
return 0;
}
}
return -1;
}
- ashu June 09, 2013static void Main(string[] args)
{
foreach (int data in arr)
removeNodes(rootNode, 20, null, null);
}
static int sumOfNodes = 0;
public static bool removeNodes(Node rootNode, int limit, Node parNode, string nodeType)
{
sumOfNodes += rootNode.data;
if (rootNode.leftNode == null && rootNode.rightNode == null)
{
if (limit > sumOfNodes)
{
if (nodeType.Equals("left", StringComparison.OrdinalIgnoreCase))
parNode.leftNode = null;
else
parNode.rightNode = null;
}
}
if (rootNode.leftNode != null)
removeNodes(rootNode.leftNode, limit, rootNode, "left");
if (rootNode.rightNode != null)
removeNodes(rootNode.rightNode, limit, rootNode, "right");
sumOfNodes -= rootNode.data;
return false;
}
- ashu June 06, 2013static void Main(string[] args)
{
#region tree's
//int[] arr = new int[] { 7, 4, 9, 3, 5, 8, 10 };
int[] arr = new int[] { 7, 6, 11, 26, 2, 4, 5 };
foreach (int data in arr) rootNode = addNode(rootNode, data);
searchLeafNode(rootNode);
foreach (int data in leafNodes)
{
count = 0;
getLevelOfEachLeafNode(rootNode, data);
}
if (res.Distinct().Count() == 1)
Console.WriteLine("\n\nthe validity of given bst is {0}", true);
else
Console.WriteLine("\n\nthe validity of given bst is {0}", false);
#endregion
}
static List<int> leafNodes = new List<int>();
static List<int> res = new List<int>();
static int count = 0;
public static void searchLeafNode(Node rootNode)
{
if (rootNode != null)
{
if (rootNode.leftNode == null && rootNode.rightNode == null)
leafNodes.Add(rootNode.data);
else
{
searchLeafNode(rootNode.leftNode);
searchLeafNode(rootNode.rightNode);
}
}
}
public static bool getLevelOfEachLeafNode(Node rootNode, int data)
{
count += 1;
if (rootNode == null)
return true;
else if (rootNode.data == data
&& (rootNode.leftNode == null && rootNode.rightNode == null))
return true;
else
{
if (data < rootNode.data)
{
if (getLevelOfEachLeafNode(rootNode.leftNode, data))
{
if (rootNode.leftNode.data == data)
res.Add(count - 1);
return true;
}
}
if (data > rootNode.data)
{
if (getLevelOfEachLeafNode(rootNode.rightNode, data))
{
if (rootNode.rightNode.data == data)
res.Add(count - 1);
return true;
}
}
}
count -= 1;
return false;
}
- ashu June 05, 2013
}
- ashu June 09, 2013