Print K th left rotation of array elements
Here given code implementation process.
// C Program
// Print K th left 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 left k rotation of an array
void leftRoationByK(int arr[], int size, int k)
{
display(arr, size);
printf(" Rotated left by %d element \n", k);
int j = k % size;
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)
leftRoationByK(arr1, size, 2);
leftRoationByK(arr1, size, 3);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
// Test Case B (of arr2)
leftRoationByK(arr2, size, 4);
leftRoationByK(arr2, size, 13);
return 0;
}
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
/*
Java program
Print K th left rotation of array elements
*/
public class LeftRotation
{
// 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 left k rotation of an array
public void leftRoationByK(int[] arr, int size, int k)
{
display(arr, size);
System.out.print(" Rotated left by " + k + " element \n");
int j = k % size;
for (int i = 0; i < size; ++i)
{
// display element
System.out.print(" " + arr[(j + i) % (size)]);
}
}
public static void main(String[] args)
{
LeftRotation work = new LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = arr2.length;
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
}
}
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left 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 left rotation of array elements
*/
class LeftRotation
{
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 left k rotation of an array
void leftRoationByK(int arr[], int size, int k)
{
this->display(arr, size);
cout << " Rotated left by " << k << " element \n";
int j = k % size;
for (int i = 0; i < size; ++i)
{
// display element
cout << " " << arr[(j + i) % (size)];
}
}
};
int main()
{
LeftRotation work = LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
return 0;
}
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
// Include namespace system
using System;
/*
C# program
Print K th left rotation of array elements
*/
public class LeftRotation
{
// 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 left k rotation of an array
public void leftRoationByK(int[] arr, int size, int k)
{
display(arr, size);
Console.Write(" Rotated left by " + k + " element \n");
int j = k % size;
for (int i = 0; i < size; ++i)
{
// display element
Console.Write(" " + arr[(j + i) % (size)]);
}
}
public static void Main(String[] args)
{
LeftRotation work = new LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = arr2.Length;
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
}
}
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
<?php
/*
Php program
Print K th left rotation of array elements
*/
class LeftRotation
{
// Display array elements
public function display( & $arr, $size)
{
echo "\n Array Elements \n";
$i = 0;
while ($i < $size)
{
echo " ". $arr[$i];
++$i;
}
echo "\n";
}
// Prints the left k rotation of an array
public function leftRoationByK( & $arr, $size, $k)
{
$this->display($arr, $size);
echo " Rotated left by ". $k ." element \n";
$j = $k % $size;
$i = 0;
while ($i < $size)
{
// display element
echo " ". $arr[($j + $i) % ($size)];
++$i;
}
}
}
function main()
{
$work = new LeftRotation();
// 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->leftRoationByK($arr1, $size, 2);
$work->leftRoationByK($arr1, $size, 3);
// Get the size
$size = count($arr2);
// Test Case B (of arr2)
$work->leftRoationByK($arr2, $size, 4);
$work->leftRoationByK($arr2, $size, 13);
}
main();
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
/*
Node Js program
Print K th left rotation of array elements
*/
class LeftRotation
{
// 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 left k rotation of an array
leftRoationByK(arr, size, k)
{
this.display(arr, size);
process.stdout.write(" Rotated left by " + k + " element \n");
var j = k % size;
for (var i = 0; i < size; ++i)
{
// display element
process.stdout.write(" " + arr[(j + i) % (size)]);
}
}
}
function main()
{
var work = new LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = arr2.length;
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
}
main();
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
# Python 3 program
# Print K th left rotation of array elements
class LeftRotation :
# 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 left k rotation of an array
def leftRoationByK(self, arr, size, k) :
self.display(arr, size)
print(" Rotated left by ", k ," element ")
j = k % size
i = 0
while (i < size) :
# display element
print(" ", arr[(j + i) % (size)], end = "")
i += 1
def main() :
work = LeftRotation()
# 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.leftRoationByK(arr1, size, 2)
work.leftRoationByK(arr1, size, 3)
# Get the size
size = len(arr2)
# Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4)
work.leftRoationByK(arr2, size, 13)
if __name__ == "__main__": main()
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
# Ruby program
# Print K th left rotation of array elements
class LeftRotation
# 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 left k rotation of an array
def leftRoationByK(arr, size, k)
self.display(arr, size)
print(" Rotated left by ", k ," element \n")
j = k % size
i = 0
while (i < size)
# display element
print(" ", arr[(j + i) % (size)])
i += 1
end
end
end
def main()
work = LeftRotation.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.leftRoationByK(arr1, size, 2)
work.leftRoationByK(arr1, size, 3)
# Get the size
size = arr2.length
# Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4)
work.leftRoationByK(arr2, size, 13)
end
main()
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
/*
Scala program
Print K th left rotation of array elements
*/
class LeftRotation
{
// 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 left k rotation of an array
def leftRoationByK(arr: Array[Int], size: Int, k: Int): Unit = {
this.display(arr, size);
print(" Rotated left by " + k + " element \n");
var j: Int = k % size;
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: LeftRotation = new LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = arr2.length;
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
}
}
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
/*
Swift 4 program
Print K th left rotation of array elements
*/
class LeftRotation
{
// 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 left k rotation of an array
func leftRoationByK(_ arr: [Int], _ size: Int, _ k: Int)
{
self.display(arr, size);
print(" Rotated left by ", k ," element ");
let j: Int = k % size;
var i: Int = 0;
while (i < size)
{
// display element
print(" ", arr[(j + i) % (size)], terminator: "");
i += 1;
}
}
}
func main()
{
let work: LeftRotation = LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = arr2.count;
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
}
main();
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 13 element
4 5 6 7 8 9 10 1 2 3
/*
Kotlin program
Print K th left rotation of array elements
*/
class LeftRotation
{
// 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 left k rotation of an array
fun leftRoationByK(arr: Array<Int>, size: Int, k: Int): Unit
{
this.display(arr, size);
print(" Rotated left by " + k + " element \n");
var j: Int = k % size;
var i: Int = 0;
while (i<size)
{
// display element
print(" " + arr[(j + i) % (size)]);
i += 1;
}
}
}
fun main(args: Array<String>): Unit
{
var work: LeftRotation = LeftRotation();
// 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.leftRoationByK(arr1, size, 2);
work.leftRoationByK(arr1, size, 3);
// Get the size
size = arr2.count();
// Test Case B (of arr2)
work.leftRoationByK(arr2, size, 4);
work.leftRoationByK(arr2, size, 13);
}
Output
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 2 element
1 6 5 4 2 7 9 8 3
Array Elements
8 3 1 6 5 4 2 7 9
Rotated left by 3 element
6 5 4 2 7 9 8 3 1
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left by 4 element
5 6 7 8 9 10 1 2 3 4
Array Elements
1 2 3 4 5 6 7 8 9 10
Rotated left 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