Print K th right rotation of array elements

Here given code implementation process.

// C Program
// Print K th right rotation of array elements
#include <stdio.h>

// Display array elements
void display(int arr[],int size)
{

    printf("\n Array Elements \n");
    for (int i = 0; i < size; ++i)
    {
        printf("   %d",arr[i]);
    }
    printf("\n");

}
// Prints the right k rotation of an array
void rightRoationByK(int arr[], int size , int k)
{


    display(arr,size);

    printf(" Rotated right by %d element \n",k);

    int j = size - k ;

    if(j < 0)
    {
        // When rotation more then size of array
        j = -j;
    }

    for (int i = 0; i < size; ++i)
    {
        // display element
        printf("   %d", arr[(j+i)%(size)]);      
    }
   
}

int main()
{
   
    // Define array of integer elements
    int arr1[] = {8,3,1,6,5,4,2,7,9};

    int arr2[] = {1,2,3,4,5,6,7,8,9,10};

    // Get the size
    int size = sizeof(arr1)/sizeof(arr1[0]);

    // Test Case A (of arr1)
    rightRoationByK(arr1, size, 2);
    rightRoationByK(arr1, size, 3);

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

    // Test Case B (of arr2)
    rightRoationByK(arr2, size, 4);
    rightRoationByK(arr2, size, 13);
  return 0;
}

Output

 Array Elements
   8   3   1   6   5   4   2   7   9
 Rotated right by 2 element
   7   9   8   3   1   6   5   4   2
 Array Elements
   8   3   1   6   5   4   2   7   9
 Rotated right by 3 element
   2   7   9   8   3   1   6   5   4
 Array Elements
   1   2   3   4   5   6   7   8   9   10
 Rotated right by 4 element
   7   8   9   10   1   2   3   4   5   6
 Array Elements
   1   2   3   4   5   6   7   8   9   10
 Rotated right by 13 element
   4   5   6   7   8   9   10   1   2   3
/*
    Java program 
    Print K th right rotation of array elements
*/

public class RightRotation
{
    // Display array elements
    public void display(int[] arr, int size)
    {
        System.out.print("\n Array Elements \n");
        for (int i = 0; i < size; ++i)
        {
            System.out.print("  " + arr[i]  );
        }
        System.out.print("\n");
    }
    // Prints the right k rotation of an array
    public void rightRoationByK(int[] arr, int size, int k)
    {
        display(arr, size);

        System.out.print(" Rotated right by " + k + " element \n");
        
        
        int j = size - k;
        
        if (j < 0)
        {
            // When rotation more then size of array
            j = -j;
        }
        for (int i = 0; i < size; ++i)
        {
            // display element
            System.out.print("  " + arr[(j + i) % (size)]  );
        }
    }
    public static void main(String[] args)
    {
        RightRotation work = new RightRotation();
        // Define array of integer elements
        int[] arr1 = 
        {
            8 , 3 , 1 , 6 , 5 , 4 , 2 , 7 , 9
        };
        int[] arr2 =  
        {
            1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
        };
        // Get the size
        int size = arr1.length;
        // Test Case A (of arr1)
        work.rightRoationByK(arr1, size, 2);
        work.rightRoationByK(arr1, size, 3);
        // Get the size
        size = arr2.length;
        // Test Case B (of arr2)
        work.rightRoationByK(arr2, size, 4);
        work.rightRoationByK(arr2, size, 13);
    }
}

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program 
    Print K th right rotation of array elements
*/

