Check if an subarray is a form of hill

Here given code implementation process.

/*
    C program for
    Check if an subarray is a form of hill
*/
#include <stdio.h>

// Display given array elements
void printArray(int arr[], int n)
{
	printf("\n");
	for (int i = 0; i < n; ++i)
	{
		printf(" %d", arr[i]);
	}
}
void isHill(int arr[], int n, int s, int e)
{
	if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
	{
		return;
	}
	// Use to collect position of adjacent increasing element
	// From left to right
	int left[n];
	// Use to collect position of adjacent decreasing element
	// From right to left
	int right[n];
	int position = 0;
	// Set inital value
	// Set first value of array left
	left[0] = 0;
	// Set last value of array right
	right[n - 1] = n - 1;
	// Collects adjacent position of increasing order element
	for (int i = 1; i < n; ++i)
	{
		if (arr[i] > arr[i - 1])
		{
			position = i;
		}
		left[i] = position;
	}
	position = n - 1;
	// Collects adjacent position of decreasing order element
	for (int i = n - 2; i >= 0; --i)
	{
		if (arr[i] > arr[i + 1])
		{
			position = i;
		}
		right[i] = position;
	}
	if (right[s] >= left[e])
	{
		printf("\n Subarray (%d-%d) is in hill form ", s, e);
	}
	else
	{
		printf("\n Subarray (%d-%d) is not in hill form ", s, e);
	}
}
int main(int argc, char
	const *argv[])
{
	int arr1[] = {
		5 , 2 , 4 , 1 , 6 , 3 , 2
	};
	int arr2[] = {
		7 , 8 , 9 , 4 , 6 , 5 , 2 , 3
	};
	int n = sizeof(arr1) / sizeof(arr1[0]);
	printArray(arr1, n);
	//  s = 2  e = 4
	isHill(arr1, n, 2, 4);
	//  s = 1  e = 3
	isHill(arr1, n, 1, 3);
	// Check arr2
	n = sizeof(arr2) / sizeof(arr2[0]);
	printArray(arr2, n);
	//  s = 1  e = 5
	isHill(arr2, n, 1, 5);
	//  s = 5  e = 7
	isHill(arr2, n, 5, 7);
	//  s = 3  e = 5
	isHill(arr2, n, 3, 5);
	return 0;
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
/*
    Java program for
    Check if an subarray is a form of hill
*/
public class Mount
{
	// Display given array elements
	public void printArray(int[] arr, int n)
	{
		System.out.print("\n");
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
	}
	public void isHill(int[] arr, int n, int s, int e)
	{
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		int[] left = new int[n];
		// Use to collect position of adjacent decreasing element
		// From right to left
		int[] right = new int[n];
		int position = 0;
		// Set inital value
		// Set first value of array left
		left[0] = 0;
		// Set last value of array right
		right[n - 1] = n - 1;
		// Collects adjacent position of increasing order element
		for (int i = 1; i < n; ++i)
		{
			if (arr[i] > arr[i - 1])
			{
				position = i;
			}
			left[i] = position;
		}
		position = n - 1;
		// Collects adjacent position of decreasing order element
		for (int i = n - 2; i >= 0; --i)
		{
			if (arr[i] > arr[i + 1])
			{
				position = i;
			}
			right[i] = position;
		}
		if (right[s] >= left[e])
		{
			System.out.print("\n Subarray (" + 
                             s + "-" + e + ") is in hill form ");
		}
		else
		{
			System.out.print("\n Subarray (" + 
                             s + "-" + e + ") is not in hill form ");
		}
	}
	public static void main(String[] args)
	{
		Mount task = new Mount();
		int[] arr1 = {
			5 , 2 , 4 , 1 , 6 , 3 , 2
		};
		int[] arr2 = {
			7 , 8 , 9 , 4 , 6 , 5 , 2 , 3
		};
		int n = arr1.length;
		task.printArray(arr1, n);
		//  s = 2  e = 4
		task.isHill(arr1, n, 2, 4);
		//  s = 1  e = 3
		task.isHill(arr1, n, 1, 3);
		// Check arr2
		n = arr2.length;
		task.printArray(arr2, n);
		//  s = 1  e = 5
		task.isHill(arr2, n, 1, 5);
		//  s = 5  e = 7
		task.isHill(arr2, n, 5, 7);
		//  s = 3  e = 5
		task.isHill(arr2, n, 3, 5);
	}
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Check if an subarray is a form of hill
*/
class Mount
{
	public:
		// Display given array elements
		void printArray(int arr[], int n)
		{
			cout << "\n";
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	void isHill(int arr[], int n, int s, int e)
	{
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		int left[n];
		// Use to collect position of adjacent decreasing element
		// From right to left
		int right[n];
		int position = 0;
		// Set inital value
		// Set first value of array left
		left[0] = 0;
		// Set last value of array right
		right[n - 1] = n - 1;
		// Collects adjacent position of increasing order element
		for (int i = 1; i < n; ++i)
		{
			if (arr[i] > arr[i - 1])
			{
				position = i;
			}
			left[i] = position;
		}
		position = n - 1;
		// Collects adjacent position of decreasing order element
		for (int i = n - 2; i >= 0; --i)
		{
			if (arr[i] > arr[i + 1])
			{
				position = i;
			}
			right[i] = position;
		}
		if (right[s] >= left[e])
		{
			cout << "\n Subarray (" 
          		 << s << "-" << e << ") is in hill form ";
		}
		else
		{
			cout << "\n Subarray (" 
          		 << s << "-" << e << ") is not in hill form ";
		}
	}
};
int main()
{
	Mount *task = new Mount();
	int arr1[] = {
		5 , 2 , 4 , 1 , 6 , 3 , 2
	};
	int arr2[] = {
		7 , 8 , 9 , 4 , 6 , 5 , 2 , 3
	};
	int n = sizeof(arr1) / sizeof(arr1[0]);
	task->printArray(arr1, n);
	//  s = 2  e = 4
	task->isHill(arr1, n, 2, 4);
	//  s = 1  e = 3
	task->isHill(arr1, n, 1, 3);
	// Check arr2
	n = sizeof(arr2) / sizeof(arr2[0]);
	task->printArray(arr2, n);
	//  s = 1  e = 5
	task->isHill(arr2, n, 1, 5);
	//  s = 5  e = 7
	task->isHill(arr2, n, 5, 7);
	//  s = 3  e = 5
	task->isHill(arr2, n, 3, 5);
	return 0;
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
// Include namespace system
using System;
/*
    Csharp program for
    Check if an subarray is a form of hill
*/
public class Mount
{
	// Display given array elements
	public void printArray(int[] arr, int n)
	{
		Console.Write("\n");
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public void isHill(int[] arr, int n, int s, int e)
	{
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		int[] left = new int[n];
		// Use to collect position of adjacent decreasing element
		// From right to left
		int[] right = new int[n];
		int position = 0;
		// Set inital value
		// Set first value of array left
		left[0] = 0;
		// Set last value of array right
		right[n - 1] = n - 1;
		// Collects adjacent position of increasing order element
		for (int i = 1; i < n; ++i)
		{
			if (arr[i] > arr[i - 1])
			{
				position = i;
			}
			left[i] = position;
		}
		position = n - 1;
		// Collects adjacent position of decreasing order element
		for (int i = n - 2; i >= 0; --i)
		{
			if (arr[i] > arr[i + 1])
			{
				position = i;
			}
			right[i] = position;
		}
		if (right[s] >= left[e])
		{
			Console.Write("\n Subarray (" + 
                          s + "-" + e + ") is in hill form ");
		}
		else
		{
			Console.Write("\n Subarray (" + 
                          s + "-" + e + ") is not in hill form ");
		}
	}
	public static void Main(String[] args)
	{
		Mount task = new Mount();
		int[] arr1 = {
			5 , 2 , 4 , 1 , 6 , 3 , 2
		};
		int[] arr2 = {
			7 , 8 , 9 , 4 , 6 , 5 , 2 , 3
		};
		int n = arr1.Length;
		task.printArray(arr1, n);
		//  s = 2  e = 4
		task.isHill(arr1, n, 2, 4);
		//  s = 1  e = 3
		task.isHill(arr1, n, 1, 3);
		// Check arr2
		n = arr2.Length;
		task.printArray(arr2, n);
		//  s = 1  e = 5
		task.isHill(arr2, n, 1, 5);
		//  s = 5  e = 7
		task.isHill(arr2, n, 5, 7);
		//  s = 3  e = 5
		task.isHill(arr2, n, 3, 5);
	}
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
package main
import "fmt"
/*
    Go program for
    Check if an subarray is a form of hill
*/

// Display given array elements
func printArray(arr[] int, n int) {
	fmt.Print("\n")
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func isHill(arr[] int, n int, s int, e int) {
	if n <= 2 || s < 0 || s >= e || e < 0 || e >= n {
		return
	}
	// Use to collect position of adjacent increasing element
	// From left to right
	var left = make([] int, n)
	// Use to collect position of adjacent decreasing element
	// From right to left
	var right = make([] int, n)
	var position int = 0
	// Set last value of array right
	right[n - 1] = n - 1
	// Collects adjacent position of increasing order element
	for i := 1 ; i < n ; i++ {
		if arr[i] > arr[i - 1] {
			position = i
		}
		left[i] = position
	}
	position = n - 1
	// Collects adjacent position of decreasing order element
	for i := n - 2 ; i >= 0 ; i-- {
		if arr[i] > arr[i + 1] {
			position = i
		}
		right[i] = position
	}
	if right[s] >= left[e] {
		fmt.Print("\n Subarray (", s, "-", e, ") is in hill form ")
	} else {
		fmt.Print("\n Subarray (", s, "-", e, ") is not in hill form ")
	}
}
func main() {

	var arr1 = [] int{5 , 2 , 4 , 1 , 6 , 3 , 2 }
	var arr2 = [] int{ 7 , 8 , 9 , 4 , 6 , 5 , 2 , 3}
	var n int = len(arr1)
	printArray(arr1, n)
	//  s = 2  e = 4
	isHill(arr1, n, 2, 4)
	//  s = 1  e = 3
	isHill(arr1, n, 1, 3)
	// Check arr2
	n = len(arr2)
	printArray(arr2, n)
	//  s = 1  e = 5
	isHill(arr2, n, 1, 5)
	//  s = 5  e = 7
	isHill(arr2, n, 5, 7)
	//  s = 3  e = 5
	isHill(arr2, n, 3, 5)
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
<?php
/*
    Php program for
    Check if an subarray is a form of hill
*/
class Mount
{
	// Display given array elements
	public	function printArray($arr, $n)
	{
		echo("\n");
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public	function isHill($arr, $n, $s, $e)
	{
		if ($n <= 2 || $s < 0 || $s >= $e || $e < 0 || $e >= $n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		$left = array_fill(0, $n, 0);
		// Use to collect position of adjacent decreasing element
		// From right to left
		$right = array_fill(0, $n, 0);
		$position = 0;
		// Set last value of array right
		$right[$n - 1] = $n - 1;
		// Collects adjacent position of increasing order element
		for ($i = 1; $i < $n; ++$i)
		{
			if ($arr[$i] > $arr[$i - 1])
			{
				$position = $i;
			}
			$left[$i] = $position;
		}
		$position = $n - 1;
		// Collects adjacent position of decreasing order element
		for ($i = $n - 2; $i >= 0; --$i)
		{
			if ($arr[$i] > $arr[$i + 1])
			{
				$position = $i;
			}
			$right[$i] = $position;
		}
		if ($right[$s] >= $left[$e])
		{
			echo("\n Subarray (".$s.
				"-".$e.
				") is in hill form ");
		}
		else
		{
			echo("\n Subarray (".$s.
				"-".$e.
				") is not in hill form ");
		}
	}
}

function main()
{
	$task = new Mount();
	$arr1 = array(5, 2, 4, 1, 6, 3, 2);
	$arr2 = array(7, 8, 9, 4, 6, 5, 2, 3);
	$n = count($arr1);
	$task->printArray($arr1, $n);
	//  s = 2  e = 4
	$task->isHill($arr1, $n, 2, 4);
	//  s = 1  e = 3
	$task->isHill($arr1, $n, 1, 3);
	// Check arr2
	$n = count($arr2);
	$task->printArray($arr2, $n);
	//  s = 1  e = 5
	$task->isHill($arr2, $n, 1, 5);
	//  s = 5  e = 7
	$task->isHill($arr2, $n, 5, 7);
	//  s = 3  e = 5
	$task->isHill($arr2, $n, 3, 5);
}
main();

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
/*
    Node JS program for
    Check if an subarray is a form of hill
*/
class Mount
{
	// Display given array elements
	printArray(arr, n)
	{
		process.stdout.write("\n");
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	isHill(arr, n, s, e)
	{
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		var left = Array(n).fill(0);
		// Use to collect position of adjacent decreasing element
		// From right to left
		var right = Array(n).fill(0);
		var position = 0;
		// Set last value of array right
		right[n - 1] = n - 1;
		// Collects adjacent position of increasing order element
		for (var i = 1; i < n; ++i)
		{
			if (arr[i] > arr[i - 1])
			{
				position = i;
			}
			left[i] = position;
		}
		position = n - 1;
		// Collects adjacent position of decreasing order element
		for (var i = n - 2; i >= 0; --i)
		{
			if (arr[i] > arr[i + 1])
			{
				position = i;
			}
			right[i] = position;
		}
		if (right[s] >= left[e])
		{
			process.stdout.write("\n Subarray (" + 
                                 s + "-" + e + ") is in hill form ");
		}
		else
		{
			process.stdout.write("\n Subarray (" + 
                                 s + "-" + e + ") is not in hill form ");
		}
	}
}

function main()
{
	var task = new Mount();
	var arr1 = [5, 2, 4, 1, 6, 3, 2];
	var arr2 = [7, 8, 9, 4, 6, 5, 2, 3];
	var n = arr1.length;
	task.printArray(arr1, n);
	//  s = 2  e = 4
	task.isHill(arr1, n, 2, 4);
	//  s = 1  e = 3
	task.isHill(arr1, n, 1, 3);
	// Check arr2
	n = arr2.length;
	task.printArray(arr2, n);
	//  s = 1  e = 5
	task.isHill(arr2, n, 1, 5);
	//  s = 5  e = 7
	task.isHill(arr2, n, 5, 7);
	//  s = 3  e = 5
	task.isHill(arr2, n, 3, 5);
}
main();

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
#    Python 3 program for
#    Check if an subarray is a form of hill
class Mount :
	#  Display given list elements
	def printArray(self, arr, n) :
		print(end = "\n")
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def isHill(self, arr, n, s, e) :
		if (n <= 2 or s < 0 or s >= e or e < 0 or e >= n) :
			return
		
		#  Use to collect position of adjacent increasing element
		#  From left to right
		left = [0] * (n)
		#  Use to collect position of adjacent decreasing element
		#  From right to left
		right = [0] * (n)
		position = 0
		#  Set last value of list right
		right[n - 1] = n - 1
		i = 1
		#  Collects adjacent position of increasing order element
		while (i < n) :
			if (arr[i] > arr[i - 1]) :
				position = i
			
			left[i] = position
			i += 1
		
		position = n - 1
		i = n - 2
		#  Collects adjacent position of decreasing order element
		while (i >= 0) :
			if (arr[i] > arr[i + 1]) :
				position = i
			
			right[i] = position
			i -= 1
		
		if (right[s] >= left[e]) :
			print("\n Subarray (", s ,"-", e ,") is in hill form ", end = "")
		else :
			print("\n Subarray (", s ,"-", e ,") is not in hill form ", end = "")
		
	

def main() :
	task = Mount()
	arr1 = [5, 2, 4, 1, 6, 3, 2]
	arr2 = [7, 8, 9, 4, 6, 5, 2, 3]
	n = len(arr1)
	task.printArray(arr1, n)
	#   s = 2  e = 4
	task.isHill(arr1, n, 2, 4)
	#   s = 1  e = 3
	task.isHill(arr1, n, 1, 3)
	#  Check arr2
	n = len(arr2)
	task.printArray(arr2, n)
	#   s = 1  e = 5
	task.isHill(arr2, n, 1, 5)
	#   s = 5  e = 7
	task.isHill(arr2, n, 5, 7)
	#   s = 3  e = 5
	task.isHill(arr2, n, 3, 5)

if __name__ == "__main__": main()

Output

  5  2  4  1  6  3  2
 Subarray ( 2 - 4 ) is not in hill form
 Subarray ( 1 - 3 ) is in hill form
  7  8  9  4  6  5  2  3
 Subarray ( 1 - 5 ) is not in hill form
 Subarray ( 5 - 7 ) is not in hill form
 Subarray ( 3 - 5 ) is in hill form
#    Ruby program for
#    Check if an subarray is a form of hill
class Mount 
	#  Display given array elements
	def printArray(arr, n) 
		print("\n")
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def isHill(arr, n, s, e) 
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n) 
			return
		end

		#  Use to collect position of adjacent increasing element
		#  From left to right
		left = Array.new(n) {0}
		#  Use to collect position of adjacent decreasing element
		#  From right to left
		right = Array.new(n) {0}
		position = 0
		#  Set last value of array right
		right[n - 1] = n - 1
		i = 1
		#  Collects adjacent position of increasing order element
		while (i < n) 
			if (arr[i] > arr[i - 1]) 
				position = i
			end

			left[i] = position
			i += 1
		end

		position = n - 1
		i = n - 2
		#  Collects adjacent position of decreasing order element
		while (i >= 0) 
			if (arr[i] > arr[i + 1]) 
				position = i
			end

			right[i] = position
			i -= 1
		end

		if (right[s] >= left[e]) 
			print("\n Subarray (", s ,"-", e ,") is in hill form ")
		else
 
			print("\n Subarray (", s ,"-", e ,") is not in hill form ")
		end

	end

end

def main() 
	task = Mount.new()
	arr1 = [5, 2, 4, 1, 6, 3, 2]
	arr2 = [7, 8, 9, 4, 6, 5, 2, 3]
	n = arr1.length
	task.printArray(arr1, n)
	#   s = 2  e = 4
	task.isHill(arr1, n, 2, 4)
	#   s = 1  e = 3
	task.isHill(arr1, n, 1, 3)
	#  Check arr2
	n = arr2.length
	task.printArray(arr2, n)
	#   s = 1  e = 5
	task.isHill(arr2, n, 1, 5)
	#   s = 5  e = 7
	task.isHill(arr2, n, 5, 7)
	#   s = 3  e = 5
	task.isHill(arr2, n, 3, 5)
end

main()

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form 
 Subarray (1-3) is in hill form 
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form 
 Subarray (5-7) is not in hill form 
 Subarray (3-5) is in hill form 
/*
    Scala program for
    Check if an subarray is a form of hill
*/
class Mount()
{
	// Display given array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		print("\n");
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def isHill(arr: Array[Int], n: Int, s: Int, e: Int): Unit = {
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		var left: Array[Int] = Array.fill[Int](n)(0);
		// Use to collect position of adjacent decreasing element
		// From right to left
		var right: Array[Int] = Array.fill[Int](n)(0);
		var position: Int = 0;
		// Set last value of array right
		right(n - 1) = n - 1;
		var i: Int = 1;
		// Collects adjacent position of increasing order element
		while (i < n)
		{
			if (arr(i) > arr(i - 1))
			{
				position = i;
			}
			left(i) = position;
			i += 1;
		}
		position = n - 1;
		i = n - 2;
		// Collects adjacent position of decreasing order element
		while (i >= 0)
		{
			if (arr(i) > arr(i + 1))
			{
				position = i;
			}
			right(i) = position;
			i -= 1;
		}
		if (right(s) >= left(e))
		{
			print("\n Subarray (" + s + "-" + e + ") is in hill form ");
		}
		else
		{
			print("\n Subarray (" + s + "-" + e + ") is not in hill form ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Mount = new Mount();
		var arr1: Array[Int] = Array(5, 2, 4, 1, 6, 3, 2);
		var arr2: Array[Int] = Array(7, 8, 9, 4, 6, 5, 2, 3);
		var n: Int = arr1.length;
		task.printArray(arr1, n);
		//  s = 2  e = 4
		task.isHill(arr1, n, 2, 4);
		//  s = 1  e = 3
		task.isHill(arr1, n, 1, 3);
		// Check arr2
		n = arr2.length;
		task.printArray(arr2, n);
		//  s = 1  e = 5
		task.isHill(arr2, n, 1, 5);
		//  s = 5  e = 7
		task.isHill(arr2, n, 5, 7);
		//  s = 3  e = 5
		task.isHill(arr2, n, 3, 5);
	}
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form
import Foundation;
/*
    Swift 4 program for
    Check if an subarray is a form of hill
*/
class Mount
{
	// Display given array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		print(terminator: "\n");
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func isHill(_ arr: [Int], _ n: Int, _ s: Int, _ e: Int)
	{
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		var left: [Int] = Array(repeating: 0, count: n);
		// Use to collect position of adjacent decreasing element
		// From right to left
		var right: [Int] = Array(repeating: 0, count: n);
		var position: Int = 0;
		// Set last value of array right
		right[n - 1] = n - 1;
		var i: Int = 1;
		// Collects adjacent position of increasing order element
		while (i < n)
		{
			if (arr[i] > arr[i - 1])
			{
				position = i;
			}
			left[i] = position;
			i += 1;
		}
		position = n - 1;
		i = n - 2;
		// Collects adjacent position of decreasing order element
		while (i >= 0)
		{
			if (arr[i] > arr[i + 1])
			{
				position = i;
			}
			right[i] = position;
			i -= 1;
		}
		if (right[s] >= left[e])
		{
			print("\n Subarray (", s ,"-", 
                  e ,") is in hill form ", terminator: "");
		}
		else
		{
			print("\n Subarray (", s ,"-", 
                  e ,") is not in hill form ", terminator: "");
		}
	}
}
func main()
{
	let task: Mount = Mount();
	let arr1: [Int] = [5, 2, 4, 1, 6, 3, 2];
	let arr2: [Int] = [7, 8, 9, 4, 6, 5, 2, 3];
	var n: Int = arr1.count;
	task.printArray(arr1, n);
	//  s = 2  e = 4
	task.isHill(arr1, n, 2, 4);
	//  s = 1  e = 3
	task.isHill(arr1, n, 1, 3);
	// Check arr2
	n = arr2.count;
	task.printArray(arr2, n);
	//  s = 1  e = 5
	task.isHill(arr2, n, 1, 5);
	//  s = 5  e = 7
	task.isHill(arr2, n, 5, 7);
	//  s = 3  e = 5
	task.isHill(arr2, n, 3, 5);
}
main();

Output

  5  2  4  1  6  3  2
 Subarray ( 2 - 4 ) is not in hill form
 Subarray ( 1 - 3 ) is in hill form
  7  8  9  4  6  5  2  3
 Subarray ( 1 - 5 ) is not in hill form
 Subarray ( 5 - 7 ) is not in hill form
 Subarray ( 3 - 5 ) is in hill form
/*
    Kotlin program for
    Check if an subarray is a form of hill
*/
class Mount
{
	// Display given array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		print("\n");
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun isHill(arr: Array < Int > , n: Int, s: Int, e: Int): Unit
	{
		if (n <= 2 || s < 0 || s >= e || e < 0 || e >= n)
		{
			return;
		}
		// Use to collect position of adjacent increasing element
		// From left to right
		val left: Array < Int > = Array(n)
		{
			0
		};
		// Use to collect position of adjacent decreasing element
		// From right to left
		val right: Array < Int > = Array(n)
		{
			0
		};
		var position: Int = 0;
		// Set last value of array right
		right[n - 1] = n - 1;
		var i: Int = 1;
		// Collects adjacent position of increasing order element
		while (i < n)
		{
			if (arr[i] > arr[i - 1])
			{
				position = i;
			}
			left[i] = position;
			i += 1;
		}
		position = n - 1;
		i = n - 2;
		// Collects adjacent position of decreasing order element
		while (i >= 0)
		{
			if (arr[i] > arr[i + 1])
			{
				position = i;
			}
			right[i] = position;
			i -= 1;
		}
		if (right[s] >= left[e])
		{
			print("\n Subarray (" + s + "-" + e + ") is in hill form ");
		}
		else
		{
			print("\n Subarray (" + s + "-" + e + ") is not in hill form ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Mount = Mount();
	val arr1: Array < Int > = arrayOf(5, 2, 4, 1, 6, 3, 2);
	val arr2: Array < Int > = arrayOf(7, 8, 9, 4, 6, 5, 2, 3);
	var n: Int = arr1.count();
	task.printArray(arr1, n);
	//  s = 2  e = 4
	task.isHill(arr1, n, 2, 4);
	//  s = 1  e = 3
	task.isHill(arr1, n, 1, 3);
	// Check arr2
	n = arr2.count();
	task.printArray(arr2, n);
	//  s = 1  e = 5
	task.isHill(arr2, n, 1, 5);
	//  s = 5  e = 7
	task.isHill(arr2, n, 5, 7);
	//  s = 3  e = 5
	task.isHill(arr2, n, 3, 5);
}

Output

 5 2 4 1 6 3 2
 Subarray (2-4) is not in hill form
 Subarray (1-3) is in hill form
 7 8 9 4 6 5 2 3
 Subarray (1-5) is not in hill form
 Subarray (5-7) is not in hill form
 Subarray (3-5) is in hill form


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved