Find bitonic point in given bitonic sequence

Here given code implementation process.

// C Program
// Find bitonic point in given bitonic sequence
#include <stdio.h>

// Print the elements of given array
void printElement(int arr[], int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("  %d", arr[i]);
	}
}
int findResult(int arr[], int low, int high)
{
	if (low > high)
	{
		return -1;
	}
	// Find middle element in subarray
	int mid = low + (high - low) / 2;
	if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1])
	{
		// When position is resultant point
		return mid;
	}
	else
	{
		if (arr[mid] < arr[mid + 1])
		{
			// Find solution in right subarray
			return findResult(arr, mid + 1, high);
		}
		else
		{
			// Find solution in left subarray
			return findResult(arr, low, mid - 1);
		}
	}
}
// Handles the request of finding the bitonic point
void bitonicPoint(int arr[], int n)
{
	printf("\n Array : ");
	// Display given array
	printElement(arr, n);
	int point = findResult(arr, 0, n - 2);
	if (point == -1)
	{
		printf("\n None");
	}
	else
	{
		printf("\n %d", arr[point]);
	}
}
int main(int argc, char const *argv[])
{
	//  Bitonic sequence
	int arr1[] = {
		1 , 2 , 3 , 4 , 6 , 7 , 3 , 2 , -1
	};
	int arr2[] = {
		1 , 10 , 12 , 16 , 1
	};
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	int n = sizeof(arr1) / sizeof(arr1[0]);
	bitonicPoint(arr1, n);
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	n = sizeof(arr2) / sizeof(arr2[0]);
	bitonicPoint(arr2, n);
	return 0;
}

Output

 Array :   1  2  3  4  6  7  3  2  -1
 7
 Array :   1  10  12  16  1
 16
