Print Distinct Elements in Alternating Order: Smallest, Largest, and Beyond
Asked 3 months ago
0Comments
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 arrayarr
๐ฆ.
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
andright
) 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 โ .
Share