The Lounge is rated PG. If you're about to post something you wouldn't want your
kid sister to read then don't post it. No flame wars, no abusive conduct, no programming
questions and please don't post ads.
Looking up some WPF stuff for an interview Monday (thank you Abhishek Sur) and I have found that I have done the usual generally think something is hard, avoid it, need to do it, learn in a rush.
It doesn't seem to be that hard...fatal thing to say I know.
It seems that you can do some stuff in a different style, mostly...
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
I don't know, about 10 minutes. And yes, I included the comments in my "solution:"
staticint maxLength(int a, int k)
// The example, subarrays of [1,2,3] is wrong!!!// It is missing subarray of 3!!!// As usual, these tests are wrong!!!// Anyways, this is a fairly inefficient algorithm as it gets all the subarrays first,// and the max length, if == a.Length, could stop the search early, and could test if sum > k// as the subarrays are parsed, and ignore subarrays whose length is less than the working max.
List<int> subArrays = GetSubArrays(a);
int max = 0;
for (int n = 0; n < subArrays.Count; n++)
if (subArrays[n].Sum() <= k)
if (subArrays[n].Length > max)
max = subArrays[n].Length;
static List<int> GetSubArrays(int a)
int l = 0; // Start with length of 1 (0 based indexing!)
List<int> subarrays = new List<int>();
// Iterate, starting with a subarray of length 1.while (l < a.Length)
int idx = 0;
// Get the subarray components for the given length lwhile (idx + l < a.Length)
int sub = newint[l+1];
for (int i = 0; i <= l; i++)
sub[i] = a[idx + i];
++l; // next we get subarrays of length 2, then length 3, etc.
Recently I did a similar coding challenge for one of the company and I was rejected because I used too many If else statement according to them. Test was asking solution for such a trivial things you don't even bother about such things. One of the solution I provided was to find most common ints from array of ints. I guess I could have put first if else together.