Check if an array is rotated sorted order

Here given code implementation process.

//C Program 
//Check if an array is rotated sorted sequence
#include <stdio.h>

//Print array elements
void print_data(int arr[],int size)
{
  
  for(int i = 0; i < size; i++)
  {
    printf("%3d",arr[i] );
  }
  printf("\n");
}

void is_sorted_rotated(int arr[],int size)
{
  
  if(size<=1)
  {
    return;
  }

  int status = 1;

  int flag = 1;

  for (int i = 0; i < size-1 && status==1; ++i)
  {
    if(flag==1)
    {
      //Check whether array element is a valid incremented sequence?
      if(arr[i] > arr[i+1])
      {
        //When not 
        flag++;
      }
    }
    else if(flag==2)
    {
      // Check whether array element is a valid incremented sequence?
      // Also check that first element of array is greater than or equal to next upcoming 
      // array element
      if(arr[0] < arr[i] || arr[0] < arr[i+1] || arr[i] > arr[i+1])
      {
        //Not rotated and sorted
        status = 0;
      }
    }
  }
  print_data(arr,size);
  //Test Case
  if(flag==1)
  {
    printf(" No : Array is sorted but not rotated\n");
  }
  else if(status==1)
  {
    printf(" Yes : Array is form of an rotated and sorted order \n");
  }
  else
  {
    printf(" No : Array is not the form of a rotated and sorted sequence\n");
  }

}

int main()
{

    int arr1[] ={1,2,3,4,5,6};

    //Get the size of array
    int size=(sizeof(arr1)/sizeof(arr1[0]));
  
    is_sorted_rotated(arr1,size);

    int arr2[] ={15,17,18,1,4,6,8,9,12};

    //Get the size of array
    size=(sizeof(arr2)/sizeof(arr2[0]));

    is_sorted_rotated(arr2,size);


    int arr3[] ={6,7,8,1,3,9};

    //Get the size of array
    size=(sizeof(arr3)/sizeof(arr3[0]));
  
    is_sorted_rotated(arr3,size);
    return 0;
}

Output

  1  2  3  4  5  6
 No : Array is sorted but not rotated
 15 17 18  1  4  6  8  9 12
 Yes : Array is form of an rotated and sorted order
  6  7  8  1  3  9
 No : Array is not the form of a rotated and sorted sequence
/*
 C++ Program
 Check if an array is rotated sorted sequence
*/
#include<iostream>

using namespace std;

class MyArray {
	public:

