Skip to main content

Find maximum element in a sorted and rotated array

Here given code implementation process.

// C Program
// Find maximum element in a sorted and rotated array
#include <stdio.h>

//Display array elements
void display(int arr[], int size)
{
	printf("\n\n Array Elements \n [");
	for (int i = 0; i < size; ++i)
	{
		printf("   %d", arr[i]);
	}
	printf("  ]\n");
}
//Find max element in array
int findMax(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 (arr[low] > arr[mid])
	{
		return findMax(arr, low, mid - 1);
	}
	else if (arr[mid] > arr[high])
	{
		return findMax(arr, mid, high - 1);
	}
	else if (arr[high] > arr[mid])
	{
		return high;
	}
	else
	{
		//When have exists duplicate elements
		int a = findMax(arr, low, mid);
		int b = findMax(arr, mid + 1, high);
		if (arr[a] > arr[b])
		{
			return a;
		}
		else
		{
			return b;
		}
	}
}
// Handles the request of finding max element in array
// We assume that given array is sorted and rotated
void maxElement(int arr[], int size)
{
	int location = 0;
	if (size > 1)
	{
		location = findMax(arr, 0, size - 1);
	}
	printf(" Max Element %d \n", arr[location]);
}
int main()
{
	// 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 , 1 , 9 , 10
	};
	int arr3[] = {
		2 , 2 , 5 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
	};
	// Get the size
	int size = sizeof(arr1) / sizeof(arr1[0]);
	display(arr1, size);
	maxElement(arr1, size);
	// Get the size
	size = sizeof(arr2) / sizeof(arr2[0]);
	display(arr2, size);
	// Test Case B 
	maxElement(arr2, size);
	// Get the size
	size = sizeof(arr3) / sizeof(arr3[0]);
	display(arr3, size);
	// Test Case C 
	maxElement(arr3, size);
	return 0;
}

Output

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


 Array Elements
 [   11   22   43   45   51   62   73   1   9   10  ]
 Max Element 73


 Array Elements
 [   2   2   5   2   2   2   2   2   2   2   2  ]
 Max Element 5
/*
    Java program 
    Find maximum element in a sorted and rotated array
*/
public class SearchElement
{
    //Display array elements
    public void display(int[] arr, int size)
    {
        System.out.print("\n\n Array Elements \n [");
        for (int i = 0; i < size; ++i)
        {
            System.out.print("  " + arr[i]  );
        }
        System.out.print(" ]\n");
    }
    //Find max element in array
    public int findMax(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 (arr[low] > arr[mid])
        {
            return findMax(arr, low, mid - 1);
        }
        else if (arr[mid] > arr[high])
        {
            return findMax(arr, mid, high - 1);
        }
        else if (arr[high] > arr[mid])
        {
            return high;
        }
        else
        {
            //When have exists duplicate elements
            int a = findMax(arr, low, mid);
            int b = findMax(arr, mid + 1, high);
            if (arr[a] > arr[b])
            {
                return a;
            }
            else
            {
                return b;
            }
        }
    }
    // Handles the request of finding max element in array
    // We assume that given array is sorted and rotated
    public void maxElement(int[] arr, int size)
    {
        int location = findMax(arr, 0, size - 1);
      
        System.out.print(" Max Element " + arr[location] + " \n");
    }
    public static void main(String[] args)
    {
        SearchElement work = new SearchElement();
        // 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 , 1 , 9 , 10
        };
        int[] arr3 =  {
            2 , 2 , 5 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
        };
        // Get the size
        int size = arr1.length;
        work.display(arr1, size);
        work.maxElement(arr1, size);
        // Get the size
        size = arr2.length;
        work.display(arr2, size);
        // Test Case B 
        work.maxElement(arr2, size);
        // Get the size
        size = arr3.length;
        work.display(arr3, size);
        // Test Case C 
        work.maxElement(arr3, size);
    }
}

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program 
    Find maximum element in a sorted and rotated array
