Skip to main content

Find element at given index after a number of rotations

Here given code implementation process.

// C program 
// Find element at given index after a number of rotations
#include <stdio.h>

//Function which is display array elements
void display(int arr[], int size)
{
    for (int i = 0; i < size; ++i)
    {
        printf("%d  ", arr[i]);
    }
    printf("\n");
}
//Function which is swapping two array elements of given location
void swapElement(int arr[], int i, int j)
{
    // Get i location element
    int temp = arr[i];
    // Set new values
    arr[i] = arr[j];
    arr[j] = temp;
}
//Function which is Reversing given array elements 
void reverseElement(int arr[], int a, int b)
{
    int front = a;
    int tail = b;
    while (front < tail)
    {
        //Swap the array elements
        swapElement(arr, front, tail);
        front++;
        tail--;
    }
}
// Find the location element after various rotation
void rotatedElement(int arr[], int ranges[][2], int step, int n, int location)
{
    if(location < 0 || location >= n)
    {
        // When get invalid location
        return;
    }
    // This auxiliary array are used to hold information of rotations
    int auxiliary[n];

    // Loop controlling variables
    int i = 0;
    int a = 0;
    int b = 0;

    // Assign value of actual array in auxiliary array
    for (i = 0; i < n; ++i)
    {
        auxiliary[i] = arr[i];
    }


    for (i = 0; i < step; ++i)
    {
        a = ranges[i][0];
        b = ranges[i][1];

        if(a >= 0 && a < n &&  b >= 0 && b < n )
        {
            // Rotate Operation 
            reverseElement(auxiliary,a,b-1); 
            reverseElement(auxiliary,a,b); 
        }
        else
        {
            printf("\n Ranges (%d-%d) not valid\n",a,b);
            return;
        }
    }
    printf("\n Location at %d is : %d",location,auxiliary[location]);
   
}
int main()
{
    
    int arr[] = { 1, 2, 3, 4, 5, 6 }; 

    // Get the size of array
    int n = sizeof(arr)/sizeof(arr[0]);


    int ranges1[][2] = { {1, 3}, { 0, 3 }, {2, 4}, {1, 4}, {0, 3} };
    int ranges2[][2] = { {0, 4}, { 1, 5 }, {2, 4}, {1, 4}, {2, 5}, {1,3} };
    
    // Get the size of range
    int step = sizeof(ranges1) / sizeof(ranges1[0]) ; 

    /*

        1, 2, 3, 4, 5 , 6 <- Actual Array
        
        Rotations (1, 3) 
        1  4  2  3  5  6 

        Rotations (0, 3) 
        3  1  4  2  5  6 

        Rotations (2, 4) 
        3  1  5  4  2  6 

        Rotations (1, 4) 
        3  2  1  5  4  6  

        Rotations (0, 3) 
        5  3  2  1  4  6  
    
        location = 1

        O/P = 3
    */ 
    rotatedElement(arr,ranges1,step, n, 1); 

    /*

        1, 2, 3, 4, 5 , 6 <- Actual Array
        -----------------
        Rotations (0, 4) 
        5  1  2  3  4  6  

        Rotations (1, 5) 
        5  6  1  2  3  4  

        Rotations (2, 4) 
        5  6  3  1  2  4  

        Rotations (1, 4) 
        5  2  6  3  1  4  

        Rotations (2, 5) 
        5  2  4  6  3  1  

        Rotations (1, 3) 
        5  6  2  4  3  1 

        location = 3

        O/P = 4

    */
    step = sizeof(ranges2) / sizeof(ranges2[0]) ; 
    rotatedElement(arr,ranges2,step, n, 3);
    return 0;
}

Output

 Location at 1 is : 3
 Location at 3 is : 4
/*
   Java Program
   Find element at given index after a number of rotations
*/
public class SearchElement
{
//Function which is display array elements
public void display(int[] arr, int size)
{
    for (int i = 0; i < size; ++i)
    {
        System.out.print("  " + arr[i] );
    }
    System.out.print("\n");
}
//Function which is swapping two array elements of given location
public void swapElement(int[] arr, int i, int j)
{
    // Get i location element
    int temp = arr[i];
    // Set new values
    arr[i] = arr[j];
    arr[j] = temp;
}
//Function which is Reversing given array elements 
public void reverseElement(int[] arr, int a, int b)
{
    int front = a;
    int tail = b;
    while (front < tail)
    {
        //Swap the array elements
        swapElement(arr, front, tail);
        front++;
        tail--;
    }
}
// Find the location element after various rotation
public void rotatedElement(int[] arr, int[][] ranges, int step, int n, int location)
{
    if (location < 0 || location >= n)
    {
        // When get invalid location
        return;
    }
    // This auxiliary array are used to hold information of rotations
    int[] auxiliary = new int[n];
    // Loop controlling variables
    int i = 0;
    int a = 0;
    int b = 0;
    // Assign value of actual array in auxiliary array
    for (i = 0; i < n; ++i)
    {
        auxiliary[i] = arr[i];
    }
    for (i = 0; i < step; ++i)
    {
        a = ranges[i][0];
        b = ranges[i][1];
        if (a >= 0 && a < n && b >= 0 && b < n)
        {
            // Rotate Operation 
            reverseElement(auxiliary, a, b - 1);
            reverseElement(auxiliary, a, b);
        }
        else
        {
            System.out.print("\n Ranges (" + a + "-" + b + ") not valid\n");
            return;
        }
    }
    System.out.print("\n Location at " + location + " is : " + auxiliary[location]);
}
    
