PHP Program to Find element at given index after a number of rotations
Last Updated :
23 Jul, 2024
An array consisting of N integers is given. There are several Right Circular Rotations of range[L..R] that we perform. After performing these rotations, we need to find an element at a given index.
Examples :
Input : arr[] : {1, 2, 3, 4, 5}
ranges[] = { {0, 2}, {0, 3} }
index : 1
Output : 3
Explanation : After first given rotation {0, 2}
arr[] = {3, 1, 2, 4, 5}
After second rotation {0, 3}
arr[] = {4, 3, 1, 2, 5}
After all rotations we have element 3 at given
index 1.
Method - 1: Brute-force Approach
The brute force approach is to actually rotate the array for all given ranges, finally return the element in at given index in the modified array.
Method - 2: Efficient Approach
We can do offline processing after saving all ranges.
Suppose, our rotate ranges are : [0..2] and [0..3]
We run through these ranges from reverse.
After range [0..3], index 0 will have the element which was on index 3.
So, we can change 0 to 3, i.e. if index = left, index will be changed to right.
After range [0..2], index 3 will remain unaffected.
So, we can make 3 cases :
If index = left, index will be changed to right.
If index is not bounds by the range, no effect of rotation.
If index is in bounds, index will have the element at index-1.
For better explanation:
10 20 30 40 50
Index: 1
Rotations: {0,2} {1,4} {0,3}
Answer:
Index 1 will have 30 after all the 3 rotations in the order {0,2} {1,4} {0,3}.
We performed {0,2} on A and now we have a new array A1.
We performed {1,4} on A1 and now we have a new array A2.
We performed {0,3} on A2 and now we have a new array A3.
Now we are looking for the value at index 1 in A3.
But A3 is {0,3} done on A2.
So index 1 in A3 is index 0 in A2.
But A2 is {1,4} done on A1.
So index 0 in A2 is also index 0 in A1 as it does not lie in the range {1,4}.
But A1 is {0,2} done on A.
So index 0 in A1 is index 2 in A.
On observing it, we are going deeper into the previous rotations
starting from the latest rotation.
{0,3}
|
{1,4}
|
{0,2}
This is the reason we are processing the rotations in reverse order.
Please note that we are not rotating the elements in the reverse order, just processing the index from reverse.
Because if we actually rotate in reverse order, we might get a completely different answer as in case of rotations the order matters.
PHP
<?php
// PHP code to rotate an array
// and answer the index query
// Function to compute the
// element at given index
function findElement($arr, $ranges,
$rotations, $index)
{
for ($i = $rotations - 1;
$i >= 0; $i--)
{
// Range[left...right]
$left = $ranges[$i][0];
$right = $ranges[$i][1];
// Rotation will not
// have any effect
if ($left <= $index &&
$right >= $index)
{
if ($index == $left)
$index = $right;
else
$index--;
}
}
// Returning new element
return $arr[$index];
}
// Driver Code
$arr = array(1, 2, 3, 4, 5);
// No. of rotations
$rotations = 2;
// Ranges according
// to 0-based indexing
$ranges = array(array(0, 2),
array(0, 3));
$index = 1;
echo findElement($arr, $ranges,
$rotations, $index);
// This code is contributed by ajit
?>
Complexity Analysis:
- Time Complexity: O(N), where N represents the given number of rotations.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Please refer complete article on Find element at given index after a number of rotations for more details!
Similar Reads
PHP Program to Generate all rotations of a number
Given an integer n, the task is to generate all the left shift numbers possible. A left shift number is a number that is generated when all the digits of the number are shifted one position to the left and the digit at the first position is shifted to the last.Examples: Input: n = 123 Output: 231 31
2 min read
PHP Program to Find closest number in array
Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples:Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input : arr[] = {2, 5, 6, 7, 8, 8, 9};
3 min read
Php Program to Check if two numbers are bit rotations of each other or not
Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info
3 min read
PHP Program to Interchange elements of first and last rows in matrix
Given a 4 x 4 matrix, we have to interchange the elements of first and last row and show the resulting matrix.Examples : Input : 3 4 5 0 2 6 1 2 2 7 1 2 2 1 1 2Output : 2 1 1 2 2 6 1 2 2 7 1 2 3 4 5 0Input : 9 7 5 1 2 3 4 1 5 6 6 5 1 2 3 1Output : 1 2 3 1 2 3 4 1 5 6 6 5 9 7 5 1The approach is very
2 min read
PHP Program to Count Rotations Divisible by 8
Given a large positive number as a string, the task is to count all rotations of the given number which are divisible by 8.Examples: Input : 8Output : 1Input : 40Output : 1Rotation : 40 is divisible by 8 04 is not divisible by 8Input : 13502Output : 0No rotation is divisible by 8Input : 43262488612O
3 min read
PHP Program to Count Rotations Divisible by 4
Given a large positive number as string, count all rotations of the given number which are divisible by 4. Examples: Input: 8Output: 1Input: 20Output: 1Rotation: 20 is divisible by 4, 02 is not divisible by 4Input : 13502Output : 0No rotation is divisible by 4Input : 43292816Output : 55 rotations ar
2 min read
PHP Program for Maximum sum of i*arr[i] among all rotations of a given array
Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1.Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8
7 min read
PHP Program to Find Maximum Value of Sum ( i*arr[i]) with Only Rotations on Given Array Allowed
Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i].Examples: Input: Arr = [ 1, 20, 2, 10 ] Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 Input
4 min read
PHP Program to Find lost element from a duplicated array
Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[]
4 min read
PHP Program to Convert Integer to Roman Number
Converting an integer to a Roman number is a common problem often encountered in applications dealing with dates, historical data, and numeral representations. In this article, we will explore various approaches to converting an integer to Roman numerals in PHP. Table of Content Using Simple Mapping
2 min read