I am trying to do a simple coding challenge that requires me to do the following:
You are given n, return an array ans, composed in such way:
`ans = [n, n - 5, n - 10, ... , m, m + 5, ... , n - 5, n]`, where m stands for the first non-positive integer obtained by subtractions.
Try to solve it without any loop.
Example
For n = 25, the output should be
listWithoutLoop(n) = [25, 20, 15, 10, 5, 0, 5, 10, 15, 20, 25].
I have done this code:
int[] listWithoutLoop(int n)
{
List<int> test = new List<int>();
if (test.Count > 2 && test[test.Count - 1] == n)
return test.ToArray();
if (n <= 0)
{
test.Add(n + 5);
return listWithoutLoop(n + 5);
}
else
{
test.Add(n - 5);
return listWithoutLoop(n - 5);
}
}
But I keep getting a stack overflow when running it. Is recursion supported by c#? If so, how to prevent getting a stackoverflow exception when running it?
You must define the test list above the listWithoutLoop() method.
List<int> test = new List<int>();
int[] listWithoutLoop(int n)
{
....
}
To simplify, I splitted the function to add up and add down separately (it's always better to have simple and understandable code at all times)
static void Main()
{
int n = 20;
int interval = 5;
List<int> list = new List<int>();
AddDown(list, n, 0, interval);
AddUp(list, 0, n, interval);
int[] arrInt = list.ToArray();
}
static void AddDown(List<int> list, int currentNumber, int targetNumber, int interval)
{
if(currentNumber > targetNumber)
{
list.Add(currentNumber);
AddDown(list, currentNumber - interval, targetNumber, interval);
}
}
static void AddUp(List<int> list, int currentNumber, int targetNumber, int interval)
{
if (currentNumber <= targetNumber)
{
list.Add(currentNumber);
AddUp(list, currentNumber + interval, targetNumber, interval);
}
}
Related
I have a List<object> which can contain either an integer value or another List<object>.
What I need to do, is to sum the values in the array, depending on its depth(If depth is 0, multiply by 0, if 1 then by 1 ... etc.)
Example array: [5, 2, [7, -1], 3, [6, [-13, 8], 4]]
How it should be calculated: 5 + 2 + 2 * (7 - 1) + 3 + 2 * (6 + 3 * (-13 + 8) + 4) = 12
What I managed to get:
public class Program
{
public static List<object> TestCase1()
{
List<object> test = new List<object>(){
5,
2,
new List<object>(){
7, -1
},
3,
new List<object>(){
6,
new List<object>(){
-13, 8
},
4,
},
};
return test;
}
public static int ProductSum(List<object> array)
{
int depthCounter = 1;
int totalSum = 0;
int tempSum = 0;
for (int i = 0; i < array.Count; i++)
{
if (array[i] is IList<object>)
{
totalSum += tempSum * depthCounter;
tempSum = 0;
depthCounter++;
}
else
{
tempSum += (int)array[i];
}
}
return totalSum;
}
static void Main(string[] args)
{
Console.WriteLine("Result = " + ProductSum(TestCase1()));
}
}
The result from my code is
The problem that I have, is I don't see a way, that I could iterate through an array to calculate it ... Maybe there is a way to sort an array of objects in some way to simplify it?
The method should call itself ("recursive"), when it encounters a nested list. It should also take a depth parameter, keeping track of the depth. Every time you call the function recursively, you pass depth + 1 as the new depth. Whenever you count something, you multiply by depth.
public static int ProductSum(List<object> list) => ProductSum(list, 1);
public static int ProductSum(List<object> list, int depth)
{
var sum = 0;
foreach (var thing in list) {
if (thing is List<object> innerList) {
sum += ProductSum(innerList, depth + 1) * depth;
} else if (thing is int x) {
sum += x * depth;
}
}
return sum;
}
You should do it recusively:
public static int ProductSum(List<object> array, int depthCounter)
{
int totalSum = 0;
for (int i = 0; i < array.Count; i++)
{
if (array[i] is List<object>)
{
totalSum += ProductSum(array[i] as List<Object>, depthCounter + 1);
}
else
{
totalSum += (int)array[i] * depthCounter;
}
}
return totalSum;
}
You call it this way:
ProductSum(TestCase1(), 0);
But I don't get the number you calculated, though: If all depth 0 candidates are multiplied by 0, these are... 0! ;)
Maybe there are some rules for your application that I don't know, and that's the reason why the calculations differ, but in the code you see how recursions work.
This question already has answers here:
recursive string reverse function
(6 answers)
Closed 1 year ago.
I tried to reverse array with recursion method but somehow my code didn't show the right output.
static void Main(string[] args)
{
int[] arr = { 10, 20, 15, 40, 70, 60, 50, 80, 90, 25 };
ReverseArray(arr, arr.Length);
string output = PrintArray(arr);
Console.WriteLine(output);
}
static void ReverseArray(int[] V, int N)
{
int start = V.Length - N;
if (N > 1)
{
int temp = V[start];
V[start] = V[N - 1];
V[N - 1] = temp;
ReverseArray(V, N - 1);
}
}
static string PrintArray(int[] arr)
{
string temp = "";
foreach (int angka in arr)
{
temp += angka.ToString() + " ";
}
return temp;
}
I want the output showing this :
(25,90,80,50,60,70,40,15,20,10)
But my output is like this :
What's wrong with my code ?
You're reversing the array, but after you get past the midpoint you keep reversing. That means you're flipping values that have already been flipped.
Changing if (N > 1) to if (N > V.Length / 2) will fix the problem.
Hi use below logic it will work.
void reverse(int[] arr, int low, int high)
{
if (low < high)
{
int temp = arr[low];
arr[low] = arr[high];
arr[high] = temp;
reverse(arr, low + 1, high - 1);
}
}
[Image of the actual problem]
we have to choose the best item/items based on input3 which is items to be selected. the choice should be such that we don't take maximum item always. Instead we take items which are not having much difference.
input1: total items
input2: array of items
input3: items to be selected
Scenario 1:
input: 6, {44,55,605,100,154,190}, 1
output should be: {605}
input: 5, {15,85,32,31,2}, 2
output should be: {32,31}
as we increase the number of items to be selected, output should have more item in selected with minimum difference.
Below is the code i am trying, i am new to this please help:
i am stuck how to make this dynamic.
public static int[] Find(int totalItems, int[] values, int totalToBeSelected)
{
var i = values;
int[] results = new int[totalToBeSelected];
var resultList = new List<int>();
if (totalToBeSelected == 1)
{
resultList.Add(values.Max());
return resultList.ToArray();
}
Array.Sort(i);
var minmumDiff = (i[0] - i[1]) * -1;
for (int k = 1; k < i.Length; k++)
{
var differnce = i[k] - i[k - 1];
if (differnce < minmumDiff)
{
resultList.Add(i[k]);
resultList.Add(i[k - 1]);
minmumDiff = differnce;
}
}
return resultList.ToArray();
}
You can look at this function.
public static int[] Find(int totalItems, int[] values, int totalToBeSelected)
{
Array.Sort(values);
Array.Reverse(values); // We need any value greater than max items diff. Max array item (first item after the sort) enough for it.
int diff = values[0];
int indx = 0;
for (int i = 0; i < totalItems - totalToBeSelected +1; i++)
{
int temp_diff = values[i] - values[i + totalToBeSelected - 1]; // We are looking for any items group that max and min value difference is minimum
if (temp_diff < diff )
{
diff = temp_diff;
indx = i;
}
}
int[] results = new int[totalToBeSelected];
Array.Copy(values, indx, results, 0, totalToBeSelected);
return results;
}
Sample:
Find( 6, new int[] { 44, 55, 605, 100, 154, 190 }, 1 );
Out: { 605 }
Find( 5, new int[] { 15, 85, 32, 31, 2 }, 2 );
Out: { 32, 31 }
The conditions in the question are unclear, some assumptions had to be made.
class Program
{
static void Main(string[] args)
{
var items = new[] {12,14,22,24,6};//new[] { 15, 85, 32, 31, 2};//new[] { 44, 55, 605, 100, 154, 190 };
var totalItems = items.Count();
var numberOfItemsToSelect = 3;
var result = Find(totalItems, items, numberOfItemsToSelect);
PrintList(result);
Console.ReadLine();
}
static void PrintList(IEnumerable<int> scoreList)
{
foreach (var score in scoreList)
{
Console.Write(score);
Console.Write(" ");
}
}
public static int[] Find(int totalItems, int[]values, int totalTobeSelected)
{
var result = new List<int>();
if (totalTobeSelected <= 1)
{
result.Add(values.Max());
}
else if (totalTobeSelected == totalItems)
{
result.AddRange(values.OrderBy(i => i).ToList());
}
else
{
var mainSet = values.OrderBy(i => i).ToList();
var setDic = new Dictionary<int, IEnumerable<int>>();
for (int i = 0; (totalItems - i >= totalTobeSelected); i++)
{
var set = mainSet.GetRange(i, totalTobeSelected);
//Inside a set, we choose the difference between the first and the second number
// ex: set = {2, 4, 9} => diff = |2-4| = 2.
var diff = Math.Abs(set[0] - set[1]);
// given two sets with the same diff, we select the first one base on the sort order of the main set:
// ex: main set = {2,4,8,10}. Both {2,4} and {6,8} have a diff of 2 so we select {2,4}
if (setDic.ContainsKey(diff)) continue;
setDic.Add(diff, set);
}
if (setDic.Count > 0)
{
var minKey = setDic.Keys.Min();
result.AddRange(setDic[minKey]);
}
}
return result.ToArray();
}
}
Im having some issues with trying to update a nested for loop to use recursion instead. Is it possible to access the a,b and c variables from the earlier for loops when using recursion? Below is a simple example of what im trying to convert into a recursive call.
for(int a= 0; a < 10; a++)
{
for(int b = 0; b < 20; b++)
{
for(int c = 0; c < 10; c++)
{
int[] indexes = new int[3]{a,b,c}
collection.add(indexes);
}
}
}
EDIT: The solution needs to be able to be adjusted at runtime, such that a user can select how many levels are required.
Here's a recursive solution (using a functional programming style):
public static IEnumerable<IEnumerable<int>> GetCombinations(IEnumerable<int> limits)
{
if (limits.Any() == false)
{
// Base case.
yield return Enumerable.Empty<int>();
}
else
{
int first = limits.First();
IEnumerable<int> remaining = limits.Skip(1);
IEnumerable<IEnumerable<int>> tails = GetCombinations(remaining);
for (int i = 0; i < first; ++i)
foreach (IEnumerable<int> tail in tails)
yield return Yield(i).Concat(tail);
}
}
// Per http://stackoverflow.com/q/1577822
public static IEnumerable<T> Yield<T>(T item)
{
yield return item;
}
Sample use:
var sequences = GetCombinations(new [] { 5, 3, 2, 4 /* ... */ });
foreach (var sequence in sequences)
Console.WriteLine(string.Join(", ", sequence));
/* Output:
0, 0, 0, 0
0, 0, 0, 1
0, 0, 0, 2
0, 0, 0, 3
0, 0, 1, 0
0, 0, 1, 1
0, 0, 1, 2
0, 0, 1, 3
0, 1, 0, 0
0, 1, 0, 1
0, 1, 0, 2
... */
For OP's specific scenario (adding arrays to collection):
var sequences = GetCombinations(new [] { 10, 20, 10 });
collection.AddRange(sequences.Select(s => s.ToArray()));
Ok, try with this
static void AddToCollectionRecursive(
List<int[]> collection,
params int[] counts)
{
AddTo(collection, new List<int>(), counts, counts.Length - 1);
}
static void AddTo(
List<int[]> collection,
IEnumerable<int> value,
IEnumerable<int> counts,
int left)
{
for (var i = 0; i < counts.First(); i++)
{
var list = value.ToList();
list.Add(i);
if (left == 0)
{
collection.Add(list.ToArray());
}
else
{
AddTo(collection, list, counts.Skip(1), left - 1);
}
}
}
Usage is like this AddToCollectionRecursive(collection, 10, 20, 10);.
something like this will work:
public void CreateIndexes(int a, int b, int c, Collection collection)
{
if(c == 10) {b++; c = 0;}
if(b == 20) {a++; b = 0;}
if(a == 10) return;
int[] indexes = new int[3]{a,b,c}
collection.add(indexes);
c++;
CreateIndexes(a, b, c, collection);
}
Off the top of my head, i.e. not tested, something like this might work:
List<int[]> collection = new List<int[]>();
private void AddValues(int a, int b, int c)
{
collection.Add(new[] { a, b, c });
if (c < 10)
{
c++;
AddValues(a, b, c);
}
if (b < 20)
{
b++;
c = 0;
AddValues(a, b, c);
}
if (a < 10)
{
a++;
b = 0;
c = 0;
AddValues(a, b, c);
}
}
Start it by calling:
AddValues(0, 0, 0);
Well, i think that if u resolve this problem using recursion, it will consume more memory and other resources!
But there is my suggestion:
private void FunctionName(int a, int b, int c, List<int[]> list)
{
if (a<10)
{
if (b<20)
{
if (c<10)
{
list.Add(new[] { a, b, c });
c++;
FunctionName(a,b,c,list);
}
else
{
c=0;
b++;
FunctionName(a,b,c,list);
}
}
else
{
b=0;
a++;
FunctionName(a,b,c,list);
}
}
}
You call like this : FunctionName(0,0,0,list).
Hope it works! ^^
This solution takes an Action for the work to be done at the leafs:
void ForEachCombo(int from, int to, int nrLevels, Action<int[]> action)
{
int[] d = new int[nrLevels];
InnerFor(from, to, 0);
void InnerFor(int from, int to, int level)
{
if (level == nrLevels)
action(d);
else
for (d[level] = from; d[level] <= to - nrLevels + level + 1; d[level]++)
InnerFor(d[level] + 1, to, level + 1);
}
}
Use like this:
ForEachCombo(0, 9, 3, (d) =>
{
Console.WriteLine(string.Join(", ", d));
});
// Output
0, 1, 2
0, 1, 3
0, 1, 4
0, 1, 5
...
6, 7, 9
6, 8, 9
7, 8, 9
//
If you want to you can save a level of recursion by writing like this:
void ForEachCombo(int from, int to, int nrLevels, Action<int[]> action)
{
int[] d = new int[nrLevels];
InnerFor(from, to, 0);
void InnerFor(int from, int to, int level)
{
if (level == nrLevels - 1)
for (d[level] = from; d[level] <= to - nrLevels + level + 1; d[level]++)
action(d);
else
for (d[level] = from; d[level] <= to - nrLevels + level + 1; d[level]++)
InnerFor(d[level] + 1, to, level + 1);
}
}
I have an array of ints. I want to get the second highest number in that array. Is there an easy way to do this?
Try this (using LINQ):
int secondHighest = (from number in numbers
orderby number descending
select number).Skip(1).First();
You could sort the array and choose the item at the second index, but the following O(n) loop will be much faster.
int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
int largest = int.MinValue;
int second = int.MinValue;
foreach (int i in myArray)
{
if (i > largest)
{
second = largest;
largest = i;
}
else if (i > second)
second = i;
}
System.Console.WriteLine(second);
Yes, have 2 vars (first and second) passthrough the array and each time compair what you get with this two cells (always putting the highest on first and the 2nd highest on second)
with one pass you will get the 2nd higher on the second var.
You don't specify if you want to do this with the minimum complexity.
Assuming your array is unsorted, please see: How to find the kth largest element in an unsorted array of length n in O(n)?
To find Kth largest element in an unsorted array: Build a max heap in O(n). Now remove k elements from the heap; where each removal costs log(n) time to maintain the heap. Total time complexity = O(n + klogn)
To understand building Max heap in O(n) see Binary heap
max1=0;
max2=0;
for( int i=0; i < a.Length; i++)
{
if (arr[i]> max1)
{
max2=max1;
max1=arr[i];
}
else
{
if (a[i]!= max1) && ( a[i] > max2)
max2[i]=arr[i];
}
}
Getting the max number first, once the max is changed do a comparison against the second high number to see if it needs to swapped. The second if statement checks if the value is less than the max and is greater than the second highest value. Because of the short circuit, if the first condition fails then it exits the if and skips
static void Main(string[] args)
{
//int[] arr = new int[10] { 9, 4, 6, 2, 11, 100, 53, 23, 72, 81 };
int[] arr = { 1, 8, 4, 5, 12, 2, 5, 6, 7, 1, 90, 100, 56, 8, 34 };
int MaxNum = 0;
int SecNum = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] > MaxNum)
{
if (MaxNum > SecNum) { SecNum = MaxNum; }
MaxNum = arr[i];
}
if (arr[i] < MaxNum && arr[i] > SecNum)
{
SecNum = arr[i];
}
}
Console.WriteLine("Highest Num: {0}. Second Highest Num {1}.", MaxNum, SecNum);
Console.ReadLine();
}
int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
int num1=0, temp=0;
for (int i = 0; i < myArray.Length; i++)
{
if (myArray[i] >= num1)
{
num1 = myArray[i];
}
else if ((myArray[i] < num1) && (myArray[i] > temp))
{
temp = myArray[i];
}
}
Console.WriteLine("The Largest Number is: " + num1);
Console.WriteLine("The Second Highest Number is: " + temp);
int[] arr = { 1, 8, 4, 5, 12, 2, 5, 6, 7, 1, 90, 100, 56, 8, 34 };
int first, second;
// Assuming the array has at least one element:
first = second = arr[0];
for(int i = 1; i < arr.Length; ++i)
{
if (first < arr[i])
{
// 'first' now contains the 2nd largest number encountered thus far:
second = first;
first = arr[i];
}
}
MessageBox.Show(second.ToString());
static void Main(string[] args)
{
int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5,12,11,14 };
int num1 = 0, temp = 0;
for (int i = 0; i < myArray.Length; i++)
{
if (myArray[i] >= num1)
{
temp = num1;
num1 = myArray[i];
}
else if ((myArray[i] < num1) && (myArray[i] > temp))
{
temp = myArray[i];
}
}
Console.WriteLine("The Largest Number is: " + num1);
Console.WriteLine("The Second Highest Number is: " + temp);
Console.ReadKey();
}
There are two possibilities to find second highest number from an array.
1). Find second max number from an array.
int[] myArray = { 0, 2, 3, 8, 13};
int max = 0;
int second_max = 0;
foreach (int arr in myArray) {
if (arr > max)
{
second_max = max;
max = arr;
}
}
Console.WriteLine("First highest number is: "+max);
Console.WriteLine("Second highest number is: " + second_max);
2). Find second max number with the smallest complexity from an array.
int[] myArray = { 0, 2, 3, 13, 8};//smaller number is given after
larger number
int max = 0;
int second_max = 0;
foreach (int arr in myArray) {
if (arr > max)
{
second_max = max;
max = arr;
}
else if (arr > second_max)
{
second_max = arr;
}
}
Console.WriteLine("First highest number is: "+max);
Console.WriteLine("Second highest number is: " + second_max);