Print all left rotations of given array

Here given code implementation process.

// C Program
// Print all left rotations of array
#include <stdio.h>

// Print all the left rotations in array
void leftRoation(int arr[],int size)
{
    // Loop controlling variables
    int i = 0;
    int j = 0;
    int k = 0;


    printf("  Array \n ");
    for (i = 0; i < size; ++i)
    {
        // Display element
        printf("  %d",arr[i]);
    }
    printf("\n  All Left Rotation \n");
    // Outer loop
    for ( i = 1; i < size; ++i)
    {
        k = i;

        // Inner loop executes n times
        for ( j = 0; j < size; ++j)
        {  
            if(k >= size)
            {
                // When k is exceed limit of last element then start of first element
                k = 0;
            }    
            // Display element
            printf("  %d",arr[k]);
            
            k++;
        }
        printf("\n");
    }

    printf("\n");


}

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

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

    //Test Case 1
    leftRoation(arr1,size);

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

    //Test Case 2
    leftRoation(arr2,size);
  return 0;
}

Output

  Array
   1  2  3  4  5  6  7  8
  All Left Rotation
  2  3  4  5  6  7  8  1
  3  4  5  6  7  8  1  2
  4  5  6  7  8  1  2  3
  5  6  7  8  1  2  3  4
  6  7  8  1  2  3  4  5
  7  8  1  2  3  4  5  6
  8  1  2  3  4  5  6  7

  Array
   6  8  9  5  3  3  2
  All Left Rotation
  8  9  5  3  3  2  6
  9  5  3  3  2  6  8
  5  3  3  2  6  8  9
  3  3  2  6  8  9  5
  3  2  6  8  9  5  3
  2  6  8  9  5  3  3
/*
    Java program 
    Print all left rotation of array
*/

public class MyMatrix
{
    // Print all the left rotations in array
    public void leftRoation(int[] arr, int size)
    {
        // Loop controlling variables
        int i = 0;
        int j = 0;
        int k = 0;
        System.out.print("  Array \n ");
        for (i = 0; i < size; ++i)
        {
            // Display element
            System.out.print("   " + arr[i] );
        }
        System.out.print("\n  All Left Rotation \n");
        // Outer loop
        for (i = 1; i < size; ++i)
        {
            k = i;
            // Inner loop executes n times
            for (j = 0; j < size; ++j)
            {
                if (k >= size)
                {
                    // When k is exceed limit of last element then start of first element
                    k = 0;
                }
                // Display element
                System.out.print("   " + arr[k] );
                k++;
            }
            System.out.print("\n");
        }
        System.out.print("\n");
    }
    public static void main(String[] args)
    {
        MyMatrix obj = new MyMatrix();
        // Define array of integer elements
        int[] arr1 = 
        {
            1 , 2 , 3 , 4 , 5 , 6 , 7 , 8
        };
        int[] arr2 = 
        {
            6 , 8 , 9 , 5 , 3 , 3 , 2
        };
        //Get the size
        int size = arr1.length;
        //Test Case 1
        obj.leftRoation(arr1, size);
        //Get the size
        size = arr2.length;
        //Test Case 2
        obj.leftRoation(arr2, size);
    }
}

Output

  Array
    1   2   3   4   5   6   7   8
  All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

  Array
    6   8   9   5   3   3   2
  All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program 
    Print all left rotation of array
*/
class MyMatrix
{
	public:
		//  Print all the left rotations in array
		void leftRoation(int arr[], int size)
		{
			//  Loop controlling variables
			int i = 0;
			int j = 0;
			int k = 0;
			cout << "   Array \n ";
			for (i = 0; i < size; ++i)
			{
				//  Display element
				cout << "   " << arr[i];
			}
			cout << "\n   All Left Rotation \n";
			//  Outer loop
			for (i = 1; i < size; ++i)
			{
				k = i;
				//  Inner loop executes n times
				for (j = 0; j < size; ++j)
				{
					if (k >= size)
					{
						//  When k is exceed limit of last element then start of first element
						k = 0;
					}
					//  Display element
					cout << "   " << arr[k];
					k++;
				}
				cout << "\n";
			}
			cout << "\n";
		}
};
int main()
{
	MyMatrix obj = MyMatrix();
	//  Define array of integer elements
	int arr1[] = {
		1 , 2 , 3 , 4 , 5 , 6 , 7 , 8
	};
	int arr2[] = {
		6 , 8 , 9 , 5 , 3 , 3 , 2
	};
	// Get the size
	int size = sizeof(arr1) / sizeof(arr1[0]);
	// Test Case 1
	obj.leftRoation(arr1, size);
	// Get the size
	size = sizeof(arr2) / sizeof(arr2[0]);
	// Test Case 2
	obj.leftRoation(arr2, size);
	return 0;
}

