Find the minimum element in a sorted and rotated array

Here given code implementation process.

//  C program for
//  Find the minimum element in a sorted and rotated array
#include <stdio.h>
 //Display array elements
void display(int arr[], int n)
{
	printf("\n\n Array Elements \n [");
	for (int i = 0; i < n; ++i)
	{
		printf("   %d", arr[i]);
	}
	printf("  ]\n");
}
// Find min element in array
int findMin(int arr[], int low, int high)
{
	if (low > high)
	{
		return 0;
	}
	if (low == high)
	{
		return low;
	}
	//Find middle element
	int mid = low + ((high - low) / 2);
	if (mid < high && arr[mid + 1] < arr[mid])
	{
		return mid + 1;
	}
	else if (mid > low && arr[mid] < arr[mid - 1])
	{
		return mid;
	}
	else
	{
		// When have exists duplicate elements
		int a = findMin(arr, low, mid);
		int b = findMin(arr, mid + 1, high);
		if (arr[a] < arr[b])
		{
			return a;
		}
		else
		{
			return b;
		}
	}
}
// Handles the request of finding min element in array
// We assume that given array is sorted and rotated.
void minElement(int arr[], int size)
{
	int index = 0;
	if (size > 1)
	{
		index = findMin(arr, 0, size - 1);
	}
	printf(" Min Element %d \n", arr[index]);
}
int main(int argc, char const *argv[])
{
	// Defining sorted and rotated array of integer element
	int arr1[] = {
		7 , 8 , 9 , 1 , 2 , 3 , 4 , 5 , 6
	};
	int arr2[] = {
		11 , 22 , 43 , 45 , 51 , 62 , 73 , 2 , 9 , 10
	};
	int arr3[] = {
		2 , 2 , 5 , -1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
	};
	// Get the size
	int size = sizeof(arr1) / sizeof(arr1[0]);
	display(arr1, size);
	minElement(arr1, size);
	// Get the size
	size = sizeof(arr2) / sizeof(arr2[0]);
	display(arr2, size);
	// Test Case B 
	minElement(arr2, size);
	// Get the size
	size = sizeof(arr3) / sizeof(arr3[0]);
	display(arr3, size);
	// Test Case C 
	minElement(arr3, size);
	return 0;
}

Output

 Array Elements
 [   7   8   9   1   2   3   4   5   6  ]
 Min Element 1


 Array Elements
 [   11   22   43   45   51   62   73   2   9   10  ]
 Min Element 2


 Array Elements
 [   2   2   5   -1   2   2   2   2   2   2   2   2  ]
 Min Element -1
