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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework