CSES Solutions Ferris Wheel (original) (raw)

Last Updated : 23 Jul, 2025

There are **N children who want to go to a Ferris wheel in the form of array **arr[], and your task is to find a gondola for each child. Each gondola may have one or two children in it, and in addition, the total weight in a gondola may not exceed **X. You know the weight of every child. What is the **minimum number of gondolas needed for the children?

**Input: N = 4, X = 10, arr[] = {7, 2, 3, 9}
**Output: 3
**Explanation: We need only 3 gondolas: {2, 3}, {7} and {9}.

**Input: N = 4, X = 6, arr[] = {2, 3, 3, 4}
**Output: 2
**Explanation: We need only 2 gondolas: {2, 4} and {3, 3}

**Approach: To solve the problem, follow the below idea:

We can solve the problem using a **Greedy Approach. In order to have the minimum number of gondolas, we can start from the child with the **largest weight and pair it with the child with the **smallest weight. If the total weight of both the children does not exceed x, then we can fit both the children and start the pairing of child with second largest weight with child with the second smallest weight, and so on. If at any point, the sum of both the children exceeds x, then it means that the heavier child has to go alone and then keep on searching for pairs of students to pair.

**Step-by-step algorithm:

Below is the implementation of the algorithm:

C++ `

#include <bits/stdc++.h> #define ll long long using namespace std;

int solve(ll* arr, ll N, ll X) {

// Sort the array in ascending order
sort(arr, arr + N);

// Maintain two pointers for children with heaviest and
// lightest weight
ll l = 0, h = N - 1;

// Variable to store the number of gondolas
ll ans = 0;

while (h >= l) {
    // If the heaviest and lightest child can fit in a
    // gondola, then pair them up
    if (arr[l] + arr[h] <= X) {
        ans++;
        l++;
        h--;
    }
    // If the heaviest and lightest child cannot fit in
    // a gondola, then put the heaviest child in a
    // separate gondola
    else {
        ans++;
        h--;
    }
}
return ans;

}

int main() { // Sample Input ll N = 4, X = 10; ll arr[N] = { 7, 2, 3, 9 };

cout << solve(arr, N, X) << endl;

}

Java

import java.util.Arrays;

public class GondolaProblem {

// Function to solve the gondola transportation problem
static int solve(long[] arr, int N, long X)
{

    // Sort the array in ascending order
    Arrays.sort(arr);

    // Maintain two pointers for children with heaviest
    // and lightest weight
    int l = 0, h = N - 1;

    // Variable to store the number of gondolas
    int ans = 0;

    // Iterate until the heaviest and lightest pointers
    // meet or cross
    while (h >= l) {
        // If the heaviest and lightest child can fit in
        // a gondola, then pair them up
        if (arr[l] + arr[h] <= X) {
            ans++;
            l++;
            h--;
        }
        // If the heaviest and lightest child cannot fit
        // in a gondola, then put the heaviest child in
        // a separate gondola
        else {
            ans++;
            h--;
        }
    }

    // Return the total number of gondolas needed
    return ans;
}

public static void main(String[] args)
{
    // Sample Input
    int N = 4;
    long X = 10;
    long[] arr = { 7, 2, 3, 9 };

    // Print the result of the solve function
    System.out.println(solve(arr, N, X));
}

}

// This code is contributed by akshitaguprzj3

Python

def solve(arr, N, X): # Sort the array in ascending order arr.sort()

# Maintain two pointers for children with heaviest and lightest weight
l, h = 0, N - 1

# Variable to store the number of gondolas
ans = 0

while h >= l:
    # If the heaviest and lightest child can fit in a gondola, then pair them up
    if arr[l] + arr[h] <= X:
        ans += 1
        l += 1
        h -= 1
    # If the heaviest and lightest child cannot fit in a gondola,
    # then put the heaviest child in a separate gondola
    else:
        ans += 1
        h -= 1

return ans

Sample Input

N = 4 X = 10 arr = [7, 2, 3, 9]

print(solve(arr, N, X))

C#

using System;

class Program { static int Solve(long[] arr, int N, long X) { // Sort the array in ascending order Array.Sort(arr);

    // Maintain two pointers for children with heaviest and
    // lightest weight
    int l = 0, h = N - 1;

    // Variable to store the number of gondolas
    int ans = 0;

    while (h >= l)
    {
        // If the heaviest and lightest child can fit in a
        // gondola, then pair them up
        if (arr[l] + arr[h] <= X)
        {
            ans++;
            l++;
            h--;
        }
        // If the heaviest and lightest child cannot fit in
        // a gondola, then put the heaviest child in a
        // separate gondola
        else
        {
            ans++;
            h--;
        }
    }
    return ans;
}

static void Main()
{
    // Sample Input
    int N = 4;
    long X = 10;
    long[] arr = { 7, 2, 3, 9 };

    // Output the result
    Console.WriteLine(Solve(arr, N, X));
}

}

JavaScript

function solve(arr, N, X) { // Sort the array in ascending order arr.sort((a, b) => a - b);

// Maintain two pointers for children with heaviest and
// lightest weight
let l = 0,
    h = N - 1;

// Variable to store the number of gondolas
let ans = 0;

while (h >= l) {
    // If the heaviest and lightest child can fit in a
    // gondola, then pair them up
    if (arr[l] + arr[h] <= X) {
        ans++;
        l++;
        h--;
    }
    // If the heaviest and lightest child cannot fit in
    // a gondola, then put the heaviest child in a
    // separate gondola
    else {
        ans++;
        h--;
    }
}
return ans;

}

function main() { // Sample Input const N = 4, X = 10; const arr = [7, 2, 3, 9];

console.log(solve(arr, N, X));

}

main();

`

**Time Complexity: O(NlogN), where **N is the total number of children.
**Auxiliary Space: O(1)