    public static void main(String[] args)
    {
    SearchElement task = new SearchElement();
    int[] arr = {
        1 , 2 , 3 , 4 , 5 , 6
    };
    // Get the size of array
    int n = arr.length;
    int [][]ranges1 = { {1, 3}, { 0, 3 }, {2, 4}, {1, 4}, {0, 3} };
    int [][]ranges2 = { {0, 4}, { 1, 5 }, {2, 4}, {1, 4}, {2, 5}, {1,3} };
    // Get the size of range
    int step = ranges1.length;
    /*
        1, 2, 3, 4, 5 , 6 <- Actual Array
        
        Rotations (1, 3) 
        1  4  2  3  5  6 

        Rotations (0, 3) 
        3  1  4  2  5  6 

        Rotations (2, 4) 
        3  1  5  4  2  6 

        Rotations (1, 4) 
        3  2  1  5  4  6  

        Rotations (0, 3) 
        5  3  2  1  4  6  

        location = 1

        O/P = 3
    */
    task.rotatedElement(arr, ranges1, step, n, 1);
    /*

        1, 2, 3, 4, 5 , 6 <- Actual Array
        -----------------
        Rotations (0, 4) 
        5  1  2  3  4  6  

        Rotations (1, 5) 
        5  6  1  2  3  4  

        Rotations (2, 4) 
        5  6  3  1  2  4  

        Rotations (1, 4) 
        5  2  6  3  1  4  

        Rotations (2, 5) 
        5  2  4  6  3  1  

        Rotations (1, 3) 
        5  6  2  4  3  1 

        location = 3

        O/P = 4

    */
    step = ranges2.length;
    task.rotatedElement(arr, ranges2, step, n, 3);
    }
}

Output

 Location at 1 is : 3
 Location at 3 is : 4
// Include header file
#include <iostream>

using namespace std;
/*
   C++ Program
   Find element at given index after a number of rotations
*/
class SearchElement
{
	public:
		//Function which is display array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	//Function which is swapping two array elements of given location
	void swapElement(int arr[], int i, int j)
	{
		// Get i location element
		int temp = arr[i];
		// Set new values
		arr[i] = arr[j];
		arr[j] = temp;
	}
	//Function which is Reversing given array elements
	void reverseElement(int arr[], int a, int b)
	{
		int front = a;
		int tail = b;
		while (front < tail)
		{
			//Swap the array elements
			this->swapElement(arr, front, tail);
			front++;
			tail--;
		}
	}
	// Find the location element after various rotation
	void rotatedElement(int arr[], int ranges[][2], int step, int n, int location)
	{
		// When get invalid location
		if (location < 0 || location >= n)
		{
			return;
		}
		// This auxiliary array are used to hold information of rotations
		int auxiliary[n];
		// Loop controlling variables
		int i = 0;
		int a = 0;
		int b = 0;
		// Assign value of actual array in auxiliary array
		for (i = 0; i < n; ++i)
		{
			auxiliary[i] = arr[i];
		}
		for (i = 0; i < step; ++i)
		{
			a = ranges[i][0];
			b = ranges[i][1];
			if (a >= 0 && a < n && b >= 0 && b < n)
			{
				// Rotate Operation
				this->reverseElement(auxiliary, a, b - 1);
				this->reverseElement(auxiliary, a, b);
			}
			else
			{
				cout << "\n Ranges (" << a << "-" << b << ") not valid\n";
				return;
			}
		}
		cout << "\n Location at " << location << " is : " << auxiliary[location];
	}
};
int main()
{
	SearchElement task = SearchElement();
	int arr[] = {
		1 , 2 , 3 , 4 , 5 , 6
	};
	// Get the size of array
	int n = sizeof(arr) / sizeof(arr[0]);
	int ranges1[][2] = {
		{
			1 , 3
		} , {
			0 , 3
		} , {
			2 , 4
		} , {
			1 , 4
		} , {
			0 , 3
		}
	};
	int ranges2[][2] = {
		{
			0 , 4
		} , {
			1 , 5
		} , {
			2 , 4
		} , {
			1 , 4
		} , {
			2 , 5
		} , {
			1 , 3
		}
	};
	// Get the size of range
	int step = sizeof(ranges1) / sizeof(ranges1[0]);
	/*
	        1, 2, 3, 4, 5 , 6 <- Actual Array
	        
	        Rotations (1, 3) 
	        1  4  2  3  5  6 

	        Rotations (0, 3) 
	        3  1  4  2  5  6 

	        Rotations (2, 4) 
	        3  1  5  4  2  6 

	        Rotations (1, 4) 
	        3  2  1  5  4  6  

	        Rotations (0, 3) 
	        5  3  2  1  4  6  

	        location = 1

	        O/P = 3
	    */
	task.rotatedElement(arr, ranges1, step, n, 1);
	/*

	        1, 2, 3, 4, 5 , 6 <- Actual Array
	        -----------------
	        Rotations (0, 4) 
	        5  1  2  3  4  6  

	        Rotations (1, 5) 
	        5  6  1  2  3  4  

	        Rotations (2, 4) 
	        5  6  3  1  2  4  

	        Rotations (1, 4) 
	        5  2  6  3  1  4  

	        Rotations (2, 5) 
	        5  2  4  6  3  1  

	        Rotations (1, 3) 
	        5  6  2  4  3  1 

	        location = 3

	        O/P = 4

	    */
	step = sizeof(ranges2) / sizeof(ranges2[0]);
	task.rotatedElement(arr, ranges2, step, n, 3);
	return 0;
}