		//Print array elements
		void print_data(int arr[], int size) {
			for (int i = 0; i < size; i++) {
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	void is_sorted_rotated(int arr[], int size) {
		if (size <= 1) {
			return;
		}
		bool status = true;
		int flag = 1;
		for (int i = 0; i < size - 1 && status == true; ++i) {
			if (flag == 1) {
				//Check whether array element is a valid incremented sequence?

				if (arr[i] > arr[i + 1]) {
					//When not 
					flag++;
				}
			} else
			if (flag == 2) {
				// Check whether array element is a valid incremented sequence?
				// Also check that first element of array is greater than or equal to next upcoming 
				// array element

				if (arr[0] < arr[i] || arr[0] < arr[i + 1] || arr[i] > arr[i + 1]) {
					//Not rotated and sorted
					status = false;
					flag++;
				}
			}
		}
		this->print_data(arr, size);
		//Test Case

		if (flag == 1) {
			cout << " No : Array is sorted but not rotated\n";
		} else
		if (status == true) {
			cout << " Yes : Array is form of an rotated and sorted order \n";
		} else {
			cout << " No : Array is not the form of a rotated and sorted sequence\n";
		}
	}
};
int main() {
	MyArray obj ;
	//Test Case
	//Define array elements
	int arr1[] = {
		1,
		2,
		3,
		4,
		5,
		6
	};
	//Get the size of array
	int size = sizeof(arr1) / sizeof(arr1[0]);
	obj.is_sorted_rotated(arr1, size);
	int arr2[] = {
		15,
		17,
		18,
		1,
		4,
		6,
		8,
		9,
		12
	};
	//Get the size of array
	size = sizeof(arr2) / sizeof(arr2[0]);
	obj.is_sorted_rotated(arr2, size);
	int arr3[] = {
		6,
		7,
		8,
		1,
		3,
		9
	};
	//Get the size of array
	size = sizeof(arr3) / sizeof(arr3[0]);
	obj.is_sorted_rotated(arr3, size);
	return 0;
}

Output

 1 2 3 4 5 6
 No : Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes : Array is form of an rotated and sorted order
 6 7 8 1 3 9
 No : Array is not the form of a rotated and sorted sequence
/*
  Java Program
  Check if an array is rotated sorted sequence
*/
public class MyArray {
  //Print array elements
  public void print_data(int []arr,int size)
  {
    
    for(int i = 0; i < size; i++)
    {
      System.out.print("  "+arr[i] );
    }
    System.out.print("\n");
  }

  public void is_sorted_rotated(int []arr,int size)
  {
    
    if(size<=1)
    {
      return;
    }

    boolean status = true;

    int flag = 1;

    for (int i = 0; i < size-1 && status==true; ++i)
    {
      if(flag==1)
      {
        //Check whether array element is a valid incremented sequence?
        if(arr[i] > arr[i+1])
        {
          //When not 
          flag++;
        }
      }
      else if(flag==2)
      {
        // Check whether array element is a valid incremented sequence?
        // Also check that first element of array is greater than or equal to next upcoming 
        // array element
        if(arr[0] < arr[i] || arr[0] < arr[i+1] || arr[i] > arr[i+1])
        {
          //Not rotated and sorted
          status = false;
          flag++;
        }
      }
    }
    print_data(arr,size);
    //Test Case
    if(flag==1)
    {
      System.out.print(" No : Array is sorted but not rotated\n");
    }
    else if(status==true)
    {
      System.out.print(" Yes : Array is form of an rotated and sorted order \n");
    }
    else
    {
      System.out.print(" No : Array is not the form of a rotated and sorted sequence\n");
    }

  }


  public static void main(String[] args) {

    MyArray obj = new MyArray();
    //Test Case
    //Define array elements
    int []arr1 ={1,2,3,4,5,6};

    //Get the size of array
    int size=arr1.length;
  
    obj.is_sorted_rotated(arr1,size);

    int []arr2 ={15,17,18,1,4,6,8,9,12};

    //Get the size of array
    size=arr2.length;

    obj.is_sorted_rotated(arr2,size);


    int []arr3 ={6,7,8,1,3,9};

    //Get the size of array
    size=arr3.length;
  
    obj.is_sorted_rotated(arr3,size);
  }
}

Output

 1 2 3 4 5 6
 No : Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes : Array is form of an rotated and sorted order
 6 7 8 1 3 9
 No : Array is not the form of a rotated and sorted sequence
/*
  C# Program
  Check if an array is rotated sorted sequence
*/
using System;
public class MyArray {
	//Print array elements
	public void print_data(int[] arr, int size) {

		for (int i = 0; i < size; i++) {
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}

	public void is_sorted_rotated(int[] arr, int size) {

		if (size <= 1) {
			return;
		}

		Boolean status = true;

		int flag = 1;

		for (int i = 0; i < size - 1 && status == true; ++i) {
			if (flag == 1) {
				//Check whether array element is a valid incremented sequence?
				if (arr[i] > arr[i + 1]) {
					//When not 
					flag++;
				}
			} else if (flag == 2) {
				// Check whether array element is a valid incremented sequence?
				// Also check that first element of array is greater than or equal to next upcoming 
				// array element
				if (arr[0] < arr[i] || arr[0] < arr[i + 1] || arr[i] > arr[i + 1]) {
					//Not rotated and sorted
					status = false;
					flag++;
				}
			}
		}
		print_data(arr, size);
		//Test Case
		if (flag == 1) {
			Console.Write(" No : Array is sorted but not rotated\n");
		} else if (status == true) {
			Console.Write(" Yes : Array is form of an rotated and sorted order \n");
		} else {
			Console.Write(" No : Array is not the form of a rotated and sorted sequence\n");
		}

	}


	public static void Main(String[] args) {

		MyArray obj = new MyArray();
		//Test Case
		//Define array elements
		int[] arr1 = {
			1,
			2,
			3,
			4,
			5,
			6
		};

		//Get the size of array
		int size = arr1.Length;

		obj.is_sorted_rotated(arr1, size);

		int[] arr2 = {
			15,
			17,
			18,
			1,
			4,
			6,
			8,
			9,
			12
		};

		//Get the size of array
		size = arr2.Length;

		obj.is_sorted_rotated(arr2, size);


		int[] arr3 = {
			6,
			7,
			8,
			1,
			3,
			9
		};

		//Get the size of array
		size = arr3.Length;

		obj.is_sorted_rotated(arr3, size);
	}
}

Output

  1  2  3  4  5  6
 No : Array is sorted but not rotated
  15  17  18  1  4  6  8  9  12
 Yes : Array is form of an rotated and sorted order
  6  7  8  1  3  9
 No : Array is not the form of a rotated and sorted sequence
# Python 3 Program
# Check if an array is rotated sorted sequence

class MyArray :
  # Print array elements
  def print_data(self, arr, size) :
    i = 0
    while (i < size) :
      print(" ", arr[i],end="")
      i += 1
    
    print(end="\n")
  
  def is_sorted_rotated(self, arr, size) :
    if (size <= 1) :
      return
    
    status = True
    flag = 1
    i = 0
    while (i < size - 1 and status == True) :
      if (flag == 1) :
        # Check whether array element is a valid incremented sequence?

        if (arr[i] > arr[i + 1]) :
          # When not 
          flag += 1
        
      elif (flag == 2) :
        #  array element
        #  Also check that first element of array is greater than or equal to next upcoming 

        if (arr[0] < arr[i] or arr[0] < arr[i + 1] or arr[i] > arr[i + 1]) :
          # Not rotated and sorted
          status = False
          flag += 1
        
      
      i += 1
    
    self.print_data(arr, size)
    # Test Case

    if (flag == 1) :
      print(" No : Array is sorted but not rotated")
    elif (status == True) :
      print(" Yes : Array is form of an rotated and sorted order ")
    else :
      print(" No : Array is not the form of a rotated and sorted sequence")
    
  

def main() :
  obj = MyArray()
  arr1 = [1, 2, 3, 4, 5, 6]
  # Get the size of array
  size = len(arr1)
  obj.is_sorted_rotated(arr1, size)
  arr2 = [15, 17, 18, 1, 4, 6, 8, 9, 12]
  # Get the size of array
  size = len(arr2)
  obj.is_sorted_rotated(arr2, size)
  arr3 = [6, 7, 8, 1, 3, 9]
  # Get the size of array
  size = len(arr3)
  obj.is_sorted_rotated(arr3, size)


if __name__ == "__main__":
  main()

Output

 1 2 3 4 5 6
 No : Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes : Array is form of an rotated and sorted order
 6 7 8 1 3 9
 No : Array is not the form of a rotated and sorted sequence
# Ruby Program 
# Check if an array is rotated sorted sequence
class MyArray 
	# Print array elements
	def print_data(arr, size) 
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
	def is_sorted_rotated(arr, size) 
		if (size <= 1) 
			return
		end
		status = true
		flag = 1
		i = 0
		while (i < size - 1 and status == true) 
			if (flag == 1) 
				# Check whether array element is a valid incremented sequence?

				if (arr[i] > arr[i + 1]) 
					# When not 
					flag += 1
				end
			elsif (flag == 2) 
				#  array element
				#  Also check that first element of array is greater than or equal to next upcoming 

				if (arr[0] < arr[i] or arr[0] < arr[i + 1] or arr[i] > arr[i + 1]) 
					# Not rotated and sorted
					status = false
					flag += 1
				end
			end
			i += 1
		end
		self.print_data(arr, size)
		# Test Case

		if (flag == 1) 
			print(" No  :Array is sorted but not rotated\n")
		elsif (status == true) 
			print(" Yes  :Array is form of an rotated and sorted order \n")
		else 
			print(" No  :Array is not the form of a rotated and sorted sequence\n")
		end
	end
end
def main() 
	obj = MyArray.new()
	arr1 = [1, 2, 3, 4, 5, 6]
	# Get the size of array
	size = arr1.length
	obj.is_sorted_rotated(arr1, size)
	arr2 = [15, 17, 18, 1, 4, 6, 8, 9, 12]
	# Get the size of array
	size = arr2.length
	obj.is_sorted_rotated(arr2, size)
	arr3 = [6, 7, 8, 1, 3, 9]
	# Get the size of array
	size = arr3.length
	obj.is_sorted_rotated(arr3, size)
end
main()

Output

 1 2 3 4 5 6
 No  :Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes  :Array is form of an rotated and sorted order 
 6 7 8 1 3 9
 No  :Array is not the form of a rotated and sorted sequence
/*
 Scala Program
 Check if an array is rotated sorted sequence
*/
class MyArray {
	//Print array elements
	def print_data(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size) {
			print(" " + arr(i));
			i += 1;
		}
		print("\n");
	}
	def is_sorted_rotated(arr: Array[Int], size: Int): Unit = {
		if (size <= 1) {
			return;
		}
		var status: Boolean = true;
		var flag: Int = 1;
		var i: Int = 0;
		while (i < size - 1 && status == true) {
			if (flag == 1) {
				//Check whether array element is a valid incremented sequence?

				if (arr(i) > arr(i + 1)) {
					//When not 
					flag += 1;
				}
			} else
			if (flag == 2) {
				// array element
				// Also check that first element of array is greater than or equal to next upcoming 

				if (arr(0) < arr(i) || arr(0) < arr(i + 1) || arr(i) > arr(i + 1)) {
					//Not rotated and sorted
					status = false;
					flag += 1;
				}
			}
			i += 1;
		}
		this.print_data(arr, size);
		//Test Case

		if (flag == 1) {
			print(" No : Array is sorted but not rotated\n");
		} else
		if (status == true) {
			print(" Yes : Array is form of an rotated and sorted order \n");
		} else {
			print(" No : Array is not the form of a rotated and sorted sequence\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyArray = new MyArray();
		var arr1: Array[Int] = Array(1, 2, 3, 4, 5, 6);
		//Get the size of array
		var size: Int = arr1.length;
  		obj.is_sorted_rotated(arr1, size);
		var arr2: Array[Int] = Array(15, 17, 18, 1, 4, 6, 8, 9, 12);
		//Get the size of array
		size = arr2.length;
 		obj.is_sorted_rotated(arr2, size);
		var arr3: Array[Int] = Array(6, 7, 8, 1, 3, 9);
		//Get the size of array
		size = arr3.length;
  		obj.is_sorted_rotated(arr3, size);
	}
}

Output

 1 2 3 4 5 6
 No : Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes : Array is form of an rotated and sorted order
 6 7 8 1 3 9
 No : Array is not the form of a rotated and sorted sequence
/*
  Swift 4 Program
 Check if an array is rotated sorted sequence
*/
class MyArray {
	//Print array elements
	func print_data(_ arr: [Int], _ size: Int) {
		var i: Int = 0;
		while (i < size) {
			print(" ", arr[i],terminator:"");
			i += 1;
		}
		print(terminator:"\n");
	}
	func is_sorted_rotated(_ arr: [Int], _ size: Int) {
		if (size <= 1) {
			return;
		}
		var status: Bool = true;
		var flag: Int = 1;
		var i: Int = 0;
		while (i < size - 1 && status == true) {
			if (flag == 1) {
				//Check whether array element is a valid incremented sequence?

				if (arr[i] > arr[i + 1]) {
					//When not 
					flag += 1;
				}
			} else
			if (flag == 2) {
				// array element
				// Also check that first element of array is greater than or equal to next upcoming 

				if (arr[0] < arr[i] || arr[0] < arr[i + 1] || arr[i] > arr[i + 1]) {
					//Not rotated and sorted
					status = false;
					flag += 1;
				}
			}
			i += 1;
		}
		self.print_data(arr, size);
		//Test Case

		if (flag == 1) {
			print(" No : Array is sorted but not rotated");
		} else
		if (status == true) {
			print(" Yes : Array is form of an rotated and sorted order ");
		} else {
			print(" No : Array is not the form of a rotated and sorted sequence");
		}
	}
}
func main() {
	let obj: MyArray = MyArray();
	let arr1: [Int] = [1, 2, 3, 4, 5, 6];
	//Get the size of array
	var size: Int = arr1.count;
	obj.is_sorted_rotated(arr1, size);
	let arr2: [Int] = [15, 17, 18, 1, 4, 6, 8, 9, 12];
	//Get the size of array
	size = arr2.count;
	obj.is_sorted_rotated(arr2, size);
	let arr3: [Int] = [6, 7, 8, 1, 3, 9];
	//Get the size of array
	size = arr3.count;
	obj.is_sorted_rotated(arr3, size);
}
main();

Output

  1  2  3  4  5  6
 No : Array is sorted but not rotated
  15  17  18  1  4  6  8  9  12
 Yes : Array is form of an rotated and sorted order
  6  7  8  1  3  9
 No : Array is not the form of a rotated and sorted sequence
<?php
/*
  Php Program
  Check if an array is rotated sorted sequence
*/
class MyArray {
	//Print array elements

	public 	function print_data($arr, $size) {
		for ($i = 0; $i < $size; $i++) {
			echo(" ". $arr[$i]);
		}
		echo("\n");
	}
	public 	function is_sorted_rotated($arr, $size) {
		if ($size <= 1) {
			return;
		}
		$status = true;
		$flag = 1;
		for ($i = 0; $i < $size - 1 && $status == true; ++$i) {
			if ($flag == 1) {
				//Check whether array element is a valid incremented sequence?

				if ($arr[$i] > $arr[$i + 1]) {
					//When not 
					$flag++;
				}
			} else
			if ($flag == 2) {
				// Check whether array element is a valid incremented sequence?
				// Also check that first element of array is greater than or equal to next upcoming 
				// array element

				if ($arr[0] < $arr[$i] || $arr[0] < $arr[$i + 1] || $arr[$i] > $arr[$i + 1]) {
					//Not rotated and sorted
					$status = false;
					$flag++;
				}
			}
		}
		$this->print_data($arr, $size);
		//Test Case

		if ($flag == 1) {
			echo(" No : Array is sorted but not rotated\n");
		} else
		if ($status == true) {
			echo(" Yes : Array is form of an rotated and sorted order \n");
		} else {
			echo(" No : Array is not the form of a rotated and sorted sequence\n");
		}
	}
};

function main() {
	$obj = new MyArray();
	//Test Case
	//Define array elements
	$arr1 = array(1, 2, 3, 4, 5, 6);
	//Get the size of array
	$size = count($arr1);
	$obj->is_sorted_rotated($arr1, $size);
	$arr2 = array(15, 17, 18, 1, 4, 6, 8, 9, 12);
	//Get the size of array
	$size = count($arr2);
	$obj->is_sorted_rotated($arr2, $size);
	$arr3 = array(6, 7, 8, 1, 3, 9);
	//Get the size of array
	$size = count($arr3);
	$obj->is_sorted_rotated($arr3, $size);
}
main();

Output

 1 2 3 4 5 6
 No : Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes : Array is form of an rotated and sorted order
 6 7 8 1 3 9
 No : Array is not the form of a rotated and sorted sequence
/*
 Node Js Program
 Check if an array is rotated sorted sequence
*/
class MyArray {
	//Print array elements
	print_data(arr, size) {
		for (var i = 0; i < size; i++) {
			process.stdout.write(" " + arr[i]);
		}
		process.stdout.write("\n");
	}
	is_sorted_rotated(arr, size) {
		if (size <= 1) {
			return;
		}
		var status = true;
		var flag = 1;
		for (var i = 0; i < size - 1 && status == true; ++i) {
			if (flag == 1) {
				//Check whether array element is a valid incremented sequence?

				if (arr[i] > arr[i + 1]) {
					//When not 
					flag++;
				}
			} else
			if (flag == 2) {
				// Check whether array element is a valid incremented sequence?
				// Also check that first element of array is greater than or equal to next upcoming 
				// array element

				if (arr[0] < arr[i] || arr[0] < arr[i + 1] || arr[i] > arr[i + 1]) {
					//Not rotated and sorted
					status = false;
					flag++;
				}
			}
		}
		this.print_data(arr, size);
		//Test Case

		if (flag == 1) {
			process.stdout.write(" No : Array is sorted but not rotated\n");
		} else
		if (status == true) {
			process.stdout.write(" Yes : Array is form of an rotated and sorted order \n");
		} else {
			process.stdout.write(" No : Array is not the form of a rotated and sorted sequence\n");
		}
	}
}

function main(args) {
	var obj = new MyArray();
	//Test Case
	//Define array elements
	var arr1 = [1, 2, 3, 4, 5, 6];
	//Get the size of array
	var size = arr1.length;
	obj.is_sorted_rotated(arr1, size);
	var arr2 = [15, 17, 18, 1, 4, 6, 8, 9, 12];
	//Get the size of array
	size = arr2.length;
	obj.is_sorted_rotated(arr2, size);
	var arr3 = [6, 7, 8, 1, 3, 9];
	//Get the size of array
	size = arr3.length;
	obj.is_sorted_rotated(arr3, size);
}
main();

Output

 1 2 3 4 5 6
 No : Array is sorted but not rotated
 15 17 18 1 4 6 8 9 12
 Yes : Array is form of an rotated and sorted order
 6 7 8 1 3 9
 No : Array is not the form of a rotated and sorted sequence


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