// Java Program 
// Find the minimum element in a sorted and rotated array
public class Searching
{
	//Display array elements
	public void display(int[] arr, int n)
	{
		System.out.print("\n\n Array Elements \n [");
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print(" ]\n");
	}
	// Find min element in array
	public int findMin(int[] arr, int low, int high)
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		int mid = low + ((high - low) / 2);
		if (mid < high && arr[mid + 1] < arr[mid])
		{
			return mid + 1;
		}
		else if (mid > low && arr[mid] < arr[mid - 1])
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			int a = findMin(arr, low, mid);
			int b = findMin(arr, mid + 1, high);
			if (arr[a] < arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	public void minElement(int[] arr, int size)
	{
		int index = 0;
		if (size > 1)
		{
			index = findMin(arr, 0, size - 1);
		}
		System.out.println(" Min Element " + arr[index]);
	}
	public static void main(String args[])
	{
		Searching task = new Searching();
		// Defining sorted and rotated array of integer element
		int[] arr1 = {
			7 , 8 , 9 , 1 , 2 , 3 , 4 , 5 , 6
		};
		int[] arr2 = {
			11 , 22 , 43 , 45 , 51 , 62 , 73 , 2 , 9 , 10
		};
		int[] arr3 = {
			2 , 2 , 5 , -1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
		};
		// Get the size
		int size = arr1.length;
		task.display(arr1, size);
		task.minElement(arr1, size);
		// Get the size
		size = arr2.length;
		task.display(arr2, size);
		// Test Case B 
		task.minElement(arr2, size);
		// Get the size
		size = arr3.length;
		task.display(arr3, size);
		// Test Case C 
		task.minElement(arr3, size);
	}
}

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
// Include header file
#include <iostream>
using namespace std;
// C++ Program 
// Find the minimum element in a sorted and rotated array
class Searching
{
	public:
		//Display array elements
		void display(int arr[], int n)
		{
			cout << "\n\n Array Elements \n [";
			for (int i = 0; i < n; ++i)
			{
				cout << " " << arr[i];
			}
			cout << " ]\n";
		}
	// Find min element in array
	int findMin(int arr[], int low, int high)
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		int mid = low + ((high - low) / 2);
		if (mid < high && arr[mid + 1] < arr[mid])
		{
			return mid + 1;
		}
		else if (mid > low && arr[mid] < arr[mid - 1])
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			int a = this->findMin(arr, low, mid);
			int b = this->findMin(arr, mid + 1, high);
			if (arr[a] < arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	void minElement(int arr[], int size)
	{
		int index = 0;
		if (size > 1)
		{
			index = this->findMin(arr, 0, size - 1);
		}
		cout << " Min Element " << arr[index] << endl;
	}
};
int main()
{
	Searching *task = new Searching();
	// Defining sorted and rotated array of integer element
	int arr1[] = {
		7 , 8 , 9 , 1 , 2 , 3 , 4 , 5 , 6
	};
	int arr2[] = {
		11 , 22 , 43 , 45 , 51 , 62 , 73 , 2 , 9 , 10
	};
	int arr3[] = {
		2 , 2 , 5 , -1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
	};
	// Get the size
	int size = sizeof(arr1) / sizeof(arr1[0]);
	task->display(arr1, size);
	task->minElement(arr1, size);
	// Get the size
	size = sizeof(arr2) / sizeof(arr2[0]);
	task->display(arr2, size);
	// Test Case B 
	task->minElement(arr2, size);
	// Get the size
	size = sizeof(arr3) / sizeof(arr3[0]);
	task->display(arr3, size);
	// Test Case C 
	task->minElement(arr3, size);
	return 0;
}

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
// Include namespace system
using System;
// Csharp Program 
// Find the minimum element in a sorted and rotated array
public class Searching
{
	//Display array elements
	public void display(int[] arr, int n)
	{
		Console.Write("\n\n Array Elements \n [");
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write(" ]\n");
	}
	// Find min element in array
	public int findMin(int[] arr, int low, int high)
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		int mid = low + ((high - low) / 2);
		if (mid < high && arr[mid + 1] < arr[mid])
		{
			return mid + 1;
		}
		else if (mid > low && arr[mid] < arr[mid - 1])
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			int a = this.findMin(arr, low, mid);
			int b = this.findMin(arr, mid + 1, high);
			if (arr[a] < arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	public void minElement(int[] arr, int size)
	{
		int index = 0;
		if (size > 1)
		{
			index = this.findMin(arr, 0, size - 1);
		}
		Console.WriteLine(" Min Element " + arr[index]);
	}
	public static void Main(String[] args)
	{
		Searching task = new Searching();
		// Defining sorted and rotated array of integer element
		int[] arr1 = {
			7 , 8 , 9 , 1 , 2 , 3 , 4 , 5 , 6
		};
		int[] arr2 = {
			11 , 22 , 43 , 45 , 51 , 62 , 73 , 2 , 9 , 10
		};
		int[] arr3 = {
			2 , 2 , 5 , -1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
		};
		// Get the size
		int size = arr1.Length;
		task.display(arr1, size);
		task.minElement(arr1, size);
		// Get the size
		size = arr2.Length;
		task.display(arr2, size);
		// Test Case B 
		task.minElement(arr2, size);
		// Get the size
		size = arr3.Length;
		task.display(arr3, size);
		// Test Case C 
		task.minElement(arr3, size);
	}
}

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
package main
import "fmt"
// Go Program 
// Find the minimum element in a sorted and rotated array
type Searching struct {}
func getSearching() * Searching {
	var me *Searching = &Searching {}
	return me
}
//Display array elements
func(this Searching) display(arr[] int, n int) {
	fmt.Print("\n\n Array Elements \n [")
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
	fmt.Print(" ]\n")
}
// Find min element in array
func(this Searching) findMin(arr[] int, low int, high int) int {
	if low > high {
		return 0
	}
	if low == high {
		return low
	}
	//Find middle element
	var mid int = low + ((high - low) / 2)
	if mid < high && arr[mid + 1] < arr[mid] {
		return mid + 1
	} else if mid > low && arr[mid] < arr[mid - 1] {
		return mid
	} else {
		// When have exists duplicate elements
		var a int = this.findMin(arr, low, mid)
		var b int = this.findMin(arr, mid + 1, high)
		if arr[a] < arr[b] {
			return a
		} else {
			return b
		}
	}
}
// Handles the request of finding min element in array
// We assume that given array is sorted and rotated.
func(this Searching) minElement(arr[] int, size int) {
	var index int = 0
	if size > 1 {
		index = this.findMin(arr, 0, size - 1)
	}
	fmt.Println(" Min Element ", arr[index])
}
func main() {
	var task * Searching = getSearching()
	// Defining sorted and rotated array of integer element
	var arr1 = [] int {
		7,
		8,
		9,
		1,
		2,
		3,
		4,
		5,
		6,
	}
	var arr2 = [] int {
		11,
		22,
		43,
		45,
		51,
		62,
		73,
		2,
		9,
		10,
	}
	var arr3 = [] int {
		2,
		2,
		5,
		-1,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
		2,
	}
	// Get the size
	var size int = len(arr1)
	task.display(arr1, size)
	task.minElement(arr1, size)
	// Get the size
	size = len(arr2)
	task.display(arr2, size)
	// Test Case B 
	task.minElement(arr2, size)
	// Get the size
	size = len(arr3)
	task.display(arr3, size)
	// Test Case C 
	task.minElement(arr3, size)
}

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
<?php
// Php Program 
// Find the minimum element in a sorted and rotated array
class Searching
{
	//Display array elements
	public	function display($arr, $n)
	{
		echo("\n\n Array Elements \n [");
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
		echo(" ]\n");
	}
	// Find min element in array
	public	function findMin($arr, $low, $high)
	{
		if ($low > $high)
		{
			return 0;
		}
		if ($low == $high)
		{
			return $low;
		}
		//Find middle element
		$mid = $low + ((int)(($high - $low) / 2));
		if ($mid < $high && $arr[$mid + 1] < $arr[$mid])
		{
			return $mid + 1;
		}
		else if ($mid > $low && $arr[$mid] < $arr[$mid - 1])
		{
			return $mid;
		}
		else
		{
			// When have exists duplicate elements
			$a = $this->findMin($arr, $low, $mid);
			$b = $this->findMin($arr, $mid + 1, $high);
			if ($arr[$a] < $arr[$b])
			{
				return $a;
			}
			else
			{
				return $b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	public	function minElement($arr, $size)
	{
		$index = 0;
		if ($size > 1)
		{
			$index = $this->findMin($arr, 0, $size - 1);
		}
		echo(" Min Element ".$arr[$index]."\n");
	}
}

function main()
{
	$task = new Searching();
	// Defining sorted and rotated array of integer element
	$arr1 = array(7, 8, 9, 1, 2, 3, 4, 5, 6);
	$arr2 = array(11, 22, 43, 45, 51, 62, 73, 2, 9, 10);
	$arr3 = array(2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2);
	// Get the size
	$size = count($arr1);
	$task->display($arr1, $size);
	$task->minElement($arr1, $size);
	// Get the size
	$size = count($arr2);
	$task->display($arr2, $size);
	// Test Case B 
	$task->minElement($arr2, $size);
	// Get the size
	$size = count($arr3);
	$task->display($arr3, $size);
	// Test Case C 
	$task->minElement($arr3, $size);
}
main();

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
// Node JS Program 
// Find the minimum element in a sorted and rotated array
class Searching
{
	//Display array elements
	display(arr, n)
	{
		process.stdout.write("\n\n Array Elements \n [");
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write(" ]\n");
	}
	// Find min element in array
	findMin(arr, low, high)
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		var mid = low + (parseInt((high - low) / 2));
		if (mid < high && arr[mid + 1] < arr[mid])
		{
			return mid + 1;
		}
		else if (mid > low && arr[mid] < arr[mid - 1])
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			var a = this.findMin(arr, low, mid);
			var b = this.findMin(arr, mid + 1, high);
			if (arr[a] < arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	minElement(arr, size)
	{
		var index = 0;
		if (size > 1)
		{
			index = this.findMin(arr, 0, size - 1);
		}
		console.log(" Min Element " + arr[index]);
	}
}

function main()
{
	var task = new Searching();
	// Defining sorted and rotated array of integer element
	var arr1 = [7, 8, 9, 1, 2, 3, 4, 5, 6];
	var arr2 = [11, 22, 43, 45, 51, 62, 73, 2, 9, 10];
	var arr3 = [2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2];
	// Get the size
	var size = arr1.length;
	task.display(arr1, size);
	task.minElement(arr1, size);
	// Get the size
	size = arr2.length;
	task.display(arr2, size);
	// Test Case B 
	task.minElement(arr2, size);
	// Get the size
	size = arr3.length;
	task.display(arr3, size);
	// Test Case C 
	task.minElement(arr3, size);
}
main();

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
#  Python 3 Program 
#  Find the minimum element in a sorted and rotated array
class Searching :
	# Display list elements
	def display(self, arr, n) :
		print("\n\n Array Elements \n [", end = "")
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
		print(" ]")
	
	#  Find min element in list
	def findMin(self, arr, low, high) :
		if (low > high) :
			return 0
		
		if (low == high) :
			return low
		
		# Find middle element
		mid = low + (int((high - low) / 2))
		if (mid < high and arr[mid + 1] < arr[mid]) :
			return mid + 1
		elif (mid > low and arr[mid] < arr[mid - 1]) :
			return mid
		else :
			#  When have exists duplicate elements
			a = self.findMin(arr, low, mid)
			b = self.findMin(arr, mid + 1, high)
			if (arr[a] < arr[b]) :
				return a
			else :
				return b
			
		
	
	#  Handles the request of finding min element in list
	#  We assume that given list is sorted and rotated.
	def minElement(self, arr, size) :
		index = 0
		if (size > 1) :
			index = self.findMin(arr, 0, size - 1)
		
		print(" Min Element ", arr[index])
	

def main() :
	task = Searching()
	#  Defining sorted and rotated list of integer element
	arr1 = [7, 8, 9, 1, 2, 3, 4, 5, 6]
	arr2 = [11, 22, 43, 45, 51, 62, 73, 2, 9, 10]
	arr3 = [2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2]
	#  Get the size
	size = len(arr1)
	task.display(arr1, size)
	task.minElement(arr1, size)
	#  Get the size
	size = len(arr2)
	task.display(arr2, size)
	#  Test Case B 
	task.minElement(arr2, size)
	#  Get the size
	size = len(arr3)
	task.display(arr3, size)
	#  Test Case C 
	task.minElement(arr3, size)

if __name__ == "__main__": main()

Output

 Array Elements
 [  7  8  9  1  2  3  4  5  6 ]
 Min Element  1


 Array Elements
 [  11  22  43  45  51  62  73  2  9  10 ]
 Min Element  2


 Array Elements
 [  2  2  5  -1  2  2  2  2  2  2  2  2 ]
 Min Element  -1
#  Ruby Program 
#  Find the minimum element in a sorted and rotated array
class Searching 
	# Display array elements
	def display(arr, n) 
		print("\n\n Array Elements \n [")
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

		print(" ]\n")
	end

	#  Find min element in array
	def findMin(arr, low, high) 
		if (low > high) 
			return 0
		end

		if (low == high) 
			return low
		end

		# Find middle element
		mid = low + ((high - low) / 2)
		if (mid < high && arr[mid + 1] < arr[mid]) 
			return mid + 1
		elsif (mid > low && arr[mid] < arr[mid - 1]) 
			return mid
		else
 
			#  When have exists duplicate elements
			a = self.findMin(arr, low, mid)
			b = self.findMin(arr, mid + 1, high)
			if (arr[a] < arr[b]) 
				return a
			else
 
				return b
			end

		end

	end

	#  Handles the request of finding min element in array
	#  We assume that given array is sorted and rotated.
	def minElement(arr, size) 
		index = 0
		if (size > 1) 
			index = self.findMin(arr, 0, size - 1)
		end

		print(" Min Element ", arr[index], "\n")
	end

end

def main() 
	task = Searching.new()
	#  Defining sorted and rotated array of integer element
	arr1 = [7, 8, 9, 1, 2, 3, 4, 5, 6]
	arr2 = [11, 22, 43, 45, 51, 62, 73, 2, 9, 10]
	arr3 = [2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2]
	#  Get the size
	size = arr1.length
	task.display(arr1, size)
	task.minElement(arr1, size)
	#  Get the size
	size = arr2.length
	task.display(arr2, size)
	#  Test Case B 
	task.minElement(arr2, size)
	#  Get the size
	size = arr3.length
	task.display(arr3, size)
	#  Test Case C 
	task.minElement(arr3, size)
end

main()

Output


 Array Elements 
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements 
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements 
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
// Scala Program 
// Find the minimum element in a sorted and rotated array
class Searching()
{
	//Display array elements
	def display(arr: Array[Int], n: Int): Unit = {
		print("\n\n Array Elements \n [");
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
		print(" ]\n");
	}
	// Find min element in array
	def findMin(arr: Array[Int], low: Int, high: Int): Int = {
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		var mid: Int = low + ((high - low) / 2);
		if (mid < high && arr(mid + 1) < arr(mid))
		{
			return mid + 1;
		}
		else if (mid > low && arr(mid) < arr(mid - 1))
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			var a: Int = findMin(arr, low, mid);
			var b: Int = findMin(arr, mid + 1, high);
			if (arr(a) < arr(b))
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	def minElement(arr: Array[Int], size: Int): Unit = {
		var index: Int = 0;
		if (size > 1)
		{
			index = findMin(arr, 0, size - 1);
		}
		println(" Min Element " + arr(index));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Searching = new Searching();
		// Defining sorted and rotated array of integer element
		var arr1: Array[Int] = Array(7, 8, 9, 1, 2, 3, 4, 5, 6);
		var arr2: Array[Int] = Array(11, 22, 43, 45, 51, 62, 73, 2, 9, 10);
		var arr3: Array[Int] = Array(2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2);
		// Get the size
		var size: Int = arr1.length;
		task.display(arr1, size);
		task.minElement(arr1, size);
		// Get the size
		size = arr2.length;
		task.display(arr2, size);
		// Test Case B 
		task.minElement(arr2, size);
		// Get the size
		size = arr3.length;
		task.display(arr3, size);
		// Test Case C 
		task.minElement(arr3, size);
	}
}

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1
import Foundation;
// Swift 4 Program 
// Find the minimum element in a sorted and rotated array
class Searching
{
	//Display array elements
	func display(_ arr: [Int], _ n: Int)
	{
		print("\n\n Array Elements \n [", terminator: "");
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print(" ]");
	}
	// Find min element in array
	func findMin(_ arr: [Int], _ low: Int, _ high: Int) -> Int
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		let mid: Int = low + ((high - low) / 2);
		if (mid < high && arr[mid + 1] < arr[mid])
		{
			return mid + 1;
		}
		else if (mid > low && arr[mid] < arr[mid - 1])
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			let a: Int = self.findMin(arr, low, mid);
			let b: Int = self.findMin(arr, mid + 1, high);
			if (arr[a] < arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	func minElement(_ arr: [Int], _ size: Int)
	{
		var index: Int = 0;
		if (size > 1)
		{
			index = self.findMin(arr, 0, size - 1);
		}
		print(" Min Element ", arr[index]);
	}
}
func main()
{
	let task: Searching = Searching();
	// Defining sorted and rotated array of integer element
	let arr1: [Int] = [7, 8, 9, 1, 2, 3, 4, 5, 6];
	let arr2: [Int] = [11, 22, 43, 45, 51, 62, 73, 2, 9, 10];
	let arr3: [Int] = [2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2];
	// Get the size
	var size: Int = arr1.count;
	task.display(arr1, size);
	task.minElement(arr1, size);
	// Get the size
	size = arr2.count;
	task.display(arr2, size);
	// Test Case B 
	task.minElement(arr2, size);
	// Get the size
	size = arr3.count;
	task.display(arr3, size);
	// Test Case C 
	task.minElement(arr3, size);
}
main();

Output

 Array Elements
 [  7  8  9  1  2  3  4  5  6 ]
 Min Element  1


 Array Elements
 [  11  22  43  45  51  62  73  2  9  10 ]
 Min Element  2


 Array Elements
 [  2  2  5  -1  2  2  2  2  2  2  2  2 ]
 Min Element  -1
// Kotlin Program 
// Find the minimum element in a sorted and rotated array
class Searching
{
	//Display array elements
	fun display(arr: Array < Int > , n: Int): Unit
	{
		print("\n\n Array Elements \n [");
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
		print(" ]\n");
	}
	// Find min element in array
	fun findMin(arr: Array < Int > , low: Int, high: Int): Int
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		//Find middle element
		val mid: Int = low + ((high - low) / 2);
		if (mid < high && arr[mid + 1] < arr[mid])
		{
			return mid + 1;
		}
		else if (mid > low && arr[mid] < arr[mid - 1])
		{
			return mid;
		}
		else
		{
			// When have exists duplicate elements
			val a: Int = this.findMin(arr, low, mid);
			val b: Int = this.findMin(arr, mid + 1, high);
			if (arr[a] < arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding min element in array
	// We assume that given array is sorted and rotated.
	fun minElement(arr: Array < Int > , size: Int): Unit
	{
		var index: Int = 0;
		if (size > 1)
		{
			index = this.findMin(arr, 0, size - 1);
		}
		println(" Min Element " + arr[index]);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Searching = Searching();
	// Defining sorted and rotated array of integer element
	val arr1: Array < Int > = arrayOf(7, 8, 9, 1, 2, 3, 4, 5, 6);
	val arr2: Array < Int > = arrayOf(11, 22, 43, 45, 51, 62, 73, 2, 9, 10);
	val arr3: Array < Int > = arrayOf(2, 2, 5, -1, 2, 2, 2, 2, 2, 2, 2, 2);
	// Get the size
	var size: Int = arr1.count();
	task.display(arr1, size);
	task.minElement(arr1, size);
	// Get the size
	size = arr2.count();
	task.display(arr2, size);
	// Test Case B 
	task.minElement(arr2, size);
	// Get the size
	size = arr3.count();
	task.display(arr3, size);
	// Test Case C 
	task.minElement(arr3, size);
}

Output

 Array Elements
 [ 7 8 9 1 2 3 4 5 6 ]
 Min Element 1


 Array Elements
 [ 11 22 43 45 51 62 73 2 9 10 ]
 Min Element 2


 Array Elements
 [ 2 2 5 -1 2 2 2 2 2 2 2 2 ]
 Min Element -1


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