Output

 Location at 1 is : 3
 Location at 3 is : 4
// Include namespace system
using System;
/*
   C# Program
   Find element at given index after a number of rotations
*/
public class SearchElement
{
    //Function which is display array elements
    public void display(int[] arr, int size)
    {
        for (int i = 0; i < size; ++i)
        {
            Console.Write("  " + arr[i]);
        }
        Console.Write("\n");
    }
    //Function which is swapping two array elements of given location
    public void swapElement(int[] arr, int i, int j)
    {
        // Get i location element
        int temp = arr[i];
        // Set new values
        arr[i] = arr[j];
        arr[j] = temp;
    }
    //Function which is Reversing given array elements
    public void reverseElement(int[] arr, int a, int b)
    {
        int front = a;
        int tail = b;
        while (front < tail)
        {
            //Swap the array elements
            swapElement(arr, front, tail);
            front++;
            tail--;
        }
    }
    // Find the location element after various rotation
    public void rotatedElement(int[] arr, int[,] ranges, int step, int n, int location)
    {
        // When get invalid location
        if (location < 0 || location >= n)
        {
            return;
        }
        // This auxiliary array are used to hold information of rotations
        int[] auxiliary = new int[n];
        // Loop controlling variables
        int i = 0;
        int a = 0;
        int b = 0;
        // Assign value of actual array in auxiliary array
        for (i = 0; i < n; ++i)
        {
            auxiliary[i] = arr[i];
        }
        for (i = 0; i < step; ++i)
        {
            a = ranges[i,0];
            b = ranges[i,1];
            if (a >= 0 && a < n && b >= 0 && b < n)
            {
                // Rotate Operation
                reverseElement(auxiliary, a, b - 1);
                reverseElement(auxiliary, a, b);
            }
            else
            {
                Console.Write("\n Ranges (" + a + "-" + b + ") not valid\n");
                return;
            }
        }
        Console.Write("\n Location at " + location + " is : " + auxiliary[location]);
    }
    public static void Main(String[] args)
    {
        SearchElement task = new SearchElement();
        int[] arr = {
            1 , 2 , 3 , 4 , 5 , 6
        };
        // Get the size of array
        int n = arr.Length;
        int[,] ranges1 = { {1, 3}, { 0, 3 }, {2, 4}, {1, 4}, {0, 3} };
        int[,] ranges2 = { {0, 4}, { 1, 5 }, {2, 4}, {1, 4}, {2, 5}, {1,3} };
        // Get the size of range
        int step = ranges1.GetLength(0);
        /*
            1, 2, 3, 4, 5 , 6 <- Actual Array
            
            Rotations (1, 3) 
            1  4  2  3  5  6 

            Rotations (0, 3) 
            3  1  4  2  5  6 

            Rotations (2, 4) 
            3  1  5  4  2  6 

            Rotations (1, 4) 
            3  2  1  5  4  6  

            Rotations (0, 3) 
            5  3  2  1  4  6  

            location = 1

            O/P = 3
        */
        task.rotatedElement(arr, ranges1, step, n, 1);
        /*

        1, 2, 3, 4, 5 , 6 <- Actual Array
        -----------------
        Rotations (0, 4) 
        5  1  2  3  4  6  

        Rotations (1, 5) 
        5  6  1  2  3  4  

        Rotations (2, 4) 
        5  6  3  1  2  4  

        Rotations (1, 4) 
        5  2  6  3  1  4  

        Rotations (2, 5) 
        5  2  4  6  3  1  

        Rotations (1, 3) 
        5  6  2  4  3  1 

        location = 3

        O/P = 4

    */
        step = ranges2.GetLength(0);
        task.rotatedElement(arr, ranges2, step, n, 3);
    }
}

Output

 Location at 1 is : 3
 Location at 3 is : 4
