gkr
BAN USER
public static int productOfMinMax(int[] min, int[] max)
{
if((min.Length==0 || min ==null) && (max==null || max.Length==0))
throw new Exception("Invalid Inputs provided");
int minimum = findMin(min);
int maximum = findMax(max);
return minimum * maximum;
}
public static int findMin(int[] arr)
{
int min = arr[0];
for(int i=1; i<arr.Length; i++)
{
if(arr[i]<min)
{
min = arr[i];
}
}
return min;
}
public static int findMax(int[] arr)
{
int max = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
public static int roundOff(int[] arr)
{
int number = getSum(arr);
return nextHighestPower(number);
}
private static int nextHighestPower(int number)
{
if ((number & number - 1) == 0)
{
return number;
}
else
{
int count = 0;
while (number != 0)
{
number = number >> 1;
count++;
}
return 1 << count;
}
}
public static int getSum(int[] arr)
{
int sum =0;
foreach (int i in arr)
{
sum +=i;
}
return sum;
}
public static string FindUniqueString(string[] str)
{
Dictionary<string, int> mydict = new Dictionary<string, int>();
for (int i = 0; i < str.Length; i++)
{
if (!mydict.ContainsKey(str[i]))
{
mydict.Add(str[i],1);
}
else
{
mydict[str[i]]++;
}
}
foreach (KeyValuePair<string,int> kvp in mydict)
{
if (kvp.Value == 1)
{
return kvp.Key;
}
}
return "";
}
- gkr September 12, 2017public static void SortByFrequency(int[] arr)
{
Dictionary<int, int> iDict = new Dictionary<int, int>();
Array.Sort(arr);
foreach (int i in arr)
{
if (!iDict.ContainsKey(i))
{
iDict.Add(i, 1);
}
else
{
iDict[i]++;
}
}
var sorted = iDict.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
foreach(KeyValuePair<int,int> keyValue in sorted)
{
for(int i =0; i<keyValue.Value; i++)
{
Console.WriteLine(keyValue.Key);
}
}
}
Note: assuming we have to count not just consecutive chars. we are counting all chars irrespective of whether they are consecutive.
public static string Compress(string str)
{
string compressed = "";
Dictionary<char, int> sDict = new Dictionary<char, int>();
foreach (char c in str)
{
if (!sDict.ContainsKey(c))
{
sDict.Add(c, 1);
}
else
{
sDict[c]++;
}
}
foreach (KeyValuePair<char, int> keyvalue in sDict)
{
compressed = compressed + keyvalue.Key + keyvalue.Value;
}
return compressed;
}
public static int[] multiplyExceptIndex(int[] arr)
{
int product = 1;
List<int> resultArray = new List<int>();
foreach(int i in arr)
{
if(i!=0)
{
product = product * i;
}
}
for(int i=0;i<arr.Length;i++)
{
if(arr[i]!=0)
{
resultArray.Add(product / arr[i]);
}
else
{
resultArray.Add(0);
}
}
return resultArray.ToArray();
}
public static int MaxSubArraySum(int[] arr)
{
int sumsofar = 0;
int maxend = 0;
if (arr.Length == 0 || arr == null)
throw new Exception("Invalid Input");
for(int i=0; i<arr.Length;i++)
{
maxend = maxend + arr[i];
if(maxend<0)
{
maxend = 0;
}
else if(sumsofar<maxend)
{
sumsofar = maxend;
}
}
return sumsofar;
}
public static List<int> FindDuplicates(int[] arr)
{
List<int> duplicates = new List<int>();
Dictionary<int, int> iDict = new Dictionary<int, int>();
foreach(int i in arr)
{
if(!iDict.ContainsKey(i))
{
iDict.Add(i, 1);
}
else
{
iDict[i]++;
}
}
foreach(KeyValuePair<int,int> keyvalue in iDict)
{
if(keyvalue.Value>1)
{
duplicates.Add(keyvalue.Key);
}
}
return duplicates;
}
public static char MaxRepeatedChar(string str)
{
char maxrepeated = ' ';
int max = 0;
Dictionary<char, int> sDict = new Dictionary<char, int>();
foreach(char c in str)
{
if(!sDict.ContainsKey(c))
{
sDict.Add(c, 1);
}
else
{
sDict[c]++;
}
}
foreach(KeyValuePair<char,int> keyvalue in sDict)
{
if(keyvalue.Value>max)
{
max = keyvalue.Value;
maxrepeated = keyvalue.Key;
}
}
return maxrepeated;
}
RepDimaOxygen15, Computer Scientist at Headrun Technologies Pvt Ltd
Hi! all my sweets friends My name is Dimo Oxygen! Now I study in Victoria University of Wellington New Zealand ...
RepEdwards IVF Surrogate is one of the best & most successful provider of surrogate services.We provide moral, emotional, ethical and ...
Repour goal is to help individuals companies and organizations of all kinds to communicate with their clients customer and employees ...
Repriverajenny935, i love my shop piano at xyz
Hello Everyone,My Name is Jenny Rivera .I have been a piano instructor for more than 25 years! I earned ...
RepLooking for the best child development center in Charlotte? Pal A Roos provide summer camp for children Charlotte. Our experienced ...
RepVirginialdelmonte, Animator at lostlovebackvashikaran
Have you lost your husband love? And you want to control your husband mind with vashikaran mantra. Guru ji is ...
Rephcr689121, Data Engineer at Autonomy Zantaz
Harrison is an electronic data processor experience in electronic data processor and Promotion. I enjoy providing health resources and how ...
- gkr November 30, 2017