Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

C# Sharp Basic: Exercise-57 with Solution

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

Sample Solution:

C# Sharp Code:

using System;
public class Example
{
    public static int adjacent_Elements_Product(int[] input_Array)
        {
          int max = input_Array[0] * input_Array[1];
           for (int x = 1; x <= input_Array.Length - 2; x++)
           {
              max = Math.Max(max, input_Array[x] * input_Array[x + 1]);
            }
         return max;
      }
    public static void Main()
        {
             Console.WriteLine(adjacent_Elements_Product(new int[] {1, -3, 4, -5, 1}));
             Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 3, 4, 5, 2}));
             Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 3, -4, 5, 2}));
             Console.WriteLine(adjacent_Elements_Product(new int[] {1 , 0, -4, 0, 2}));
         }
}    

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:

Improve this sample solution and post your code through Disqus

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.