<?php
/*
   Php Program
   Find element at given index after a number of rotations
*/
class SearchElement
{
    //Function which is display array elements
    public  function display( & $arr, $size)
    {
        for ($i = 0; $i < $size; ++$i)
        {
            echo "  ". $arr[$i];
        }
        echo "\n";
    }
    //Function which is swapping two array elements of given location
    public  function swapElement( & $arr, $i, $j)
    {
        // Get i location element
        $temp = $arr[$i];
        // Set new values
        $arr[$i] = $arr[$j];
        $arr[$j] = $temp;
    }
    //Function which is Reversing given array elements
    public  function reverseElement( & $arr, $a, $b)
    {
        $front = $a;
        $tail = $b;
        while ($front < $tail)
        {
            //Swap the array elements
            $this->swapElement($arr, $front, $tail);
            $front++;
            $tail--;
        }
    }
    // Find the location element after various rotation
    public  function rotatedElement( & $arr, & $ranges, $step, $n, $location)
    {
        // When get invalid location
        if ($location < 0 || $location >= $n)
        {
            return;
        }
        // This auxiliary array are used to hold information of rotations
        $auxiliary = array_fill(0, $n, 0);
        // Loop controlling variables
        $i = 0;
        $a = 0;
        $b = 0;
        // Assign value of actual array in auxiliary array
        for ($i = 0; $i < $n; ++$i)
        {
            $auxiliary[$i] = $arr[$i];
        }
        for ($i = 0; $i < $step; ++$i)
        {
            $a = $ranges[$i][0];
            $b = $ranges[$i][1];
            if ($a >= 0 && $a < $n && $b >= 0 && $b < $n)
            {
                // Rotate Operation
                $this->reverseElement($auxiliary, $a, $b - 1);
                $this->reverseElement($auxiliary, $a, $b);
            }
            else
            {
                echo "\n Ranges (". $a ."-". $b .") not valid\n";
                return;
            }
        }
        echo "\n Location at ". $location ." is : ". $auxiliary[$location];
    }
}

function main()
{
    $task = new SearchElement();
    $arr = array(1, 2, 3, 4, 5, 6);
    // Get the size of array
    $n = count($arr);
    $ranges1 = array(
        array(1, 3), 
        array(0, 3), 
        array(2, 4), 
        array(1, 4), 
        array(0, 3)
    );
    $ranges2 = array(
        array(0, 4), 
        array(1, 5), 
        array(2, 4), 
        array(1, 4), 
        array(2, 5), 
        array(1, 3)
    );
    // Get the size of range
    $step = count($ranges1);
    /*
        1, 2, 3, 4, 5 , 6 <- Actual Array
        
        Rotations (1, 3) 
        1  4  2  3  5  6 

        Rotations (0, 3) 
        3  1  4  2  5  6 

        Rotations (2, 4) 
        3  1  5  4  2  6 

        Rotations (1, 4) 
        3  2  1  5  4  6  

        Rotations (0, 3) 
        5  3  2  1  4  6  

        location = 1

        O/P = 3
    */
    $task->rotatedElement($arr, $ranges1, $step, $n, 1);
    /*

        1, 2, 3, 4, 5 , 6 <- Actual Array
        -----------------
        Rotations (0, 4) 
        5  1  2  3  4  6  

        Rotations (1, 5) 
        5  6  1  2  3  4  

        Rotations (2, 4) 
        5  6  3  1  2  4  

        Rotations (1, 4) 
        5  2  6  3  1  4  

        Rotations (2, 5) 
        5  2  4  6  3  1  

        Rotations (1, 3) 
        5  6  2  4  3  1 

        location = 3

        O/P = 4

    */
    $step = count($ranges2);
    $task->rotatedElement($arr, $ranges2, $step, $n, 3);
}
main();

Output

 Location at 1 is : 3
 Location at 3 is : 4
/*
   Node Js Program
   Find element at given index after a number of rotations
*/
class SearchElement
{
    //Function which is display array elements
    display(arr, size)
    {
        for (var i = 0; i < size; ++i)
        {
            process.stdout.write("  " + arr[i]);
        }
        process.stdout.write("\n");
    }
    //Function which is swapping two array elements of given location
    swapElement(arr, i, j)
    {
        // Get i location element
        var temp = arr[i];
        // Set new values
        arr[i] = arr[j];
        arr[j] = temp;
    }
    //Function which is Reversing given array elements
    reverseElement(arr, a, b)
    {
        var front = a;
        var tail = b;
        while (front < tail)
        {
            //Swap the array elements
            this.swapElement(arr, front, tail);
            front++;
            tail--;
        }
    }
    // Find the location element after various rotation
    rotatedElement(arr, ranges, step, n, location)
    {
        // When get invalid location
        if (location < 0 || location >= n)
        {
            return;
        }
        // This auxiliary array are used to hold information of rotations
        var auxiliary = Array(n).fill(0);
        // Loop controlling variables
        var i = 0;
        var a = 0;
        var b = 0;
        // Assign value of actual array in auxiliary array
        for (i = 0; i < n; ++i)
        {
            auxiliary[i] = arr[i];
        }
        for (i = 0; i < step; ++i)
        {
            a = ranges[i][0];
            b = ranges[i][1];
            if (a >= 0 && a < n && b >= 0 && b < n)
            {
                // Rotate Operation
                this.reverseElement(auxiliary, a, b - 1);
                this.reverseElement(auxiliary, a, b);
            }
            else
            {
                process.stdout.write("\n Ranges (" + a + "-" + b + ") not valid\n");
                return;
            }
        }
        process.stdout.write("\n Location at " + location + " is : " + auxiliary[location]);
    }
}