Output

   Array
    1   2   3   4   5   6   7   8
   All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array
    6   8   9   5   3   3   2
   All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3
// Include namespace system
using System;
/*
    C# program 
    Print all left rotation of array
*/
public class MyMatrix
{
	//  Print all the left rotations in array
	public void leftRoation(int[] arr, int size)
	{
		//  Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		Console.Write("   Array \n ");
		for (i = 0; i < size; ++i)
		{
			//  Display element
			Console.Write("   " + arr[i]);
		}
		Console.Write("\n   All Left Rotation \n");
		//  Outer loop
		for (i = 1; i < size; ++i)
		{
			k = i;
			//  Inner loop executes n times
			for (j = 0; j < size; ++j)
			{
				if (k >= size)
				{
					//  When k is exceed limit of last element then start of first element
					k = 0;
				}
				//  Display element
				Console.Write("   " + arr[k]);
				k++;
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyMatrix obj = new MyMatrix();
		//  Define array of integer elements
		int[] arr1 = {
			1 , 2 , 3 , 4 , 5 , 6 , 7 , 8
		};
		int[] arr2 = {
			6 , 8 , 9 , 5 , 3 , 3 , 2
		};
		// Get the size
		int size = arr1.Length;
		// Test Case 1
		obj.leftRoation(arr1, size);
		// Get the size
		size = arr2.Length;
		// Test Case 2
		obj.leftRoation(arr2, size);
	}
}

Output

   Array
    1   2   3   4   5   6   7   8
   All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array
    6   8   9   5   3   3   2
   All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3
<?php
/*
    Php program 
    Print all left rotation of array
*/
class MyMatrix
{
	//  Print all the left rotations in array
	public	function leftRoation( & $arr, $size)
	{
		//  Loop controlling variables
		$i = 0;
		$j = 0;
		$k = 0;
		echo "   Array \n ";
		for ($i = 0; $i < $size; ++$i)
		{
			//  Display element
			echo "   ". $arr[$i];
		}
		echo "\n   All Left Rotation \n";
		//  Outer loop
		for ($i = 1; $i < $size; ++$i)
		{
			$k = $i;
			//  Inner loop executes n times
			for ($j = 0; $j < $size; ++$j)
			{
				if ($k >= $size)
				{
					//  When k is exceed limit of last element then start of first element
					$k = 0;
				}
				//  Display element
				echo "   ". $arr[$k];
				$k++;
			}
			echo "\n";
		}
		echo "\n";
	}
}

function main()
{
	$obj = new MyMatrix();
	//  Define array of integer elements
	$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8);
	$arr2 = array(6, 8, 9, 5, 3, 3, 2);
	// Get the size
	$size = count($arr1);
	// Test Case 1
	$obj->leftRoation($arr1, $size);
	// Get the size
	$size = count($arr2);
	// Test Case 2
	$obj->leftRoation($arr2, $size);
}
main();

Output