class RightRotation
{
    public:
    //  Display array elements
    void display(int arr[], int size)
    {
        cout << "\n Array Elements \n";
        for (int i = 0; i < size; ++i)
        {
            cout << "  " << arr[i];
        }
        cout << "\n";
    }
    //  Prints the right k rotation of an array
    void rightRoationByK(int arr[], int size, int k)
    {
        this->display(arr, size);
        cout << " Rotated right by " << k << " element \n";
        int j = size - k;
        if (j < 0)
        {
            //  When rotation more then size of array
            j = -j;
        }
        for (int i = 0; i < size; ++i)
        {
            //  display element
            cout << "  " << arr[(j + i) % (size)];
        }
    }
};
int main()
{
    RightRotation work = RightRotation();
    //  Define array of integer elements
    int arr1[] = {
        8 , 3 , 1 , 6 , 5 , 4 , 2 , 7 , 9
    };
    int arr2[] = {
        1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
    };
    //  Get the size
    int size = sizeof(arr1) / sizeof(arr1[0]);
    //  Test Case A (of arr1)
    work.rightRoationByK(arr1, size, 2);
    work.rightRoationByK(arr1, size, 3);
    //  Get the size
    size = sizeof(arr2) / sizeof(arr2[0]);
    //  Test Case B (of arr2)
    work.rightRoationByK(arr2, size, 4);
    work.rightRoationByK(arr2, size, 13);
    return 0;
}

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3
// Include namespace system
using System;
/*
    C# program 
    Print K th right rotation of array elements
*/
public class RightRotation
{
	//  Display array elements
	public void display(int[] arr, int size)
	{
		Console.Write("\n Array Elements \n");
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	//  Prints the right k rotation of an array
	public void rightRoationByK(int[] arr, int size, int k)
	{
		display(arr, size);
		Console.Write(" Rotated right by " + k + " element \n");
		int j = size - k;
		if (j < 0)
		{
			//  When rotation more then size of array
			j = -j;
		}
		for (int i = 0; i < size; ++i)
		{
			//  display element
			Console.Write("  " + arr[(j + i) % (size)]);
		}
	}
	public static void Main(String[] args)
	{
		RightRotation work = new RightRotation();
		//  Define array of integer elements
		int[] arr1 = {
			8 , 3 , 1 , 6 , 5 , 4 , 2 , 7 , 9
		};
		int[] arr2 = {
			1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
		};
		//  Get the size
		int size = arr1.Length;
		//  Test Case A (of arr1)
		work.rightRoationByK(arr1, size, 2);
		work.rightRoationByK(arr1, size, 3);
		//  Get the size
		size = arr2.Length;
		//  Test Case B (of arr2)
		work.rightRoationByK(arr2, size, 4);
		work.rightRoationByK(arr2, size, 13);
	}
}

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3
/*
    Node Js program 
    Print K th right rotation of array elements
*/
class RightRotation
{
	//  Display array elements
	display(arr, size)
	{
		process.stdout.write("\n Array Elements \n");
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	//  Prints the right k rotation of an array
	rightRoationByK(arr, size, k)
	{
		this.display(arr, size);
		process.stdout.write(" Rotated right by " + k + " element \n");
		var j = size - k;
		if (j < 0)
		{
			//  When rotation more then size of array
			j = -j;
		}
		for (var i = 0; i < size; ++i)
		{
			//  display element
			process.stdout.write("  " + arr[(j + i) % (size)]);
		}
	}
}

function main()
{
	var work = new RightRotation();
	//  Define array of integer elements
	var arr1 = [8, 3, 1, 6, 5, 4, 2, 7, 9];
	var arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	//  Get the size
	var size = arr1.length;
	//  Test Case A (of arr1)
	work.rightRoationByK(arr1, size, 2);
	work.rightRoationByK(arr1, size, 3);
	//  Get the size
	size = arr2.length;
	//  Test Case B (of arr2)
	work.rightRoationByK(arr2, size, 4);
	work.rightRoationByK(arr2, size, 13);
}
main();

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3
<?php
/*
    Php program 
    Print K th right rotation of array elements
*/
class RightRotation
{
	//  Display array elements
	public	function display( & $arr, $size)
	{
		echo "\n Array Elements \n";
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	//  Prints the right k rotation of an array
	public	function rightRoationByK( & $arr, $size, $k)
	{
		$this->display($arr, $size);
		echo " Rotated right by ". $k ." element \n";
		$j = $size - $k;
		if ($j < 0)
		{
			//  When rotation more then size of array
			$j = -$j;
		}
		for ($i = 0; $i < $size; ++$i)
		{
			//  display element
			echo "  ". $arr[($j + $i) % ($size)];
		}
	}
}

function main()
{
	$work = new RightRotation();
	//  Define array of integer elements
	$arr1 = array(8, 3, 1, 6, 5, 4, 2, 7, 9);
	$arr2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
	//  Get the size
	$size = count($arr1);
	//  Test Case A (of arr1)
	$work->rightRoationByK($arr1, $size, 2);
	$work->rightRoationByK($arr1, $size, 3);
	//  Get the size
	$size = count($arr2);
	//  Test Case B (of arr2)
	$work->rightRoationByK($arr2, $size, 4);
	$work->rightRoationByK($arr2, $size, 13);
}
main();

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3
#  Python 3 program 
#  Print K th right rotation of array elements

class RightRotation :
	#   Display array elements
	def display(self, arr, size) :
		print("\n Array Elements ")
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#   Prints the right k rotation of an array
	def rightRoationByK(self, arr, size, k) :
		self.display(arr, size)
		print(" Rotated right by ", k ," element ")
		j = size - k
		if (j < 0) :
			#   When rotation more then size of array
			j = -j
		