function main()
{
    var task = new SearchElement();
    var arr = [1, 2, 3, 4, 5, 6];
    // Get the size of array
    var n = arr.length;
    var ranges1 = [
        [1, 3] , [0, 3] , [2, 4] , [1, 4] , [0, 3]
    ];
    var ranges2 = [
        [0, 4] , [1, 5] , [2, 4] , [1, 4] , [2, 5] , [1, 3]
    ];
    // Get the size of range
    var step = ranges1.length;
    /*
        1, 2, 3, 4, 5 , 6 <- Actual Array
        
        Rotations (1, 3) 
        1  4  2  3  5  6 

        Rotations (0, 3) 
        3  1  4  2  5  6 

        Rotations (2, 4) 
        3  1  5  4  2  6 

        Rotations (1, 4) 
        3  2  1  5  4  6  

        Rotations (0, 3) 
        5  3  2  1  4  6  

        location = 1

        O/P = 3
    */
    task.rotatedElement(arr, ranges1, step, n, 1);
    /*

        1, 2, 3, 4, 5 , 6 <- Actual Array
        -----------------
        Rotations (0, 4) 
        5  1  2  3  4  6  

        Rotations (1, 5) 
        5  6  1  2  3  4  

        Rotations (2, 4) 
        5  6  3  1  2  4  

        Rotations (1, 4) 
        5  2  6  3  1  4  

        Rotations (2, 5) 
        5  2  4  6  3  1  

        Rotations (1, 3) 
        5  6  2  4  3  1 

        location = 3

        O/P = 4

    */
    step = ranges2.length;
    task.rotatedElement(arr, ranges2, step, n, 3);
}
main();

Output

 Location at 1 is : 3
 Location at 3 is : 4
# 
#    Python 3 Program
#    Find element at given index after a number of rotations

class SearchElement :
	# Function which is display array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	# Function which is swapping two array elements of given location
	def swapElement(self, arr, i, j) :
		#  Get i location element
		temp = arr[i]
		#  Set new values
		arr[i] = arr[j]
		arr[j] = temp
	
	# Function which is Reversing given array elements
	def reverseElement(self, arr, a, b) :
		front = a
		tail = b
		while (front < tail) :
			# Swap the array elements
			self.swapElement(arr, front, tail)
			front += 1
			tail -= 1
		
	
	#  Find the location element after various rotation
	def rotatedElement(self, arr, ranges, step, n, location) :
		#  When get invalid location
		if (location < 0 or location >= n) :
			return
		
		#  This auxiliary array are used to hold information of rotations
		auxiliary = [0] * (n)
		#  Loop controlling variables
		i = 0
		a = 0
		b = 0
		#  Assign value of actual array in auxiliary array
		i = 0
		while (i < n) :
			auxiliary[i] = arr[i]
			i += 1
		
		i = 0
		while (i < step) :
			a = ranges[i][0]
			b = ranges[i][1]
			if (a >= 0 and a < n and b >= 0 and b < n) :
				#  Rotate Operation
				self.reverseElement(auxiliary, a, b - 1)
				self.reverseElement(auxiliary, a, b)
			else :
				print("\n Ranges (", a ,"-", b ,") not valid")
				return
			
			i += 1
		
		print("\n Location at ", location ," is : ", auxiliary[location], end = "")
	

def main() :
	task = SearchElement()
	arr = [1, 2, 3, 4, 5, 6]
	#  Get the size of array
	n = len(arr)
	ranges1 = [
		[1, 3] , [0, 3] , [2, 4] , [1, 4] , [0, 3]
	]
	ranges2 = [
		[0, 4] , [1, 5] , [2, 4] , [1, 4] , [2, 5] , [1, 3]
	]
	#  Get the size of range
	step = len(ranges1)
	# 
	# 		       1, 2, 3, 4, 5 , 6 <- Actual Array
	# 		       
	# 		       Rotations (1, 3) 
	# 		       1  4  2  3  5  6 
	# 		       Rotations (0, 3) 
	# 		       3  1  4  2  5  6 
	# 		       Rotations (2, 4) 
	# 		       3  1  5  4  2  6 
	# 		       Rotations (1, 4) 
	# 		       3  2  1  5  4  6  
	# 		       Rotations (0, 3) 
	# 		       5  3  2  1  4  6  
	# 		       location = 1
	# 		       O/P = 3
	# 		   
	
	task.rotatedElement(arr, ranges1, step, n, 1)
	# 
	# 		       1, 2, 3, 4, 5 , 6 <- Actual Array
	# 		       -----------------
	# 		       Rotations (0, 4) 
	# 		       5  1  2  3  4  6  
	# 		       Rotations (1, 5) 
	# 		       5  6  1  2  3  4  
	# 		       Rotations (2, 4) 
	# 		       5  6  3  1  2  4  
	# 		       Rotations (1, 4) 
	# 		       5  2  6  3  1  4  
	# 		       Rotations (2, 5) 
	# 		       5  2  4  6  3  1  
	# 		       Rotations (1, 3) 
	# 		       5  6  2  4  3  1 
	# 		       location = 3
	# 		       O/P = 4
	# 		   
	
	step = len(ranges2)
	task.rotatedElement(arr, ranges2, step, n, 3)

if __name__ == "__main__": main()

Output

 Location at  1  is :  3
 Location at  3  is :  4
