w3resource

C#: Find the pair of adjacent elements that has the highest product of an given array of integers

C# Sharp Basic: Exercise-57 with Solution

Max Product of Adjacent Integers

Write a C# program to find the pair of adjacent elements that has the highest product of an array of integers.

Sample Solution:

C# Sharp Code:

using System;

public class Example
{
    // Function to find the maximum product of adjacent elements in an array
    public static int adjacent_Elements_Product(int[] input_Array)
    {
        // Initialize the maximum product with the product of the first two elements in the array
        int max = input_Array[0] * input_Array[1];

        // Loop through the array to find the maximum product of adjacent elements
        for (int x = 1; x <= input_Array.Length - 2; x++)
        {
            // Update the max variable with the maximum of the current max and the product of the current and next elements
            max = Math.Max(max, input_Array[x] * input_Array[x + 1]);
        }

        // Return the maximum product of adjacent elements
        return max;
    }

    // Main method to test the adjacent_Elements_Product function
    public static void Main()
    {
        // Testing the adjacent_Elements_Product function with different input arrays
        Console.WriteLine(adjacent_Elements_Product(new int[] {1, -3, 4, -5, 1})); // Output: 20
        Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 3, 4, 5, 2})); // Output: 20
        Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 3, -4, 5, 2})); // Output: 15
        Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 0, -4, 0, 2})); // Output: 0
    }
}

Sample Output:

-3
20
10
0

Flowchart:

Flowchart: C# Sharp Exercises - Find the pair of adjacent elements that has the highest product of an given array of integers

C# Sharp Code Editor:

Previous: Write a C# program to check if a given string is a palindrome or not.
Next: Write a C# program which will accept a list of integers and checks how many integers are needed to complete the range.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/basic/csharp-basic-exercise-57.php