Arranging of flower sticks in a bouquet problem | FACE Prep

Arranging of flower sticks in a bouquet problem | FACE Prep

Arranging Flower Sticks in a Bouquet: A Coding Challenge

Introduction

Emma wants to surprise her father with a beautifully arranged bouquet on his birthday. To help her, her mother, Rosy, gives her N flower sticks numbered 1 to N and instructs her to arrange them in a specific order:

  • The first K flower sticks should be arranged in increasing order of their length.
  • The remaining sticks should be arranged in decreasing order of their length.

This problem is commonly asked in recruitment drives, particularly in coding assessments. Let’s explore how to solve it efficiently!

Problem Statement

Input:

The function/method receives three inputs:

  1. num (integer): Number of flower sticks (N).
  2. random (integer): The count K of flower sticks to be sorted in ascending order.
  3. arr (list of integers): A list representing the lengths of flower sticks.

Output:

Return a list of integers representing the final bouquet arrangement.

Constraints:

  • random < num
  • 0 < num < 10^6

Example Walkthrough

Example Input:

num = 7
random = 3
arr = [8, 3, 6, 7, 2, 9, 5]

Processing:

  • The first 3 elements [8, 3, 6] should be sorted in increasing order[3, 6, 8]
  • The remaining elements [7, 2, 9, 5] should be sorted in decreasing order[9, 7, 5, 2]

Final Output:

[3, 6, 8, 9, 7, 5, 2]

Algorithm & Approach

  1. Extract the first K elements and sort them in ascending order.
  2. Extract the remaining elements and sort them in descending order.
  3. Concatenate both sorted parts to get the final arrangement.

Code Implementation

Python Solution:

def arrange_flower_sticks(num, random, arr):
    first_part = sorted(arr[:random])  # Sort first K elements in ascending order
    second_part = sorted(arr[random:], reverse=True)  # Sort remaining elements in descending order
    return first_part + second_part  # Combine both parts

# Example usage
num = 7
random = 3
arr = [8, 3, 6, 7, 2, 9, 5]
print(arrange_flower_sticks(num, random, arr))

C++ Solution:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> arrangeFlowerSticks(int num, int random, vector<int>& arr) {
    sort(arr.begin(), arr.begin() + random);  // Sort first K elements in ascending order
    sort(arr.begin() + random, arr.end(), greater<int>());  // Sort remaining in descending order
    return arr;
}

int main() {
    vector<int> arr = {8, 3, 6, 7, 2, 9, 5};
    int num = 7, random = 3;
    vector<int> result = arrangeFlowerSticks(num, random, arr);
    for (int val : result) cout << val << " ";
    return 0;
}

Java Solution:

import java.util.*;

public class FlowerBouquet {
    public static List<Integer> arrangeFlowerSticks(int num, int random, List<Integer> arr) {
        List<Integer> firstPart = arr.subList(0, random);
        List<Integer> secondPart = arr.subList(random, num);
        Collections.sort(firstPart);  // Sort first K elements in ascending order
        secondPart.sort(Collections.reverseOrder());  // Sort remaining in descending order
        firstPart.addAll(secondPart);
        return firstPart;
    }
    
    public static void main(String[] args) {
        List<Integer> arr = Arrays.asList(8, 3, 6, 7, 2, 9, 5);
        int num = 7, random = 3;
        System.out.println(arrangeFlowerSticks(num, random, arr));
    }
}

Time & Space Complexity Analysis

  • Sorting Complexity: O(K log K + (N-K) log (N-K))
  • Space Complexity: O(1) (in-place sorting)

SEO-Optimized Keywords & Topics

  • Coding Interview Questions
  • Sorting Algorithm Applications
  • Python, C++, and Java Programming
  • Technical Interview Preparation
  • Wipro Recruitment Questions

Conclusion

This problem is a great example of using sorting techniques efficiently to solve real-world tasks. It frequently appears in coding assessments, so practicing this will improve your problem-solving skills. Try implementing it in different languages and optimizing your solution further!

Arranging Flower Sticks in a Bouquet: A Coding Challenge