
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the number of times array is rotated in the sorted array by recursion using C#?
Find index of mid element (minimum element) Apply Binary Search on the subarray based on following conditions −
If number lies between start element and element at mid1 position.
Then find number in array start to mid-1 using binary search
Else if number lies between mid and last element, then find number in array mid to last element using binary search.
Example
using System; using System.Collections.Generic; using System.Text; using System.Linq; namespace ConsoleApplication{ public class Arrays{ public int FindNumberRotated(int[] array, int start, int end, int value){ if (start > end){ return -1; } int mid = (start + end) / 2; if (array[mid] == value){ return mid; } if (array[start] <= array[mid]){ if (value >= array[start] && value <= array[mid]){ return FindNumberRotated(array, start, mid - 1, value); } return FindNumberRotated(array, mid + 1, end, value); } if (value >= array[mid] && value <= array[end]){ return FindNumberRotated(array, mid + 1, end, value); } return FindNumberRotated(array, start, mid - 1, value); } } class Program{ static void Main(string[] args){ Arrays a = new Arrays(); int[] arr = { 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 }; int res = a.FindNumberRotated(arr, 0, arr.Length - 1, 1); Console.WriteLine(res); } } }
Output
8
- Related Articles
- Find the Rotation Count in Rotated Sorted array in C++
- Find Minimum in Rotated Sorted Array in C++
- Find Minimum in Rotated Sorted Array II in C++
- Search in Rotated Sorted Array in Python
- Search in Rotated Sorted Array II in C++
- Search in Rotated Sorted Array II in Python
- Check if an array is sorted and rotated in Python
- Check if an array is sorted and rotated in C++
- Program to check whether an array Is sorted and rotated in Python
- Find the only missing number in a sorted array using C++
- C++ program to search an element in a sorted rotated array
- Finding smallest element in a sorted array which is rotated in JavaScript
- Maximum element in a sorted and rotated array in C++
- Find the number of elements greater than k in a sorted array using C++
- How to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?

Advertisements