Knowee
Questions
Features
Study Tools

Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the second lowest and second greatest numbers, respectively, separated by a space. For example: if arr contains [7, 7, 12, 98, 106] the output should be 12 98. The array will not be empty and will contain at least 2 numbers. It can get tricky if there's just two numbers!Once your function is working, take the final output string and combine it with your ChallengeToken, both in reverse order and separated by a colon.Your ChallengeToken: nsulxhvazb10ExamplesInput: new int[] {1, 42, 42, 180}Output: 42 42Final Output: 24 24:01bzavhxlusnInput: new int[] {4, 90}Output: 90 4Final Output: 4 09:01bzavhxlusn

Question

Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the second lowest and second greatest numbers, respectively, separated by a space. For example: if arr contains [7, 7, 12, 98, 106] the output should be 12 98. The array will not be empty and will contain at least 2 numbers. It can get tricky if there's just two numbers!Once your function is working, take the final output string and combine it with your ChallengeToken, both in reverse order and separated by a colon.Your ChallengeToken: nsulxhvazb10ExamplesInput: new int[] {1, 42, 42, 180}Output: 42 42Final Output: 24 24:01bzavhxlusnInput: new int[] {4, 90}Output: 90 4Final Output: 4 09:01bzavhxlusn

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution in C#:

  1. First, we need to sort the array in ascending order. We can use the Array.Sort() method for this.
Array.Sort(arr);
  1. After sorting, we need to find the second lowest and second greatest numbers. If the array has only two numbers, they will be the second lowest and second greatest numbers. If the array has more than two numbers, we need to skip duplicates to find the second lowest and second greatest numbers.
int secondLowest = arr[0];
int secondGreatest = arr[arr.Length - 1];

for (int i = 0; i < arr.Length; i++)
{
    if (arr[i] > secondLowest)
    {
        secondLowest = arr[i];
        break;
    }
}

for (int i = arr.Length - 1; i >= 0; i--)
{
    if (arr[i] < secondGreatest)
    {
        secondGreatest = arr[i];
        break;
    }
}
  1. Now, we have the second lowest and second greatest numbers. We need to return them as a string separated by a space.
string result = secondLowest + " " + secondGreatest;
  1. Finally, we need to reverse the result string and the ChallengeToken, and combine them with a colon.
string reversedResult = new string(result.Reverse().ToArray());
string reversedToken = new string("nsulxhvazb10".Reverse().ToArray());

string finalOutput = reversedResult + ":" + reversedToken;

return finalOutput;

So, the complete function would look like this:

public static string ArrayChallenge(int[] arr)
{
    Array.Sort(arr);

    int secondLowest = arr[0];
    int secondGreatest = arr[arr.Length - 1];

    for (int i = 0; i < arr.Length; i++)
    {
        if (arr[i] > secondLowest)
        {
            secondLowest = arr[i];
            break;
        }
    }

    for (int i = arr.Length - 1; i >= 0; i--)
    {
        if (arr[i] < secondGreatest)
        {
            secondGreatest = arr[i];
            break;
        }
    }

    string result = secondLowest + " " + secondGreatest;

    string reversedResult = new string(result.Reverse().ToArray());
    string reversedToken = new string("nsulxhvazb10".Reverse().ToArray());

    string finalOutput = reversedResult + ":" + reversedToken;

    return finalOutput;
}

This problem has been solved

Similar Questions

Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the second lowest and second greatest numbers, respectively, separated by a space. For example: if arr contains [7, 7, 12, 98, 106] the output should be 12 98. The array will not be empty and will contain at least 2 numbers. It can get tricky if there's just two numbers!Once your function is working, take the final output string and intersperse it character-by-character with your ChallengeToken.Your ChallengeToken: mpkc5dt91ExamplesInput: {1, 42, 42, 180}Output: 42 42Final Output: 4m2p k4c25dt91Input: {4, 90}Output: 90 4Final Output: 9m0p k4c5dt9

Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array (excluding the largest number) can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.

Array ChallengeHave the function ArrayChallenge(arr) take the array of numbers stored in arr which will contain 5 positive integers, the first two representing a range of numbers (a to b), the next 2 also representing another range of integers (c to d), and a final 5th element (x) which will also be a positive integer, and return the string true if both sets of ranges overlap by at least x numbers. For example: if arr is [4, 10, 2, 6, 3] then your program should return the string true. The first range of numbers are 4, 5, 6, 7, 8, 9, 10 and the second range of numbers are 2, 3, 4, 5, 6. The last element in the array is 3, and there are 3 numbers that overlap between both ranges: 4, 5, and 6. If both ranges do not overlap by at least x numbers, then your program should return the string false.ExamplesInput: {5,11,1,5,1}Output: trueInput: {1,8,2,4,4}Output: false

Array ChallengeHave the function ArrayChallenge(arr) read the array of non-negative integers stored in arr which will represent the heights of bars on a graph (where each bar width is 1), and determine the largest area underneath the entire bar graph. For example: if arr is [2, 1, 3, 4, 1] then this looks like the following bar graph:You can see in the above bar graph that the largest area underneath the graph is covered by the x's. The area of that space is equal to 6 because the entire width is 2 and the maximum height is 3, therefore 2 * 3 = 6. Your program should return 6. The array will always contain at least 1 element.ExamplesInput: {6, 3, 1, 4, 12, 4}Output: 12Input: {5, 6, 7, 4, 1}Output: 16

Suppose following numbers are sorted in an array A:32,51,26,84,63,21,11,54Using Binary search find the location of item  26,11 and 99.Answer text

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.