		i = 0
		while (i < size) :
			#   display element
			print("  ", arr[(j + i) % (size)], end = "")
			i += 1
		
	

def main() :
	work = RightRotation()
	#   Define array of integer elements
	arr1 = [8, 3, 1, 6, 5, 4, 2, 7, 9]
	arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	#   Get the size
	size = len(arr1)
	#   Test Case A (of arr1)
	work.rightRoationByK(arr1, size, 2)
	work.rightRoationByK(arr1, size, 3)
	#   Get the size
	size = len(arr2)
	#   Test Case B (of arr2)
	work.rightRoationByK(arr2, size, 4)
	work.rightRoationByK(arr2, size, 13)

if __name__ == "__main__": main()

Output

 Array Elements
   8   3   1   6   5   4   2   7   9
 Rotated right by  2  element
   7   9   8   3   1   6   5   4   2
 Array Elements
   8   3   1   6   5   4   2   7   9
 Rotated right by  3  element
   2   7   9   8   3   1   6   5   4
 Array Elements
   1   2   3   4   5   6   7   8   9   10
 Rotated right by  4  element
   7   8   9   10   1   2   3   4   5   6
 Array Elements
   1   2   3   4   5   6   7   8   9   10
 Rotated right by  13  element
   4   5   6   7   8   9   10   1   2   3
#  Ruby program 
#  Print K th right rotation of array elements

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

		print("\n")
	end

	#   Prints the right k rotation of an array
	def rightRoationByK(arr, size, k) 
		self.display(arr, size)
		print(" Rotated right by ", k ," element \n")
		j = size - k
		if (j < 0) 
			#   When rotation more then size of array
			j = -j
		end

		i = 0
		while (i < size) 
			#   display element
			print("  ", arr[(j + i) % (size)])
			i += 1
		end

	end

end

def main() 
	work = RightRotation.new()
	#   Define array of integer elements
	arr1 = [8, 3, 1, 6, 5, 4, 2, 7, 9]
	arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	#   Get the size
	size = arr1.length
	#   Test Case A (of arr1)
	work.rightRoationByK(arr1, size, 2)
	work.rightRoationByK(arr1, size, 3)
	#   Get the size
	size = arr2.length
	#   Test Case B (of arr2)
	work.rightRoationByK(arr2, size, 4)
	work.rightRoationByK(arr2, size, 13)
end

main()

Output

