Print Distinct Elements in Alternating Order: Smallest, Largest, and Beyond

clock icon

Asked 3 months ago

message

0Comments

eye

0 Views

Here ! we tackle an interesting ๐Ÿง  problem: given an ๐Ÿงฎ array of integers, we aim to print all its ๐Ÿ” distinct elements, arranged in a specific โ†”๏ธ alternating order of smallest, largest, second smallest, second largest, and so on. This challenge not only tests our ability to handle arrays ๐Ÿ“ฆ and remove duplicates โŒ but also involves sorting ๐Ÿ“Š and creative output manipulation. ๐Ÿ› ๏ธ


Problem Statement

๐ŸŽฏ Given an ๐Ÿงฎ array of integers, print the distinct elements such that the output alternates between the smallest ๐Ÿ”ฝ and largest ๐Ÿ”ผ values, followed by the second smallest and second largest, and so on.

Input Format ๐Ÿ“

๐Ÿ’ป A single line containing an array of integers.

Constraints โ›“๏ธ

  • ๐Ÿ”ข All numbers in the input array are integers.

Output Format ๐Ÿ–จ๏ธ

Print the distinct integers in the specified โ†”๏ธ alternating order.


Example

Input:

1 7 9 1 4 3 2 4

Output:

1 9 2 7 3 4

Approach ๐Ÿš€

To solve this problem, weโ€™ll break it down into smaller steps:

1๏ธโƒฃ Parse Input: Read the ๐Ÿงฎ array of integers as input. 2๏ธโƒฃ Remove Duplicates: Use a data structure ๐Ÿ“‚ to remove duplicates and sort the elements. 3๏ธโƒฃ Arrange in Alternating Order: Use two pointers ๐Ÿ‘†๐Ÿ‘‡ to alternate between the smallest and largest elements. 4๏ธโƒฃ Output the Result: Print ๐Ÿ–จ๏ธ the processed array in the desired order.


Step-by-Step Solution ๐Ÿ› ๏ธ

Step 1: Parse Input ๐Ÿ“ฅ

Weโ€™ll read the input as a single line of ๐Ÿ“ text and extract integers ๐Ÿ”ข from it. This step ensures flexibility in handling input formats.

Step 2: Remove Duplicates โŒ

To remove duplicates and maintain sorted order, weโ€™ll use a std::set. This data structure automatically handles duplicate removal and keeps elements in ascending order ๐Ÿ”ผ.

Step 3: Arrange in Alternating Order โ†”๏ธ

With the distinct and sorted elements, we use two pointers ๐Ÿ‘†๐Ÿ‘‡: one starting from the smallest element ๐Ÿ”ฝ and the other from the largest ๐Ÿ”ผ. By alternating between these pointers, we can construct the desired order.

Step 4: Output the Result ๐Ÿ–จ๏ธ

Finally, print ๐Ÿ–จ๏ธ the elements with appropriate formatting.


Code Implementation ๐Ÿ’ป

low is the complete implementation in C++:

#include <iostream> ๐Ÿ“ฅ
#include <vector> ๐Ÿ“ฆ
#include <set> ๐Ÿ“‚
#include <algorithm> ๐Ÿ”„
using namespace std; ๐Ÿš€

int main() {
    // Step 1: Parse Input ๐Ÿ“ฅ
    string input;
    getline(cin, input);

    vector<int> arr;
    int num;
    for (size_t i = 0; i < input.size(); ++i) {
        if (isdigit(input[i]) || input[i] == '-') {
            size_t idx;
            num = stoi(input.substr(i), &idx);
            arr.push_back(num);
            i += idx - 1;
        }
    }

    // Step 2: Remove Duplicates โŒ and Sort ๐Ÿ”ผ
    set<int> uniqueElements(arr.begin(), arr.end());
    vector<int> sortedElements(uniqueElements.begin(), uniqueElements.end());

    // Step 3: Arrange in Alternating Order โ†”๏ธ
    vector<int> result;
    int left = 0, right = sortedElements.size() - 1;
    while (left <= right) {
        result.push_back(sortedElements[left++]);
        if (left <= right) result.push_back(sortedElements[right--]);
    }

    // Step 4: Output the Result ๐Ÿ–จ๏ธ
    for (size_t i = 0; i < result.size(); ++i) {
        if (i > 0) cout << " ";
        cout << result[i];
    }
    cout << endl;

    return 0;
}

