Bob is doing a research in Pendulum. He is just pushing the pendulum aside and the pendulum started moving in to and fro motion. Bob will push the pendulum always towards his right side to start the oscillation. Bob wanted to calculate the distance between extreme position and the centre position of pendulum for each oscillations. He somehow calculated all the possible distance. Since he is busy in this research he is giving the task to his assistant who needs to arrange the values as instructed.
Since he is pushing the pendulum to his right always. He wanted to store that distance in the right extreme of the arrangement. And the pendulum will move towards the extreme left at that time he want that value to be stored in the left most extreme in the arrangement. And this continues till the pendulum stops. He is also sure that the distance reached at that oscillation will always be lesser than the revious oscillation towards that particular end.
Write a program to arrange the distance as instructed.
Sample Input:
5
13254
Sample Output:
42135
Explanation:
The maximum distance in the given data is 5 hence that is placed in the right most end The next maximum element is 4 which is placed in the left most end.
Again the pendulum oscillates towards right to cover a distance of 3 and this continues.
Solution
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void helper(int* arr, int n) {
// Sort the distances
sort(arr, arr + n);
int* res = new int[n];
// Start from the middle index and alternate between left and right positions
int mid = n / 2;
int left = mid - 1;
int right = mid + 1;
bool isLeft = true;
res[mid] = arr[0];
//sauravhathi
for (int i = 1; i < n; i++) {
// Place the distances alternatively in the left and right positions
if (isLeft) {
res[left] = arr[i];
left--;
} else {
res[right] = arr[i];
right++;
}
// Alternate between left and right positions
isLeft = !isLeft;
}
// Copy the result array to the original array
for (int i = 0; i < n; i++) {
arr[i] = res[i];
}
// Delete the result array to free memory
delete[] res;
}
int main() {
// Read the number of distances and the distances
int n;
cin >> n;
int distances;
cin >> distances;
int* distancesArray = new int[n];
// Convert the distances number to an array of digits
int temp = distances;
for (int i = n - 1; i >= 0; i--) {
distancesArray[i] = temp % 10;
temp /= 10;
}
// Arrange the distances in the array
helper(distancesArray, n);
for (int i = 0; i < n; i++) {
cout << distancesArray[i];
}
// Delete the distances array to free memory
delete[] distancesArray;
}
def arrangeDistances(n, distances):
# Sort the distances
distances.sort()
# Create a result array to store the arranged distances
arrangedDistances = [0] * n
# Start from the middle index and alternate between left and right positions
midIndex = n // 2
leftIndex = midIndex - 1
rightIndex = midIndex + 1
isLeft = True
arrangedDistances[midIndex] = distances[0] # The first distance is placed in the middle
for i in range(1, n):
# Place the distances alternatively in the left and right positions
if isLeft:
arrangedDistances[leftIndex] = distances[i]
leftIndex -= 1
else:
arrangedDistances[rightIndex] = distances[i]
rightIndex += 1
# Alternate between left and right positions
isLeft = not isLeft
# Return the arranged distances array
return arrangedDistances
# Read the number of distances and the distances
n = int(input())
distances = int(input())
# Convert the distances number to an array of digits
distancesArray = [int(digit) for digit in str(distances)]
# Arrange the distances in the array
arrangedDistances = arrangeDistances(n, distancesArray)
# Print the arranged distances
for distance in arrangedDistances:
print(distance, end = "")
import java.util.Arrays;
import java.util.Scanner;
public class PendulumDistanceArranger {
static int[] arrangeDistances(int n, int[] distances) {
// Sort the distances
Arrays.sort(distances);
// Create a result array to store the arranged distances
int[] arrangedDistances = new int[n];
// Start from the middle index and alternate between left and right positions
int midIndex = n / 2;
int leftIndex = midIndex - 1;
int rightIndex = midIndex + 1;
boolean isLeft = true;
// sauravhathi
arrangedDistances[midIndex] = distances[0]; // The first distance is placed in the middle
for (int i = 1; i < n; i++) {
// Place the distances alternatively in the left and right positions
if (isLeft) {
arrangedDistances[leftIndex] = distances[i];
leftIndex--;
} else {
arrangedDistances[rightIndex] = distances[i];
rightIndex++;
}
// Alternate between left and right positions
isLeft = !isLeft;
}
// Return the arranged distances array
return arrangedDistances;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the number of distances and the distances
int n = scanner.nextInt();
int distances = scanner.nextInt();
// Convert the distances number to an array of digits
int[] distancesArray = new int[n];
int temp = distances;
for (int i = n - 1; i >= 0; i--) {
distancesArray[i] = temp % 10;
temp /= 10;
}
// Arrange the distances in the array
int[] arrangedDistances = arrangeDistances(n, distancesArray);
// Print the arranged distances
for (int i = 0; i < n; i++) {
System.out.print(arrangedDistances[i]);
}
}
}
In this program, we define a class called PendulumDistanceArranger
. It contains a static method arrangeDistances
that takes in the number of distances (n
) and an array of distances (distances
) as parameters. This method arranges the distances in a specific way and returns the resulting array.
To begin, we sort the distances
array in ascending order using the Arrays.sort
method. This ensures that the distances are arranged from smallest to largest.
Next, we create an array called arrangedDistances
to store the arranged distances. This array has the same length as the input array.
We determine the middle index of the arrangedDistances
array by dividing the number of distances (n
) by 2. This index represents the starting position for arranging the distances.
We also initialize two variables leftIndex
and rightIndex
to keep track of the left and right positions, respectively. The left position starts from the middle index minus 1, and the right position starts from the middle index plus 1.
We use a boolean flag isLeft
to alternate between placing distances in the left and right positions. Initially, the flag is set to true
.
We start by placing the first distance from the sorted array in the middle position (arrangedDistances[midIndex] = distances[0]
).
Next, we iterate over the remaining distances in the sorted array. For each distance, we check the isLeft
flag. If it is true
, we place the distance in the left position (arrangedDistances[leftIndex] = distances[i]
) and decrement the leftIndex
. Otherwise, we place the distance in the right position (arrangedDistances[rightIndex] = distances[i]
) and increment the rightIndex
.
After placing a distance, we toggle the isLeft
flag by negating its value (isLeft = !isLeft
) to alternate between left and right positions for the next distance.
Once all distances are arranged, we return the arrangedDistances
array.
In the main
method, we create a Scanner
object to read input from the user.
We prompt the user to enter the number of distances and read it using scanner.nextInt()
. We also prompt the user to enter the distances separated by spaces, and we read them into an array called distancesArray
.
We then call the arrangeDistances
method, passing in the number of distances (n
) and the distancesArray
. The returned array is stored in arrangedDistances
.
Finally, we print the arranged distances using a for
loop, displaying each distance separated by a space.
Happy Learning – If you require any further information, feel free to contact me.