   Array
    1   2   3   4   5   6   7   8
   All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array
    6   8   9   5   3   3   2
   All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3
/*
    Node Js program 
    Print all left rotation of array
*/
class MyMatrix
{
	//  Print all the left rotations in array
	leftRoation(arr, size)
	{
		//  Loop controlling variables
		var i = 0;
		var j = 0;
		var k = 0;
		process.stdout.write("   Array \n ");
		for (i = 0; i < size; ++i)
		{
			//  Display element
			process.stdout.write("   " + arr[i]);
		}
		process.stdout.write("\n   All Left Rotation \n");
		//  Outer loop
		for (i = 1; i < size; ++i)
		{
			k = i;
			//  Inner loop executes n times
			for (j = 0; j < size; ++j)
			{
				if (k >= size)
				{
					//  When k is exceed limit of last element then start of first element
					k = 0;
				}
				//  Display element
				process.stdout.write("   " + arr[k]);
				k++;
			}
			process.stdout.write("\n");
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var obj = new MyMatrix();
	//  Define array of integer elements
	var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
	var arr2 = [6, 8, 9, 5, 3, 3, 2];
	// Get the size
	var size = arr1.length;
	// Test Case 1
	obj.leftRoation(arr1, size);
	// Get the size
	size = arr2.length;
	// Test Case 2
	obj.leftRoation(arr2, size);
}
main();

Output

   Array
    1   2   3   4   5   6   7   8
   All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array
    6   8   9   5   3   3   2
   All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3
#  Python 3 program 
#  Print all left rotation of array

class MyMatrix :
	#   Print all the left rotations in array
	def leftRoation(self, arr, size) :
		#   Loop controlling variables
		i = 0
		j = 0
		k = 0
		print("   Array \n ", end = "")
		while (i < size) :
			#   Display element
			print("   ", arr[i], end = "")
			i += 1
		
		print("\n   All Left Rotation ")
		#   Outer loop
		i = 1
		while (i < size) :
			k = i
			#   Inner loop executes n times
			j = 0
			while (j < size) :
				if (k >= size) :
					#   When k is exceed limit of last element then start of first element
					k = 0
				
				#   Display element
				print("   ", arr[k], end = "")
				k += 1
				j += 1
			
			print(end = "\n")
			i += 1
		
		print(end = "\n")
	

def main() :
	obj = MyMatrix()
	#   Define array of integer elements
	arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
	arr2 = [6, 8, 9, 5, 3, 3, 2]
	#  Get the size
	size = len(arr1)
	#  Test Case 1
	obj.leftRoation(arr1, size)
	#  Get the size
	size = len(arr2)
	#  Test Case 2
	obj.leftRoation(arr2, size)

if __name__ == "__main__": main()

Output

   Array
     1    2    3    4    5    6    7    8
   All Left Rotation
    2    3    4    5    6    7    8    1
    3    4    5    6    7    8    1    2
    4    5    6    7    8    1    2    3
    5    6    7    8    1    2    3    4
    6    7    8    1    2    3    4    5
    7    8    1    2    3    4    5    6
    8    1    2    3    4    5    6    7

   Array
     6    8    9    5    3    3    2
   All Left Rotation
    8    9    5    3    3    2    6
    9    5    3    3    2    6    8
    5    3    3    2    6    8    9
    3    3    2    6    8    9    5
    3    2    6    8    9    5    3
    2    6    8    9    5    3    3
#  Ruby program 
#  Print all left rotation of array

class MyMatrix 
	#   Print all the left rotations in array
	def leftRoation(arr, size) 
		#   Loop controlling variables
		i = 0
		j = 0
		k = 0
		print("   Array \n ")
		while (i < size) 
			#   Display element
			print("   ", arr[i])
			i += 1
		end

		print("\n   All Left Rotation \n")
		#   Outer loop
		i = 1
		while (i < size) 
			k = i
			#   Inner loop executes n times
			j = 0
			while (j < size) 
				if (k >= size) 
					#   When k is exceed limit of last element then start of first element
					k = 0
				end

				#   Display element
				print("   ", arr[k])
				k += 1
				j += 1
			end

			print("\n")
			i += 1
		end

		print("\n")
	end

end

def main() 
	obj = MyMatrix.new()
	#   Define array of integer elements
	arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
	arr2 = [6, 8, 9, 5, 3, 3, 2]
	#  Get the size
	size = arr1.length
	#  Test Case 1
	obj.leftRoation(arr1, size)
	#  Get the size
	size = arr2.length
	#  Test Case 2
	obj.leftRoation(arr2, size)
end

main()

Output

   Array 
    1   2   3   4   5   6   7   8
   All Left Rotation 
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array 
    6   8   9   5   3   3   2
   All Left Rotation 
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3

/*
    Scala program 
    Print all left rotation of array
*/
class MyMatrix
{
	//   Print all the left rotations in array
	def leftRoation(arr: Array[Int], size: Int): Unit = {
		//   Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		print("   Array \n ");
		while (i < size)
		{
			//   Display element
			print("   " + arr(i));
			i += 1;
		}
		print("\n   All Left Rotation \n");
		//   Outer loop
		i = 1;
		while (i < size)
		{
			k = i;
			//   Inner loop executes n times
			j = 0;
			while (j < size)
			{
				if (k >= size)
				{
					//   When k is exceed limit of last element then start of first element
					k = 0;
				}
				//   Display element
				print("   " + arr(k));
				k += 1;
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMatrix = new MyMatrix();
		//   Define array of integer elements
		var arr1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8);
		var arr2: Array[Int] = Array(6, 8, 9, 5, 3, 3, 2);
		//  Get the size
		var size: Int = arr1.length;
		//  Test Case 1
		obj.leftRoation(arr1, size);
		//  Get the size
		size = arr2.length;
		//  Test Case 2
		obj.leftRoation(arr2, size);
	}
}

Output

   Array
    1   2   3   4   5   6   7   8
   All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array
    6   8   9   5   3   3   2
   All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   3
/*
    Swift 4 program 
    Print all left rotation of array
*/
class MyMatrix
{
	//   Print all the left rotations in array
	func leftRoation(_ arr: [Int], _ size: Int)
	{
		//   Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		print("   Array \n ", terminator: "");
		while (i < size)
		{
			//   Display element
			print("   ", arr[i], terminator: "");
			i += 1;
		}
		print("\n   All Left Rotation ");
		//   Outer loop
		i = 1;
		while (i < size)
		{
			k = i;
			//   Inner loop executes n times
			j = 0;
			while (j < size)
			{
				if (k >= size)
				{
					//   When k is exceed limit of last element then start of first element
					k = 0;
				}
				//   Display element
				print("   ", arr[k], terminator: "");
				k += 1;
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let obj: MyMatrix = MyMatrix();
	//   Define array of integer elements
	let arr1: [Int] = [1, 2, 3, 4, 5, 6, 7, 8];
	let arr2: [Int] = [6, 8, 9, 5, 3, 3, 2];
	//  Get the size
	var size: Int = arr1.count;
	//  Test Case 1
	obj.leftRoation(arr1, size);
	//  Get the size
	size = arr2.count;
	//  Test Case 2
	obj.leftRoation(arr2, size);
}
main();

Output

   Array
     1    2    3    4    5    6    7    8
   All Left Rotation
    2    3    4    5    6    7    8    1
    3    4    5    6    7    8    1    2
    4    5    6    7    8    1    2    3
    5    6    7    8    1    2    3    4
    6    7    8    1    2    3    4    5
    7    8    1    2    3    4    5    6
    8    1    2    3    4    5    6    7

   Array
     6    8    9    5    3    3    2
   All Left Rotation
    8    9    5    3    3    2    6
    9    5    3    3    2    6    8
    5    3    3    2    6    8    9
    3    3    2    6    8    9    5
    3    2    6    8    9    5    3
    2    6    8    9    5    3    3
/*
    Kotlin program 
    Print all left rotation of array
*/
class MyMatrix
{
	//   Print all the left rotations in array
	fun leftRoation(arr: Array<Int>, size: Int): Unit
	{
		//   Loop controlling variables
		var i: Int = 0;
		var j: Int ;
		var k: Int ;
		print("   Array \n ");
		while (i < size)
		{
			//   Display element
			print("   " + arr[i]);
			i += 1;
		}
		print("\n   All Left Rotation \n");
		//   Outer loop
		i = 1;
		while (i<size)
		{
			k = i;
			//   Inner loop executes n times
			j = 0;
			while (j<size)
			{
				if (k>= size)
				{
					//   When k is exceed limit of last element then start of first element
					k = 0;
				}
				//   Display element
				print("   " + arr[k]);
				k += 1;
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
fun main(args: Array<String>): Unit
{
	var obj: MyMatrix = MyMatrix();
	//   Define array of integer elements
	var arr1: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8);
	var arr2: Array<Int> = arrayOf(6, 8, 9, 5, 3, 3, 2);
	//  Get the size
	var size: Int = arr1.count();
	//  Test Case 1
	obj.leftRoation(arr1, size);
	//  Get the size
	size = arr2.count();
	//  Test Case 2
	obj.leftRoation(arr2, size);
}

Output

   Array
    1   2   3   4   5   6   7   8
   All Left Rotation
   2   3   4   5   6   7   8   1
   3   4   5   6   7   8   1   2
   4   5   6   7   8   1   2   3
   5   6   7   8   1   2   3   4
   6   7   8   1   2   3   4   5
   7   8   1   2   3   4   5   6
   8   1   2   3   4   5   6   7

   Array
    6   8   9   5   3   3   2
   All Left Rotation
   8   9   5   3   3   2   6
   9   5   3   3   2   6   8
   5   3   3   2   6   8   9
   3   3   2   6   8   9   5
   3   2   6   8   9   5   3
   2   6   8   9   5   3   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