#  Ruby Program
#  Find element at given index after a number of rotations

class SearchElement 
	# Function which is display array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	# Function which is swapping two array elements of given location
	def swapElement(arr, i, j) 
		#  Get i location element
		temp = arr[i]
		#  Set new values
		arr[i] = arr[j]
		arr[j] = temp
	end

	# Function which is Reversing given array elements
	def reverseElement(arr, a, b) 
		front = a
		tail = b
		while (front < tail) 
			# Swap the array elements
			self.swapElement(arr, front, tail)
			front += 1
			tail -= 1
		end

	end

	#  Find the location element after various rotation
	def rotatedElement(arr, ranges, step, n, location) 
		#  When get invalid location
		if (location < 0 || location >= n) 
			return
		end

		#  This auxiliary array are used to hold information of rotations
		auxiliary = Array.new(n) {0}
		#  Loop controlling variables
		i = 0
		a = 0
		b = 0
		#  Assign value of actual array in auxiliary array
		i = 0
		while (i < n) 
			auxiliary[i] = arr[i]
			i += 1
		end

		i = 0
		while (i < step) 
			a = ranges[i][0]
			b = ranges[i][1]
			if (a >= 0 && a < n && b >= 0 && b < n) 
				#  Rotate Operation
				self.reverseElement(auxiliary, a, b - 1)
				self.reverseElement(auxiliary, a, b)
			else 
				print("\n Ranges (", a ,"-", b ,") not valid\n")
				return
			end

			i += 1
		end

		print("\n Location at ", location ," is : ", auxiliary[location])
	end

end

def main() 
	task = SearchElement.new()
	arr = [1, 2, 3, 4, 5, 6]
	#  Get the size of array
	n = arr.length
	ranges1 = [
		[1, 3] , [0, 3] , [2, 4] , [1, 4] , [0, 3]
	]
	ranges2 = [
		[0, 4] , [1, 5] , [2, 4] , [1, 4] , [2, 5] , [1, 3]
	]
	#  Get the size of range
	step = ranges1.length
	# 
	# 		       1, 2, 3, 4, 5 , 6 <- Actual Array
	# 		       
	# 		       Rotations (1, 3) 
	# 		       1  4  2  3  5  6 
	# 		       Rotations (0, 3) 
	# 		       3  1  4  2  5  6 
	# 		       Rotations (2, 4) 
	# 		       3  1  5  4  2  6 
	# 		       Rotations (1, 4) 
	# 		       3  2  1  5  4  6  
	# 		       Rotations (0, 3) 
	# 		       5  3  2  1  4  6  
	# 		       location = 1
	# 		       O/P = 3
	# 		   
	
	task.rotatedElement(arr, ranges1, step, n, 1)
	# 
	# 		       1, 2, 3, 4, 5 , 6 <- Actual Array
	# 		       -----------------
	# 		       Rotations (0, 4) 
	# 		       5  1  2  3  4  6  
	# 		       Rotations (1, 5) 
	# 		       5  6  1  2  3  4  
	# 		       Rotations (2, 4) 
	# 		       5  6  3  1  2  4  
	# 		       Rotations (1, 4) 
	# 		       5  2  6  3  1  4  
	# 		       Rotations (2, 5) 
	# 		       5  2  4  6  3  1  
	# 		       Rotations (1, 3) 
	# 		       5  6  2  4  3  1 
	# 		       location = 3
	# 		       O/P = 4
	# 		   
	
	step = ranges2.length
	task.rotatedElement(arr, ranges2, step, n, 3)
end

main()

Output

 Location at 1 is : 3
 Location at 3 is : 4
