Tech Point Fundamentals

Tuesday, February 8, 2022

C# Program to Rotate an Array to a Pivot

C# Program to Rotate an Array to a Pivot

coding-interview-question-csharp

Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor. 


This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.


Watch our videos here





Question: 


How can you rotate an array to the right to a given pivot? Write a program to rotate an array.


Here we have to rotate the elements of the array towards its right by the specified number of times (pivot). An array is said to be right rotated if all elements of the array are moved to its right by one position. 








C# Program to Rotate an Array to a Pivot Element


       
 

// Author 	: Tech Point Fundamentals
// Website 	: www.techpointfunda.com
// Channel	: https://www.youtube.com/c/TechPointFundamentals
// Demo		: Rotating Array

using System;

public class Program
{
public static void Main()
{
	int[] array = new int[] {5, 4, 3, 2, 1, 6, 7, 8, 9};							
	int pivot = 3;
	
	Console.WriteLine("Input Array : ");	
	Console.WriteLine("------------------------------------------------ ");	
	
	for( int row = 0; row < array.Length; row++)
	{
		Console.WriteLine(array[row]);
	}	
	
	var rotatedArray =  RotateArray(array, pivot);	
	
	Console.WriteLine(String.Format("\n\nArray After Rotating to Pivote {0} : ", pivot));	
	Console.WriteLine("------------------------------------------------ ");	
	
	for( int row = 0; row < rotatedArray.Length; row++)
	{
		Console.WriteLine(rotatedArray[row]);
	}		
}		
	
public static int[] RotateArray(int[] array, int pivot)
{
  if (pivot < 0 || array == null)
	  throw new Exception("Invalid argument");

  pivot = pivot % array.Length;

  //Rotate first half
  array = RotateSubArray(array, 0, pivot - 1);

  //Rotate second half
  array = RotateSubArray(array, pivot, array.Length - 1);

  //Rotate all
  array = RotateSubArray(array, 0, array.Length - 1);

  return array;
}

private static int[] RotateSubArray(int[] subArray, int start, int end)
{
	while (start < end)
	{
		int temp = subArray[start];
		subArray[start] = subArray[end];
		subArray[end] = temp;
		start++;
		end--;
	}
	return subArray;
}

// Way 2
 public static int[] RotateArray2(int[] array, int pivot)
 {
	  if (pivot < 0 || array == null)
		  throw new Exception("Invalid argument");

	  for(int i = 0; i < pivot; i++)
	  {  
		int j, last;  
	  
		last = array[array.Length-1];  
	  
		for(j = array.Length-1; j > 0; j--)
		{               
			array[j] = array[j-1];  
		}  
	  
		array[0] = last;  
	}  
	return array;
  }
}





Output: 



               Input Array : ------------------------------------------------ 5 4 3 2 1 6 7 8 9 Array After Rotating to Pivote 3 : ------------------------------------------------ 2 1 6 7 8 9 5 4 3


Live Demo






No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.