Find the Nth element in array after K left rotations
Here given code implementation process.
// C program
// Find the Nth element in array after K left rotations
#include <stdio.h>
// Find nth element after left rotation
void findElement(int collection[], int size, int k, int n)
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
int location = (k + n - 1) % size;
// Display calculated result
printf(" After %d-th rotation, %d-th element is : %d\n", k, n, collection[location]);
}
int main()
{
int collection[] = {
1 , 2 , 4 , 5 , 6 , 7 , 8 , 9
};
// Get the size
int size = sizeof(collection) / sizeof(collection[0]);
int k = 4;
int n = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
findElement(collection, size, k, n);
return 0;
}
Output
After 4-th rotation, 3-th element is : 8
After 3-th rotation, 6-th element is : 1
/*
Java Program
Find the Nth element in array after K left rotations
*/
public class FindElement
{
// Find nth element after left rotation
public void findElement(int[] collection, int size, int k, int n)
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
int location = (k + n - 1) % size;
// Display calculated result
System.out.print(" After " + k + "-th rotation " + n + "-th element is : " + collection[location] + "\n");
}
public static void main(String[] args)
{
FindElement task = new FindElement();
int[] collection = {
1 , 2 , 4 , 5 , 6 , 7 , 8 , 9
};
// Get the size
int size = collection.length;
int k = 4;
int n = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
}
}
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find the Nth element in array after K left rotations
*/
class FindElement
{
public:
// Find nth element after left rotation
void findElement(int collection[], int size, int k, int n)
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
int location = (k + n - 1) % size;
// Display calculated result
cout << " After " << k << "-th rotation " << n << "-th element is : " << collection[location] << "\n";
}
};
int main()
{
FindElement task = FindElement();
int collection[] = {
1 , 2 , 4 , 5 , 6 , 7 , 8 , 9
};
// Get the size
int size = sizeof(collection) / sizeof(collection[0]);
int k = 4;
int n = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
return 0;
}
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
// Include namespace system
using System;
/*
C# Program
Find the Nth element in array after K left rotations
*/
public class FindElement
{
// Find nth element after left rotation
public void findElement(int[] collection, int size, int k, int n)
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
int location = (k + n - 1) % size;
// Display calculated result
Console.Write(" After " + k + "-th rotation " + n + "-th element is : " + collection[location] + "\n");
}
public static void Main(String[] args)
{
FindElement task = new FindElement();
int[] collection = {
1 , 2 , 4 , 5 , 6 , 7 , 8 , 9
};
// Get the size
int size = collection.Length;
int k = 4;
int n = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
}
}
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
<?php
/*
Php Program
Find the Nth element in array after K left rotations
*/
class SearchElement
{
// Find nth element after left rotation
public function findElement( $collection, $size, $k, $n)
{
if ($k <= 0 || $size <= 0)
{
return;
}
// Calculate nth element location after k rotation
$location = ($k + $n - 1) % $size;
// Display calculated result
echo " After ". $k ."-th rotation ". $n ."-th element is : ". $collection[$location] ."\n";
}
}
function main()
{
$task = new SearchElement();
$collection = array(1, 2, 4, 5, 6, 7, 8, 9);
// Get the size
$size = count($collection);
$k = 4;
$n = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
$task->findElement($collection, $size, $k, $n);
$k = 3;
$n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
$task->findElement($collection, $size, $k, $n);
}
main();
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
/*
Node Js Program
Find the Nth element in array after K left rotations
*/
class SearchElement
{
// Find nth element after left rotation
findElement(collection, size, k, n)
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
var location = (k + n - 1) % size;
// Display calculated result
process.stdout.write(" After " + k + "-th rotation " + n + "-th element is : " + collection[location] + "\n");
}
}
function main()
{
var task = new SearchElement();
var collection = [1, 2, 4, 5, 6, 7, 8, 9];
// Get the size
var size = collection.length;
var k = 4;
var n = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
}
main();
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
# Python 3 Program
# Find the Nth element in array after K left rotations
class SearchElement :
# Find nth element after left rotation
def findElement(self, collection, size, k, n) :
if (k <= 0 or size <= 0) :
return
# Calculate nth element location after k rotation
location = (k + n - 1) % size
# Display calculated result
print(" After ", k ,"-th rotation ", n ,"-th element is : ", collection[location] )
def main() :
task = SearchElement()
collection = [1, 2, 4, 5, 6, 7, 8, 9]
# Get the size
size = len(collection)
k = 4
n = 3
# Before Rotation : :1,2,4,5,6,7,8,9
# K = 4
#
# After k Rotation : :6,7,8,9,1,2,4,5
# n = 3
# output : 8
#
task.findElement(collection, size, k, n)
k = 3
n = 6
# Before Rotation : :1,2,4,5,6,7,8,9
# K = 3
#
# After k Rotation : :5,6,7,8,9,1,2,4
# n = 6
# output : 1
#
task.findElement(collection, size, k, n)
if __name__ == "__main__": main()
Output
After 4 -th rotation 3 -th element is : 8
After 3 -th rotation 6 -th element is : 1
# Ruby Program
# Find the Nth element in array after K left rotations
class SearchElement
# Find nth element after left rotation
def findElement(collection, size, k, n)
if (k <= 0 || size <= 0)
return
end
# Calculate nth element location after k rotation
location = (k + n - 1) % size
# Display calculated result
print(" After ", k ,"-th rotation ", n ,"-th element is : ", collection[location] ,"\n")
end
end
def main()
task = SearchElement.new()
collection = [1, 2, 4, 5, 6, 7, 8, 9]
# Get the size
size = collection.length
k = 4
n = 3
#
# Before Rotation : 1,2,4,5,6,7,8,9end
# K = 4
#
# After k Rotation : 6,7,8,9,1,2,4,5end
# n = 3
# output : 8
#
task.findElement(collection, size, k, n)
k = 3
n = 6
#
# Before Rotation : 1,2,4,5,6,7,8,9end
# K = 3
#
# After k Rotation : 5,6,7,8,9,1,2,4end
# n = 6
# output : 1
#
task.findElement(collection, size, k, n)
end
main()
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
/*
Scala Program
Find the Nth element in array after K left rotations
*/
class SearchElement
{
// Find nth element after left rotation
def findElement(collection: Array[Int], size: Int, k: Int, n: Int): Unit = {
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
var location: Int = (k + n - 1) % size;
// Display calculated result
print(" After " + k + "-th rotation " + n + "-th element is : " + collection(location) + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SearchElement = new SearchElement();
var collection: Array[Int] = Array(1, 2, 4, 5, 6, 7, 8, 9);
// Get the size
var size: Int = collection.length;
var k: Int = 4;
var n: Int = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
}
}
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
/*
Swift 4 Program
Find the Nth element in array after K left rotations
*/
class SearchElement
{
// Find nth element after left rotation
func findElement(_ collection: [Int], _ size: Int, _ k: Int, _ n: Int)
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
let location: Int = (k + n - 1) % size;
// Display calculated result
print(" After \(k)-th rotation \(n)-th element is : ", collection[location]);
}
}
func main()
{
let task: SearchElement = SearchElement();
let collection: [Int] = [1, 2, 4, 5, 6, 7, 8, 9];
// Get the size
let size: Int = collection.count;
var k: Int = 4;
var n: Int = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
}
main();
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
/*
Kotlin Program
Find the Nth element in array after K left rotations
*/
class SearchElement
{
// Find nth element after left rotation
fun findElement(collection: Array<Int>, size: Int, k: Int, n: Int): Unit
{
if (k <= 0 || size <= 0)
{
return;
}
// Calculate nth element location after k rotation
var location: Int = (k + n - 1) % size;
// Display calculated result
print(" After " + k + "-th rotation " + n + "-th element is : " + collection[location] + "\n");
}
}
fun main(args: Array<String>): Unit
{
var task: SearchElement = SearchElement();
var collection: Array<Int> = arrayOf(1, 2, 4, 5, 6, 7, 8, 9);
// Get the size
var size: Int = collection.count();
var k: Int = 4;
var n: Int = 3;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 4
After k Rotation : {6,7,8,9,1,2,4,5}
n = 3
output : 8
*/
task.findElement(collection, size, k, n);
k = 3;
n = 6;
/*
Before Rotation : {1,2,4,5,6,7,8,9}
K = 3
After k Rotation : {5,6,7,8,9,1,2,4}
n = 6
output : 1
*/
task.findElement(collection, size, k, n);
}
Output
After 4-th rotation 3-th element is : 8
After 3-th rotation 6-th element is : 1
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