/*
   Scala Program
   Find element at given index after a number of rotations
*/
class SearchElement
{
    //Function which is display array elements
    def display(arr: Array[Int], size: Int): Unit = {
        var i: Int = 0;
        while (i < size)
        {
            print("  " + arr(i));
            i += 1;
        }
        print("\n");
    }
    //Function which is swapping two array elements of given location
    def swapElement(arr: Array[Int], i: Int, j: Int): Unit = {
        // Get i location element
        var temp: Int = arr(i);
        // Set new values
        arr(i) = arr(j);
        arr(j) = temp;
    }
    //Function which is Reversing given array elements
    def reverseElement(arr: Array[Int], a: Int, b: Int): Unit = {
        var front: Int = a;
        var tail: Int = b;
        while (front < tail)
        {
            //Swap the array elements
            this.swapElement(arr, front, tail);
            front += 1;
            tail -= 1;
        }
    }
    // Find the location element after various rotation
    def rotatedElement(arr: Array[Int], ranges: Array[Array[Int]], step: Int, n: Int, location: Int): Unit = {
        // When get invalid location
        if (location < 0 || location >= n)
        {
            return;
        }
        // This auxiliary array are used to hold information of rotations
        var auxiliary: Array[Int] = Array.fill[Int](n)(0);
        // Loop controlling variables
        var i: Int = 0;
        var a: Int = 0;
        var b: Int = 0;
        // Assign value of actual array in auxiliary array
        i = 0;
        while (i < n)
        {
            auxiliary(i) = arr(i);
            i += 1;
        }
        i = 0;
        while (i < step)
        {
            a = ranges(i)(0);
            b = ranges(i)(1);
            if (a >= 0 && a < n && b >= 0 && b < n)
            {
                // Rotate Operation
                this.reverseElement(auxiliary, a, b - 1);
                this.reverseElement(auxiliary, a, b);
            }
            else
            {
                print("\n Ranges (" + a + "-" + b + ") not valid\n");
                return;
            }
            i += 1;
        }
        print("\n Location at " + location + " is : " + auxiliary(location));
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: SearchElement = new SearchElement();
        var arr: Array[Int] = Array(1, 2, 3, 4, 5, 6);
        // Get the size of array
        var n: Int = arr.length;
        var ranges1: Array[Array[Int]] = Array(
            Array(1, 3), 
            Array(0, 3), 
            Array(2, 4), 
            Array(1, 4), 
            Array(0, 3)
        );
        var ranges2: Array[Array[Int]] = Array(
            Array(0, 4), 
            Array(1, 5), 
            Array(2, 4), 
            Array(1, 4), 
            Array(2, 5), 
            Array(1, 3)
        );
        // Get the size of range
        var step: Int = ranges1.length;
        /*
           1, 2, 3, 4, 5 , 6 <- Actual Array
           
           Rotations (1, 3) 
           1  4  2  3  5  6 

           Rotations (0, 3) 
           3  1  4  2  5  6 

           Rotations (2, 4) 
           3  1  5  4  2  6 

           Rotations (1, 4) 
           3  2  1  5  4  6  

           Rotations (0, 3) 
           5  3  2  1  4  6  

           location = 1

           O/P = 3
       */
        task.rotatedElement(arr, ranges1, step, n, 1);
        /*

           1, 2, 3, 4, 5 , 6 <- Actual Array
           -----------------
           Rotations (0, 4) 
           5  1  2  3  4  6  

           Rotations (1, 5) 
           5  6  1  2  3  4  

           Rotations (2, 4) 
           5  6  3  1  2  4  

           Rotations (1, 4) 
           5  2  6  3  1  4  

           Rotations (2, 5) 
           5  2  4  6  3  1  

           Rotations (1, 3) 
           5  6  2  4  3  1 

           location = 3

           O/P = 4

       */
        step = ranges2.length;
        task.rotatedElement(arr, ranges2, step, n, 3);
    }
}

Output

 Location at 1 is : 3
 Location at 3 is : 4
/*
   Swift 4 Program
   Find element at given index after a number of rotations
*/
class SearchElement
{
    //Function which is display array elements
    func display(_ arr: [Int], _ size: Int)
    {
        var i: Int = 0;
        while (i < size)
        {
            print("  ", arr[i], terminator: "");
            i += 1;
        }
        print(terminator: "\n");
    }
    //Function which is swapping two array elements of given location
    func swapElement(_ arr: inout[Int], _ i: Int, _ j: Int)
    {
        // Get i location element
        let temp: Int = arr[i];
        // Set new values
        arr[i] = arr[j];
        arr[j] = temp;
    }
    //Function which is Reversing given array elements
    func reverseElement(_ arr: inout[Int], _ a: Int, _ b: Int)
    {
        var front: Int = a;
        var tail: Int = b;
        while (front < tail)
        {
            //Swap the array elements
            self.swapElement(&arr, front, tail);
            front += 1;
            tail -= 1;
        }
    }
    // Find the location element after various rotation
    func rotatedElement(_ arr: [Int], _ ranges: [[Int]], _ step: Int, _ n: Int, _ location: Int)
    {
        // When get invalid location
        if (location < 0 || location >= n)
        {
            return;
        }
        // This auxiliary array are used to hold information of rotations
        var auxiliary: [Int] = Array(repeating: 0, count: n);
        // Loop controlling variables
        var i: Int = 0;
        var a: Int = 0;
        var b: Int = 0;
        // Assign value of actual array in auxiliary array
        i = 0;
        while (i < n)
        {
            auxiliary[i] = arr[i];
            i += 1;
        }
        i = 0;
        while (i < step)
        {
            a = ranges[i][0];
            b = ranges[i][1];
            if (a >= 0 && a < n && b >= 0 && b < n)
            {
                // Rotate Operation
                self.reverseElement(&auxiliary, a, b - 1);
                self.reverseElement(&auxiliary, a, b);
            }
            else
            {
                print("\n Ranges (", a ,"-", b ,") not valid");
                return;
            }
            i += 1;
        }
        print("\n Location at ", location ," is : ", auxiliary[location], terminator: "");
    }
}
func main()
{
    let task: SearchElement = SearchElement();
    let arr: [Int] = [1, 2, 3, 4, 5, 6];
    // Get the size of array
    let n: Int = arr.count;
    let ranges1: [[Int]] = [[1, 3] , [0, 3] , [2, 4] , [1, 4] , [0, 3]];
    let ranges2: [[Int]] = [[0, 4] , [1, 5] , [2, 4] , [1, 4] , [2, 5] , [1, 3]];
    // Get the size of range
    var step: Int = ranges1.count;
    /*
       1, 2, 3, 4, 5 , 6 <- Actual Array
       
       Rotations (1, 3) 
       1  4  2  3  5  6 

       Rotations (0, 3) 
       3  1  4  2  5  6 

       Rotations (2, 4) 
       3  1  5  4  2  6 

       Rotations (1, 4) 
       3  2  1  5  4  6  

       Rotations (0, 3) 
       5  3  2  1  4  6  

       location = 1

       O/P = 3
   */
    task.rotatedElement(arr, ranges1, step, n, 1);
    /*

       1, 2, 3, 4, 5 , 6 <- Actual Array
       -----------------
       Rotations (0, 4) 
       5  1  2  3  4  6  

       Rotations (1, 5) 
       5  6  1  2  3  4  

       Rotations (2, 4) 
       5  6  3  1  2  4  

       Rotations (1, 4) 
       5  2  6  3  1  4  

       Rotations (2, 5) 
       5  2  4  6  3  1  

       Rotations (1, 3) 
       5  6  2  4  3  1 

       location = 3

       O/P = 4

   */
    step = ranges2.count;
    task.rotatedElement(arr, ranges2, step, n, 3);
}
main();