// Java program for
// Find bitonic point in given bitonic sequence
public class Sequence
{
	// Print the elements of given array
	public void printElement(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
	}
	public int findResult(int[] arr, int low, int high)
	{
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		int mid = low + (high - low) / 2;
		if (arr[mid - 1] < arr[mid] && 
            arr[mid] > arr[mid + 1])
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr[mid] < arr[mid + 1])
			{
				// Find solution in right subarray
				return findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	public void bitonicPoint(int[] arr, int n)
	{
		System.out.print("\n Array : ");
		// Display given array
		printElement(arr, n);
		int point = findResult(arr, 0, n - 2);
		if (point == -1)
		{
			System.out.print("\n None");
		}
		else
		{
			System.out.print("\n " + arr[point]);
		}
	}
	public static void main(String[] args)
	{
		Sequence task = new Sequence();
		//  Bitonic sequence
		int[] arr1 = {
			1 , 2 , 3 , 4 , 6 , 7 , 3 , 2 , -1
		};
		int[] arr2 = {
			1 , 10 , 12 , 16 , 1
		};
		// Test Case A
		// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
		//  Result = 7
		int n = arr1.length;
		task.bitonicPoint(arr1, n);
		// Test Case B
		// [ 1, 10, 12, 16,1]
		//  Result = 16
		n = arr2.length;
		task.bitonicPoint(arr2, n);
	}
}

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find bitonic point in given bitonic sequence
class Sequence
{
	public:
		// Print the elements of given array
		void printElement(int arr[], int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
		}
	int findResult(int arr[], int low, int high)
	{
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		int mid = low + (high - low) / 2;
		if (arr[mid - 1] < arr[mid] && 
            arr[mid] > arr[mid + 1])
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr[mid] < arr[mid + 1])
			{
				// Find solution in right subarray
				return this->findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return this->findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	void bitonicPoint(int arr[], int n)
	{
		cout << "\n Array : ";
		// Display given array
		this->printElement(arr, n);
		int point = this->findResult(arr, 0, n - 2);
		if (point == -1)
		{
			cout << "\n None";
		}
		else
		{
			cout << "\n " << arr[point];
		}
	}
};
int main()
{
	Sequence *task = new Sequence();
	//  Bitonic sequence
	int arr1[] = {
		1 , 2 , 3 , 4 , 6 , 7 , 3 , 2 , -1
	};
	int arr2[] = {
		1 , 10 , 12 , 16 , 1
	};
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	int n = sizeof(arr1) / sizeof(arr1[0]);
	task->bitonicPoint(arr1, n);
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	n = sizeof(arr2) / sizeof(arr2[0]);
	task->bitonicPoint(arr2, n);
	return 0;
}

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
// Include namespace system
using System;
// Csharp program for
// Find bitonic point in given bitonic sequence
public class Sequence
{
	// Print the elements of given array
	public void printElement(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public int findResult(int[] arr, int low, int high)
	{
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		int mid = low + (high - low) / 2;
		if (arr[mid - 1] < arr[mid] && 
            arr[mid] > arr[mid + 1])
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr[mid] < arr[mid + 1])
			{
				// Find solution in right subarray
				return this.findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return this.findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	public void bitonicPoint(int[] arr, int n)
	{
		Console.Write("\n Array : ");
		// Display given array
		this.printElement(arr, n);
		int point = this.findResult(arr, 0, n - 2);
		if (point == -1)
		{
			Console.Write("\n None");
		}
		else
		{
			Console.Write("\n " + arr[point]);
		}
	}
	public static void Main(String[] args)
	{
		Sequence task = new Sequence();
		//  Bitonic sequence
		int[] arr1 = {
			1 , 2 , 3 , 4 , 6 , 7 , 3 , 2 , -1
		};
		int[] arr2 = {
			1 , 10 , 12 , 16 , 1
		};
		// Test Case A
		// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
		//  Result = 7
		int n = arr1.Length;
		task.bitonicPoint(arr1, n);
		// Test Case B
		// [ 1, 10, 12, 16,1]
		//  Result = 16
		n = arr2.Length;
		task.bitonicPoint(arr2, n);
	}
}

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
package main
import "fmt"
// Go program for
// Find bitonic point in given bitonic sequence
type Sequence struct {}
func getSequence() * Sequence {
	var me *Sequence = &Sequence {}
	return me
}
// Print the elements of given array
func(this Sequence) printElement(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func(this Sequence) findResult(arr[] int, 
	low int, high int) int {
	if low > high {
		return -1
	}
	// Find middle element in subarray
	var mid int = low + (high - low) / 2
	if arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1] {
		// When position is resultant point
		return mid
	} else {
		if arr[mid] < arr[mid + 1] {
			// Find solution in right subarray
			return this.findResult(arr, mid + 1, high)
		} else {
			// Find solution in left subarray
			return this.findResult(arr, low, mid - 1)
		}
	}
}
// Handles the request of finding the bitonic point
func(this Sequence) bitonicPoint(arr[] int, n int) {
	fmt.Print("\n Array : ")
	// Display given array
	this.printElement(arr, n)
	var point int = this.findResult(arr, 0, n - 2)
	if point == -1 {
		fmt.Print("\n None")
	} else {
		fmt.Print("\n ", arr[point])
	}
}
func main() {
	var task * Sequence = getSequence()
	//  Bitonic sequence
	var arr1 = [] int {
		1,
		2,
		3,
		4,
		6,
		7,
		3,
		2,
		-1,
	}
	var arr2 = [] int {
		1,
		10,
		12,
		16,
		1,
	}
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	var n int = len(arr1)
	task.bitonicPoint(arr1, n)
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	n = len(arr2)
	task.bitonicPoint(arr2, n)
}

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
<?php
// Php program for
// Find bitonic point in given bitonic sequence
class Sequence
{
	// Print the elements of given array
	public	function printElement($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public	function findResult($arr, $low, $high)
	{
		if ($low > $high)
		{
			return -1;
		}
		// Find middle element in subarray
		$mid = $low + (int)(($high - $low) / 2);
		if ($arr[$mid - 1] < $arr[$mid] && 
            $arr[$mid] > $arr[$mid + 1])
		{
			// When position is resultant point
			return $mid;
		}
		else
		{
			if ($arr[$mid] < $arr[$mid + 1])
			{
				// Find solution in right subarray
				return $this->findResult($arr, $mid + 1, $high);
			}
			else
			{
				// Find solution in left subarray
				return $this->findResult($arr, $low, $mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	public	function bitonicPoint($arr, $n)
	{
		echo("\n Array : ");
		// Display given array
		$this->printElement($arr, $n);
		$point = $this->findResult($arr, 0, $n - 2);
		if ($point == -1)
		{
			echo("\n None");
		}
		else
		{
			echo("\n ".$arr[$point]);
		}
	}
}

function main()
{
	$task = new Sequence();
	//  Bitonic sequence
	$arr1 = array(1, 2, 3, 4, 6, 7, 3, 2, -1);
	$arr2 = array(1, 10, 12, 16, 1);
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	$n = count($arr1);
	$task->bitonicPoint($arr1, $n);
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	$n = count($arr2);
	$task->bitonicPoint($arr2, $n);
}
main();

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
// Node JS program for
// Find bitonic point in given bitonic sequence
class Sequence
{
	// Print the elements of given array
	printElement(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	findResult(arr, low, high)
	{
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		var mid = low + parseInt((high - low) / 2);
		if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1])
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr[mid] < arr[mid + 1])
			{
				// Find solution in right subarray
				return this.findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return this.findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	bitonicPoint(arr, n)
	{
		process.stdout.write("\n Array : ");
		// Display given array
		this.printElement(arr, n);
		var point = this.findResult(arr, 0, n - 2);
		if (point == -1)
		{
			process.stdout.write("\n None");
		}
		else
		{
			process.stdout.write("\n " + arr[point]);
		}
	}
}

function main()
{
	var task = new Sequence();
	//  Bitonic sequence
	var arr1 = [1, 2, 3, 4, 6, 7, 3, 2, -1];
	var arr2 = [1, 10, 12, 16, 1];
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	var n = arr1.length;
	task.bitonicPoint(arr1, n);
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	n = arr2.length;
	task.bitonicPoint(arr2, n);
}
main();

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
#  Python 3 program for
#  Find bitonic point in given bitonic sequence
class Sequence :
	#  Print the elements of given list
	def printElement(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def findResult(self, arr, low, high) :
		if (low > high) :
			return -1
		
		#  Find middle element in sublist
		mid = low + int((high - low) / 2)
		if (arr[mid - 1] < arr[mid] and arr[mid] > arr[mid + 1]) :
			#  When position is resultant point
			return mid
		else :
			if (arr[mid] < arr[mid + 1]) :
				#  Find solution in right sublist
				return self.findResult(arr, mid + 1, high)
			else :
				#  Find solution in left sublist
				return self.findResult(arr, low, mid - 1)
			
		
	
	#  Handles the request of finding the bitonic point
	def bitonicPoint(self, arr, n) :
		print("\n Array : ", end = "")
		#  Display given list
		self.printElement(arr, n)
		point = self.findResult(arr, 0, n - 2)
		if (point == -1) :
			print("\n None", end = "")
		else :
			print("\n ", arr[point], end = "")
		
	

def main() :
	task = Sequence()
	#   Bitonic sequence
	arr1 = [1, 2, 3, 4, 6, 7, 3, 2, -1]
	arr2 = [1, 10, 12, 16, 1]
	#  Test Case A
	#  [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	#   Result = 7
	n = len(arr1)
	task.bitonicPoint(arr1, n)
	#  Test Case B
	#  [ 1, 10, 12, 16,1]
	#   Result = 16
	n = len(arr2)
	task.bitonicPoint(arr2, n)

if __name__ == "__main__": main()

Output

 Array :   1  2  3  4  6  7  3  2  -1
  7
 Array :   1  10  12  16  1
  16
#  Ruby program for
#  Find bitonic point in given bitonic sequence
class Sequence 
	#  Print the elements of given array
	def printElement(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def findResult(arr, low, high) 
		if (low > high) 
			return -1
		end

		#  Find middle element in subarray
		mid = low + (high - low) / 2
		if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1]) 
			#  When position is resultant point
			return mid
		else
 
			if (arr[mid] < arr[mid + 1]) 
				#  Find solution in right subarray
				return self.findResult(arr, mid + 1, high)
			else
 
				#  Find solution in left subarray
				return self.findResult(arr, low, mid - 1)
			end

		end

	end

	#  Handles the request of finding the bitonic point
	def bitonicPoint(arr, n) 
		print("\n Array : ")
		#  Display given array
		self.printElement(arr, n)
		point = self.findResult(arr, 0, n - 2)
		if (point == -1) 
			print("\n None")
		else
 
			print("\n ", arr[point])
		end

	end

end

def main() 
	task = Sequence.new()
	#   Bitonic sequence
	arr1 = [1, 2, 3, 4, 6, 7, 3, 2, -1]
	arr2 = [1, 10, 12, 16, 1]
	#  Test Case A
	#  [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	#   Result = 7
	n = arr1.length
	task.bitonicPoint(arr1, n)
	#  Test Case B
	#  [ 1, 10, 12, 16,1]
	#   Result = 16
	n = arr2.length
	task.bitonicPoint(arr2, n)
end

main()

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
// Scala program for
// Find bitonic point in given bitonic sequence
class Sequence()
{
	// Print the elements of given array
	def printElement(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def findResult(arr: Array[Int], low: Int, high: Int): Int = {
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		var mid: Int = low + (high - low) / 2;
		if (arr(mid - 1) < arr(mid) && arr(mid) > arr(mid + 1))
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr(mid) < arr(mid + 1))
			{
				// Find solution in right subarray
				return findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	def bitonicPoint(arr: Array[Int], n: Int): Unit = {
		print("\n Array : ");
		// Display given array
		printElement(arr, n);
		var point: Int = findResult(arr, 0, n - 2);
		if (point == -1)
		{
			print("\n None");
		}
		else
		{
			print("\n " + arr(point));
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Sequence = new Sequence();
		//  Bitonic sequence
		var arr1: Array[Int] = Array(1, 2, 3, 4, 6, 7, 3, 2, -1);
		var arr2: Array[Int] = Array(1, 10, 12, 16, 1);
		// Test Case A
		// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
		//  Result = 7
		var n: Int = arr1.length;
		task.bitonicPoint(arr1, n);
		// Test Case B
		// [ 1, 10, 12, 16,1]
		//  Result = 16
		n = arr2.length;
		task.bitonicPoint(arr2, n);
	}
}

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16
import Foundation;
// Swift 4 program for
// Find bitonic point in given bitonic sequence
class Sequence
{
	// Print the elements of given array
	func printElement(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func findResult(_ arr: [Int], _ low: Int, _ high: Int) -> Int
	{
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		let mid: Int = low + (high - low) / 2;
		if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1])
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr[mid] < arr[mid + 1])
			{
				// Find solution in right subarray
				return self.findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return self.findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	func bitonicPoint(_ arr: [Int], _ n: Int)
	{
		print("\n Array : ", terminator: "");
		// Display given array
		self.printElement(arr, n);
		let point: Int = self.findResult(arr, 0, n - 2);
		if (point == -1)
		{
			print("\n None", terminator: "");
		}
		else
		{
			print("\n ", arr[point], terminator: "");
		}
	}
}
func main()
{
	let task: Sequence = Sequence();
	//  Bitonic sequence
	let arr1: [Int] = [1, 2, 3, 4, 6, 7, 3, 2, -1];
	let arr2: [Int] = [1, 10, 12, 16, 1];
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	var n: Int = arr1.count;
	task.bitonicPoint(arr1, n);
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	n = arr2.count;
	task.bitonicPoint(arr2, n);
}
main();

Output

 Array :   1  2  3  4  6  7  3  2  -1
  7
 Array :   1  10  12  16  1
  16
// Kotlin program for
// Find bitonic point in given bitonic sequence
class Sequence
{
	// Print the elements of given array
	fun printElement(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun findResult(arr: Array < Int > , low: Int, high: Int): Int
	{
		if (low > high)
		{
			return -1;
		}
		// Find middle element in subarray
		val mid: Int = low + (high - low) / 2;
		if (arr[mid - 1] < arr[mid] && arr[mid] > arr[mid + 1])
		{
			// When position is resultant point
			return mid;
		}
		else
		{
			if (arr[mid] < arr[mid + 1])
			{
				// Find solution in right subarray
				return this.findResult(arr, mid + 1, high);
			}
			else
			{
				// Find solution in left subarray
				return this.findResult(arr, low, mid - 1);
			}
		}
	}
	// Handles the request of finding the bitonic point
	fun bitonicPoint(arr: Array < Int > , n: Int): Unit
	{
		print("\n Array : ");
		// Display given array
		this.printElement(arr, n);
		val point: Int = this.findResult(arr, 0, n - 2);
		if (point == -1)
		{
			print("\n None");
		}
		else
		{
			print("\n " + arr[point]);
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Sequence = Sequence();
	//  Bitonic sequence
	val arr1: Array < Int > = arrayOf(1, 2, 3, 4, 6, 7, 3, 2, -1);
	val arr2: Array < Int > = arrayOf(1, 10, 12, 16, 1);
	// Test Case A
	// [ 1 , 2, 3, 4, 6, 7, 3, 2, -1]
	//  Result = 7
	var n: Int = arr1.count();
	task.bitonicPoint(arr1, n);
	// Test Case B
	// [ 1, 10, 12, 16,1]
	//  Result = 16
	n = arr2.count();
	task.bitonicPoint(arr2, n);
}

Output

 Array :  1 2 3 4 6 7 3 2 -1
 7
 Array :  1 10 12 16 1
 16


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