*/
class SearchElement
{
	public:
		// Display array elements
		void display(int arr[], int size)
		{
			cout << "\n\n Array Elements \n [";
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << " ]\n";
		}
	// Find max element in array
	int findMax(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 (arr[low] > arr[mid])
		{
			return this->findMax(arr, low, mid - 1);
		}
		else if (arr[mid] > arr[high])
		{
			return this->findMax(arr, mid, high - 1);
		}
		else if (arr[high] > arr[mid])
		{
			return high;
		}
		else
		{
			// When have exists duplicate elements
			int a = this->findMax(arr, low, mid);
			int b = this->findMax(arr, mid + 1, high);
			if (arr[a] > arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	//  Handles the request of finding max element in array
	//  We assume that given array is sorted and rotated
	void maxElement(int arr[], int size)
	{
		int location = this->findMax(arr, 0, size - 1);
		cout << " Max Element " << arr[location] << " \n";
	}
};
int main()
{
	SearchElement work = SearchElement();
	//  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 , 1 , 9 , 10
	};
	int arr3[] = {
		2 , 2 , 5 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
	};
	//  Get the size
	int size = sizeof(arr1) / sizeof(arr1[0]);
	work.display(arr1, size);
	work.maxElement(arr1, size);
	//  Get the size
	size = sizeof(arr2) / sizeof(arr2[0]);
	work.display(arr2, size);
	//  Test Case B
	work.maxElement(arr2, size);
	//  Get the size
	size = sizeof(arr3) / sizeof(arr3[0]);
	work.display(arr3, size);
	//  Test Case C
	work.maxElement(arr3, size);
	return 0;
}

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5
// Include namespace system
using System;
/*
    C# program 
    Find maximum element in a sorted and rotated array
*/
public class SearchElement
{
	// Display array elements
	public void display(int[] arr, int size)
	{
		Console.Write("\n\n Array Elements \n [");
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write(" ]\n");
	}
	// Find max element in array
	public int findMax(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 (arr[low] > arr[mid])
		{
			return findMax(arr, low, mid - 1);
		}
		else if (arr[mid] > arr[high])
		{
			return findMax(arr, mid, high - 1);
		}
		else if (arr[high] > arr[mid])
		{
			return high;
		}
		else
		{
			// When have exists duplicate elements
			int a = findMax(arr, low, mid);
			int b = findMax(arr, mid + 1, high);
			if (arr[a] > arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	// Handles the request of finding max element in array
	// We assume that given array is sorted and rotated
	public void maxElement(int[] arr, int size)
	{
		int location = findMax(arr, 0, size - 1);
		Console.Write(" Max Element " + arr[location] + " \n");
	}
	public static void Main(String[] args)
	{
		SearchElement work = new SearchElement();
		// 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 , 1 , 9 , 10
		};
		int[] arr3 = {
			2 , 2 , 5 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2
		};
		//  Get the size
		int size = arr1.Length;
		work.display(arr1, size);
		work.maxElement(arr1, size);
		//  Get the size
		size = arr2.Length;
		work.display(arr2, size);
		//  Test Case B
		work.maxElement(arr2, size);
		//  Get the size
		size = arr3.Length;
		work.display(arr3, size);
		//  Test Case C
		work.maxElement(arr3, size);
	}
}

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5
<?php
/*
    Php program 
    Find maximum element in a sorted and rotated array
*/
class SearchElement
{
	// Display array elements
	public	function display( & $arr, $size)
	{
		echo "\n\n Array Elements \n [";
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo " ]\n";
	}
	// Find max element in array
	public	function findMax( & $arr, $low, $high)
	{
		if ($low > $high)
		{
			return 0;
		}
		if ($low == $high)
		{
			return $low;
		}
		// Find middle element
		$mid = $low + (intval(($high - $low) / 2));
		if ($arr[$low] > $arr[$mid])
		{
			return $this->findMax($arr, $low, $mid - 1);
		}
		else if ($arr[$mid] > $arr[$high])
		{
			return $this->findMax($arr, $mid, $high - 1);
		}
		else if ($arr[$high] > $arr[$mid])
		{
			return $high;
		}
		else
		{
			// When have exists duplicate elements
			$a = $this->findMax($arr, $low, $mid);
			$b = $this->findMax($arr, $mid + 1, $high);
			if ($arr[$a] > $arr[$b])
			{
				return $a;
			}
			else
			{
				return $b;
			}
		}
	}
	//  Handles the request of finding max element in array
	//  We assume that given array is sorted and rotated
	public	function maxElement( & $arr, $size)
	{
		$location = $this->findMax($arr, 0, $size - 1);
		echo " Max Element ". $arr[$location] ." \n";
	}
}

function main()
{
	$work = new SearchElement();
	//  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, 1, 9, 10);
	$arr3 = array(2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2);
	//  Get the size
	$size = count($arr1);
	$work->display($arr1, $size);
	$work->maxElement($arr1, $size);
	//  Get the size
	$size = count($arr2);
	$work->display($arr2, $size);
	//  Test Case B
	$work->maxElement($arr2, $size);
	//  Get the size
	$size = count($arr3);
	$work->display($arr3, $size);
	//  Test Case C
	$work->maxElement($arr3, $size);
}
main();

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5
/*
    Node Js program 
    Find maximum element in a sorted and rotated array
*/
class SearchElement
{
	// Display array elements
	display(arr, size)
	{
		process.stdout.write("\n\n Array Elements \n [");
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write(" ]\n");
	}
	// Find max element in array
	findMax(arr, low, high)
	{
		if (low > high)
		{
			return 0;
		}
		if (low == high)
		{
			return low;
		}
		// Find middle element
		var mid = low + (parseInt((high - low) / 2));
		if (arr[low] > arr[mid])
		{
			return this.findMax(arr, low, mid - 1);
		}
		else if (arr[mid] > arr[high])
		{
			return this.findMax(arr, mid, high - 1);
		}
		else if (arr[high] > arr[mid])
		{
			return high;
		}
		else
		{
			// When have exists duplicate elements
			var a = this.findMax(arr, low, mid);
			var b = this.findMax(arr, mid + 1, high);
			if (arr[a] > arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	//  Handles the request of finding max element in array
	//  We assume that given array is sorted and rotated
	maxElement(arr, size)
	{
		var location = this.findMax(arr, 0, size - 1);
		process.stdout.write(" Max Element " + arr[location] + " \n");
	}
}

function main()
{
	var work = new SearchElement();
	//  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, 1, 9, 10];
	var arr3 = [2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2];
	//  Get the size
	var size = arr1.length;
	work.display(arr1, size);
	work.maxElement(arr1, size);
	//  Get the size
	size = arr2.length;
	work.display(arr2, size);
	//  Test Case B
	work.maxElement(arr2, size);
	//  Get the size
	size = arr3.length;
	work.display(arr3, size);
	//  Test Case C
	work.maxElement(arr3, size);
}
main();

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5
#  Python 3 program 
#  Find maximum element in a sorted and rotated array

class SearchElement :
	#  Display array elements
	def display(self, arr, size) :
		print("\n\n Array Elements \n [", end = "")
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(" ]")
	
	#  Find max element in array
	def findMax(self, arr, low, high) :
		if (low > high) :
			return 0
		
		if (low == high) :
			return low
		
		#  Find middle element
		mid = low + (int((high - low) / 2))
		if (arr[low] > arr[mid]) :
			return self.findMax(arr, low, mid - 1)
		
		elif(arr[mid] > arr[high]) :
			return self.findMax(arr, mid, high - 1)
		
		elif(arr[high] > arr[mid]) :
			return high
		else :
			#  When have exists duplicate elements
			a = self.findMax(arr, low, mid)
			b = self.findMax(arr, mid + 1, high)
			if (arr[a] > arr[b]) :
				return a
			else :
				return b
			
		
	
	#   Handles the request of finding max element in array
	#   We assume that given array is sorted and rotated
	def maxElement(self, arr, size) :
		location = self.findMax(arr, 0, size - 1)
		print(" Max Element ", arr[location] ," ")
	

def main() :
	work = SearchElement()
	#   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, 1, 9, 10]
	arr3 = [2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2]
	#   Get the size
	size = len(arr1)
	work.display(arr1, size)
	work.maxElement(arr1, size)
	#   Get the size
	size = len(arr2)
	work.display(arr2, size)
	#   Test Case B
	work.maxElement(arr2, size)
	#   Get the size
	size = len(arr3)
	work.display(arr3, size)
	#   Test Case C
	work.maxElement(arr3, size)

if __name__ == "__main__": main()

Output

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


 Array Elements
 [   11   22   43   45   51   62   73   1   9   10 ]
 Max Element  73


 Array Elements
 [   2   2   5   2   2   2   2   2   2   2   2 ]
 Max Element  5
#  Ruby program 
#  Find maximum element in a sorted and rotated array

class SearchElement 
	#  Display array elements
	def display(arr, size) 
		print("\n\n Array Elements \n [")
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print(" ]\n")
	end

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

		if (low == high) 
			return low
		end

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

		end

	end

	#   Handles the request of finding max element in array
	#   We assume that given array is sorted and rotated
	def maxElement(arr, size) 
		location = self.findMax(arr, 0, size - 1)
		print(" Max Element ", arr[location] ," \n")
	end

end

def main() 
	work = SearchElement.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, 1, 9, 10]
	arr3 = [2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2]
	#   Get the size
	size = arr1.length
	work.display(arr1, size)
	#   Get the size
	work.maxElement(arr1, size)
	#   Get the size
	size = arr2.length
	work.display(arr2, size)
	#   Test Case 2
	work.maxElement(arr2, size)
	#   Get the size
	size = arr3.length
	work.display(arr3, size)
	#   Test Case 3
	work.maxElement(arr3, size)
end

main()

Output


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


 Array Elements 
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73 


 Array Elements 
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5 
/*
    Scala program 
    Find maximum element in a sorted and rotated array
*/
class SearchElement
{
	//  Display array elements
	def display(arr: Array[Int], size: Int): Unit = {
		print("\n\n Array Elements \n [");
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print(" ]\n");
	}
	//  Find max element in array
	def findMax(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).toInt);
		if (arr(low) > arr(mid))
		{
			return this.findMax(arr, low, mid - 1);
		}
		else if (arr(mid) > arr(high))
		{
			return this.findMax(arr, mid, high - 1);
		}
		else if (arr(high) > arr(mid))
		{
			return high;
		}
		else
		{
			//  When have exists duplicate elements
			var a: Int = this.findMax(arr, low, mid);
			var b: Int = this.findMax(arr, mid + 1, high);
			if (arr(a) > arr(b))
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	//   Handles the request of finding max element in array
	//   We assume that given array is sorted and rotated
	def maxElement(arr: Array[Int], size: Int): Unit = {
		var location: Int = this.findMax(arr, 0, size - 1);
		print(" Max Element " + arr(location) + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var work: SearchElement = new SearchElement();
		//   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, 1, 9, 10);
		var arr3: Array[Int] = Array(2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2);
		//   Get the size
		var size: Int = arr1.length;
		work.display(arr1, size);
		work.maxElement(arr1, size);
		//   Get the size
		size = arr2.length;
		work.display(arr2, size);
		//   Test Case B
		work.maxElement(arr2, size);
		//   Get the size
		size = arr3.length;
		work.display(arr3, size);
		//   Test Case C
		work.maxElement(arr3, size);
	}
}

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5
/*
    Swift 4 program 
    Find maximum element in a sorted and rotated array
*/
class SearchElement
{
	//  Display array elements
	func display(_ arr: [Int], _ size: Int)
	{
		print("\n\n Array Elements ", terminator: "\n [");
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(" ]");
	}
	//  Find max element in array
	func findMax(_ 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 (arr[low] > arr[mid])
		{
			return self.findMax(arr, low, mid - 1);
		}
		else if (arr[mid] > arr[high])
		{
			return self.findMax(arr, mid, high - 1);
		}
		else if (arr[high] > arr[mid])
		{
			return high;
		}
		else
		{
			//  When have exists duplicate elements
			let a: Int = self.findMax(arr, low, mid);
			let b: Int = self.findMax(arr, mid + 1, high);
			if (arr[a] > arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	//   Handles the request of finding max element in array
	//   We assume that given array is sorted and rotated
	func maxElement(_ arr: [Int], _ size: Int)
	{
		let location: Int = self.findMax(arr, 0, size - 1);
		print(" Max Element ", arr[location]," ");
	}
}
func main()
{
	let work: SearchElement = SearchElement();
	//   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, 1, 9, 10];
	let arr3: [Int] = [2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2];
	//   Get the size
	var size: Int = arr1.count;
	work.display(arr1, size);
	work.maxElement(arr1, size);
	//   Get the size
	size = arr2.count;
	work.display(arr2, size);
	//   Test Case B
	work.maxElement(arr2, size);
	//   Get the size
	size = arr3.count;
	work.display(arr3, size);
	//   Test Case C
	work.maxElement(arr3, size);
}
main();

Output

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


 Array Elements
 [   11   22   43   45   51   62   73   1   9   10 ]
 Max Element  73


 Array Elements
 [   2   2   5   2   2   2   2   2   2   2   2 ]
 Max Element  5
/*
    Kotlin program 
    Find maximum element in a sorted and rotated array
*/
class SearchElement
{
	//  Display array elements
	fun display(arr: Array<Int>, size: Int): Unit
	{
		print("\n\n Array Elements \n [");
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print(" ]\n");
	}
	//  Find max element in array
	fun findMax(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 (arr[low]>arr[mid])
		{
			return this.findMax(arr, low, mid - 1);
		}
		else
		if (arr[mid]>arr[high])
		{
			return this.findMax(arr, mid, high - 1);
		}
		else
		if (arr[high]>arr[mid])
		{
			return high;
		}
		else
		{
			//  When have exists duplicate elements
			var a: Int = this.findMax(arr, low, mid);
			var b: Int = this.findMax(arr, mid + 1, high);
			if (arr[a]>arr[b])
			{
				return a;
			}
			else
			{
				return b;
			}
		}
	}
	//   Handles the request of finding max element in array
	//   We assume that given array is sorted and rotated
	fun maxElement(arr: Array<Int>, size: Int): Unit
	{
		var location: Int = this.findMax(arr, 0, size - 1);
		print(" Max Element " + arr[location] + " \n");
	}
}
fun main(args: Array<String>): Unit
{
	var work: SearchElement = SearchElement();
	//   Defining sorted and rotated array of integer element
	var arr1: Array<Int> = arrayOf(7, 8, 9, 1, 2, 3, 4, 5, 6);
	var arr2: Array<Int> = arrayOf(11, 22, 43, 45, 51, 62, 73, 1, 9, 10);
	var arr3: Array<Int> = arrayOf(2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2);
	//   Get the size
	var size: Int = arr1.count();
	work.display(arr1, size);
	work.maxElement(arr1, size);
	//   Get the size
	size = arr2.count();
	work.display(arr2, size);
	//   Test Case B
	work.maxElement(arr2, size);
	//   Get the size
	size = arr3.count();
	work.display(arr3, size);
	//   Test Case C
	work.maxElement(arr3, size);
}

Output

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


 Array Elements
 [  11  22  43  45  51  62  73  1  9  10 ]
 Max Element 73


 Array Elements
 [  2  2  5  2  2  2  2  2  2  2  2 ]
 Max Element 5




Comment

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