Output

 Location at  1  is :  3
 Location at  3  is :  4
/*
   Kotlin Program
   Find element at given index after a number of rotations
*/
class SearchElement
{
    //Function which is display array elements
    fun display(arr: Array<Int>, size: Int): Unit
    {
        var i: Int = 0;
        while (i<size)
        {
            print("  " + arr[i]);
            i += 1;
        }
        print("\n");
    }
    //Function which is swapping two array elements of given location
    fun swapElement(arr: Array<Int>, i: Int, j: Int): Unit
    {
        // Get i location element
        var temp: Int = arr[i];
        // Set new values
        arr[i] = arr[j];
        arr[j] = temp;
    }
    //Function which is Reversing given array elements
    fun reverseElement(arr: Array<Int>, a: Int, b: Int): Unit
    {
        var front: Int = a;
        var tail: Int = b;
        while (front<tail)
        {
            //Swap the array elements
            this.swapElement(arr, front, tail);
            front += 1;
            tail -= 1;
        }
    }
    // Find the location element after various rotation
    fun rotatedElement(arr: Array<Int>, ranges: Array<Array<Int>> , step: Int, n: Int, location: Int): Unit
    {
        // When get invalid location
        if (location<0 || location>= n)
        {
            return;
        }
        // This auxiliary array are used to hold information of rotations
        var auxiliary: Array<Int> = Array(n)
        {
            0
        };
        // Loop controlling variables
        var i: Int = 0;
        var a: Int ;
        var b: Int ;
        // Assign value of actual array in auxiliary array
        while (i<n)
        {
            auxiliary[i] = arr[i];
            i += 1;
        }
        i = 0;
        while (i<step)
        {
            a = ranges[i][0];
            b = ranges[i][1];
            if (a>= 0 && a<n && b>= 0 && b<n)
            {
                // Rotate Operation
                this.reverseElement(auxiliary, a, b - 1);
                this.reverseElement(auxiliary, a, b);
            }
            else
            {
                print("\n Ranges (" + a + "-" + b + ") not valid\n");
                return;
            }
            i += 1;
        }
        print("\n Location at " + location + " is : " + auxiliary[location]);
    }
}
fun main(args: Array<String>): Unit
{
    var task: SearchElement = SearchElement();
    var arr: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6);
    // Get the size of array
    var n: Int = arr.count();
    var ranges1: Array<Array<Int>> = arrayOf(
        arrayOf(1, 3), 
        arrayOf(0, 3), 
        arrayOf(2, 4), 
        arrayOf(1, 4), 
        arrayOf(0, 3)
    );
    var ranges2: Array<Array<Int>> = arrayOf(
        arrayOf(0, 4), 
        arrayOf(1, 5), 
        arrayOf(2, 4), 
        arrayOf(1, 4), 
        arrayOf(2, 5), 
        arrayOf(1, 3)
    );
    // Get the size of range
    var step: Int = ranges1.count();
    /*
       1, 2, 3, 4, 5 , 6 <- Actual Array
       
       Rotations (1, 3) 
       1  4  2  3  5  6 

       Rotations (0, 3) 
       3  1  4  2  5  6 

       Rotations (2, 4) 
       3  1  5  4  2  6 

       Rotations (1, 4) 
       3  2  1  5  4  6  

       Rotations (0, 3) 
       5  3  2  1  4  6  

       location = 1

       O/P = 3
   */
    task.rotatedElement(arr, ranges1, step, n, 1);
    /*

       1, 2, 3, 4, 5 , 6 <- Actual Array
       -----------------
       Rotations (0, 4) 
       5  1  2  3  4  6  

       Rotations (1, 5) 
       5  6  1  2  3  4  

       Rotations (2, 4) 
       5  6  3  1  2  4  

       Rotations (1, 4) 
       5  2  6  3  1  4  

       Rotations (2, 5) 
       5  2  4  6  3  1  

       Rotations (1, 3) 
       5  6  2  4  3  1 

       location = 3

       O/P = 4

   */
    step = ranges2.count();
    task.rotatedElement(arr, ranges2, step, n, 3);
}

Output

 Location at 1 is : 3
 Location at 3 is : 4




Comment

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