Explanation of Code ๐Ÿงต
Input Parsing ๐Ÿ“ฅ
getline(cin, input);

๐Ÿ“œ Reads the input as a single line of ๐Ÿ“ text.
for (size_t i = 0; i < input.size(); ++i) {
    if (isdigit(input[i]) || input[i] == '-') {
        size_t idx;
        num = stoi(input.substr(i), &idx);
        arr.push_back(num);
        i += idx - 1;
    }
}
 
  • ๐Ÿ”„ Loops through the input string ๐Ÿงต, extracts integers ๐Ÿ”ข using stoi(), and appends them to the array arr ๐Ÿ“ฆ.

Removing Duplicates โŒ

set<int> uniqueElements(arr.begin(), arr.end());
vector<int> sortedElements(uniqueElements.begin(), uniqueElements.end());
  • A std::set ๐Ÿ“‚ is initialized with the array, removing duplicates โŒ and sorting the elements ๐Ÿ”ผ.
  • The sorted elements are transferred to a vector ๐Ÿ“ฆ for further manipulation.

Alternating Order โ†”๏ธ

 
int left = 0, right = sortedElements.size() - 1;
while (left <= right) {
    result.push_back(sortedElements[left++]);
    if (left <= right) result.push_back(sortedElements[right--]);
}

  • Two pointers ๐Ÿ‘†๐Ÿ‘‡ (left and right) are used to alternately pick elements ๐Ÿ” from the smallest ๐Ÿ”ฝ and largest ๐Ÿ”ผ ends of the sorted array until all elements are used.

Output ๐Ÿ–จ๏ธ

 
for (size_t i = 0; i < result.size(); ++i) {
    if (i > 0) cout << " ";
    cout << result[i];
}
cout << endl;
 
  • The result vector ๐Ÿ“ฆ is printed with spaces โž– between the elements.

Example Walkthrough ๐Ÿ‘ฃ

Input:

1 7 9 1 4 3 2 4

Execution ๐Ÿ”„:

1๏ธโƒฃ Parse Input: arr = [1, 7, 9, 1, 4, 3, 2, 4]

2๏ธโƒฃ Remove Duplicates: uniqueElements = {1, 2, 3, 4, 7, 9} sortedElements = [1, 2, 3, 4, 7, 9]

3๏ธโƒฃ Alternating Order: result = [1, 9, 2, 7, 3, 4]

4๏ธโƒฃ Output:

1 9 2 7 3 4

Complexity Analysis ๐Ÿ“Š

1๏ธโƒฃ Input Parsing: , where is the length of the input string ๐Ÿงต. 2๏ธโƒฃ Removing Duplicates: for inserting elements into the set ๐Ÿ“‚. 3๏ธโƒฃ Alternating Order Construction: , where is the number of unique elements ๐Ÿ”ข.

Overall: ๐Ÿš€


Key Takeaways ๐Ÿ—๏ธ

1๏ธโƒฃ Using Sets: std::set is an efficient ๐Ÿ’ก way to remove duplicates โŒ and sort elements ๐Ÿ”ผ. 2๏ธโƒฃ Two-Pointer Technique: Ideal for alternating โ†”๏ธ between the smallest ๐Ÿ”ฝ and largest ๐Ÿ”ผ values. 3๏ธโƒฃ Flexible Input Handling: Parsing strings ๐Ÿงต with mixed characters is crucial ๐ŸŒŸ for real-world ๐ŸŒ scenarios.

This problem demonstrates how combining simple ๐Ÿ› ๏ธ data structures ๐Ÿ“‚ and algorithms ๐Ÿ”„ can solve complex tasks effectively โœ….

easy

Share

Write your comment here !

0 Responses