 Array Elements 
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element 
  7  9  8  3  1  6  5  4  2
 Array Elements 
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element 
  2  7  9  8  3  1  6  5  4
 Array Elements 
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element 
  7  8  9  10  1  2  3  4  5  6
 Array Elements 
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element 
  4  5  6  7  8  9  10  1  2  3
/*
    Scala program 
    Print K th right rotation of array elements
*/
class RightRotation
{
	//   Display array elements
	def display(arr: Array[Int], size: Int): Unit = {
		print("\n Array Elements \n");
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	//   Prints the right k rotation of an array
	def rightRoationByK(arr: Array[Int], size: Int, k: Int): Unit = {
		this.display(arr, size);
		print(" Rotated right by " + k + " element \n");
		var j: Int = size - k;
		if (j < 0)
		{
			//   When rotation more then size of array
			j = -j;
		}
		var i: Int = 0;
		while (i < size)
		{
			//   display element
			print("  " + arr((j + i) % (size)));
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var work: RightRotation = new RightRotation();
		//   Define array of integer elements
		var arr1: Array[Int] = Array(8, 3, 1, 6, 5, 4, 2, 7, 9);
		var arr2: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		//   Get the size
		var size: Int = arr1.length;
		//   Test Case A (of arr1)
		work.rightRoationByK(arr1, size, 2);
		work.rightRoationByK(arr1, size, 3);
		//   Get the size
		size = arr2.length;
		//   Test Case B (of arr2)
		work.rightRoationByK(arr2, size, 4);
		work.rightRoationByK(arr2, size, 13);
	}
}

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3
/*
    Swift 4 program 
    Print K th right rotation of array elements
*/
class RightRotation
{
	//   Display array elements
	func display(_ arr: [Int], _ size: Int)
	{
		print("\n Array Elements ");
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	//   Prints the right k rotation of an array
	func rightRoationByK(_ arr: [Int], _ size: Int, _ k: Int)
	{
		self.display(arr, size);
		print(" Rotated right by ", k ," element ");
		var j: Int = size - k;
		if (j < 0)
		{
			//   When rotation more then size of array
			j = -j;
		}
		var i: Int = 0;
		while (i < size)
		{
			//   display element
			print("  ", arr[(j + i) % (size)], terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let work: RightRotation = RightRotation();
	//   Define array of integer elements
	let arr1: [Int] = [8, 3, 1, 6, 5, 4, 2, 7, 9];
	let arr2: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	//   Get the size
	var size: Int = arr1.count;
	//   Test Case A (of arr1)
	work.rightRoationByK(arr1, size, 2);
	work.rightRoationByK(arr1, size, 3);
	//   Get the size
	size = arr2.count;
	//   Test Case B (of arr2)
	work.rightRoationByK(arr2, size, 4);
	work.rightRoationByK(arr2, size, 13);
}
main();

Output

 Array Elements
   8   3   1   6   5   4   2   7   9
 Rotated right by  2  element
   7   9   8   3   1   6   5   4   2
 Array Elements
   8   3   1   6   5   4   2   7   9
 Rotated right by  3  element
   2   7   9   8   3   1   6   5   4
 Array Elements
   1   2   3   4   5   6   7   8   9   10
 Rotated right by  4  element
   7   8   9   10   1   2   3   4   5   6
 Array Elements
   1   2   3   4   5   6   7   8   9   10
 Rotated right by  13  element
   4   5   6   7   8   9   10   1   2   3
/*
    Kotlin program 
    Print K th right rotation of array elements
*/
class RightRotation
{
	// Display array elements
	fun display(arr: Array<Int>, size: Int): Unit
	{
		print("\n Array Elements \n");
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	//   Prints the right k rotation of an array
	fun rightRoationByK(arr: Array<Int>, size: Int, k: Int): Unit
	{
		this.display(arr, size);
		print(" Rotated right by " + k + " element \n");
		var j: Int = size - k;
		if (j < 0)
		{
			//   When rotation more then size of array
			j = -j;
		}
		var i: Int = 0;
		while (i < size)
		{
			//   display element
			print("  " + arr[(j + i) % (size)]);
			i += 1;
		}
	}
}
fun main(args: Array<String>): Unit
{
	var work: RightRotation = RightRotation();
	//   Define array of integer elements
	var arr1: Array<Int> = arrayOf(8, 3, 1, 6, 5, 4, 2, 7, 9);
	var arr2: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
	//   Get the size
	var size: Int = arr1.count();
	//   Test Case A (of arr1)
	work.rightRoationByK(arr1, size, 2);
	work.rightRoationByK(arr1, size, 3);
	//   Get the size
	size = arr2.count();
	//   Test Case B (of arr2)
	work.rightRoationByK(arr2, size, 4);
	work.rightRoationByK(arr2, size, 13);
}

Output

 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 2 element
  7  9  8  3  1  6  5  4  2
 Array Elements
  8  3  1  6  5  4  2  7  9
 Rotated right by 3 element
  2  7  9  8  3  1  6  5  4
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 4 element
  7  8  9  10  1  2  3  4  5  6
 Array Elements
  1  2  3  4  5  6  7  8  9  10
 Rotated right by 13 element
  4  5  6  7  8  9  10  1  2  3


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