0% found this document useful (0 votes)
10 views3 pages

Sorting (Advanced) (Page 4 of 5)

Uploaded by

ledinhduc1879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Sorting (Advanced) (Page 4 of 5)

Uploaded by

ledinhduc1879
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11/10/23, 12:46 AM Sorting (Advanced) (page 4 of 5)

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652&page=3 1/3
Question 4
11/10/23, 12:46 AM Sorting (Advanced) (page 4 of 5)

Correct
Marked out of 2.00

Given a list of distinct unsorted integers nums.


Your task is to implement a function with following prototype:
int minDiffPairs(int* arr, int n);
This function identify and return all pairs of elements with the smallest absolute difference among them. If there are multiple pairs that
meet this criterion, the function should find and return all of them.
Note: Following libraries are included: iostream, string, algorithm, sstream
For example:
Test Result
int arr[] = {10, 5, 7, 9, 15, 6, 11, 8, 12, 2}; (5, 6), (6, 7), (7, 8), (8, 9), (9, 10), (10, 11), (11, 12)
cout << minDiffPairs(arr, 10);

int arr[] = {10};


cout << minDiffPairs(arr, 1);

int arr[] = {10, -1, -150, 200}; (-1, 10)


cout << minDiffPairs(arr, 4);

Answer: (penalty regime: 0 %)


Reset answer
1 ▼ string minDiffPairs(int* arr, int n){
2 // STUDENT ANSWER
3 sort(arr, arr + n);
4 int min = 2147483647;
5 ▼ for (int i = 1; i < n; i++){
6 if (arr[i] - arr[i - 1] < min) min = arr[i] - arr[i - 1];
7 }
8 string s = "";
9 ▼ for (int i = 1; i < n; i++){
10 ▼ if (arr[i] - arr[i - 1] == min) {
11 s = s + "(" + to_string(arr[i - 1]) + ", " + to_string(arr[i]) + ")
12 }
13 }
14 int size = s.size();
15 return s.substr(0, size - 2);
16 }

Precheck Check

Test Expected Got


 int arr[] = {10, 5, 7, 9, 15, 6, (5, 6), (6, 7), (7, 8), (8, 9), (9, (5, 6), (6, 7), (7, 8), (8, 9), 
11, 8, 12, 2}; 10), (10, 11), (11, 12) (9, 10), (10, 11), (11, 12)
cout << minDiffPairs(arr, 10);

 int arr[] = {10}; 


cout << minDiffPairs(arr, 1);

 int arr[] = {10, -1, -150, 200}; (-1, 10) (-1, 10) 
cout << minDiffPairs(arr, 4);

Passed all tests! 

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652&page=3 2/3
11/10/23, 12:46 AM Sorting (Advanced) (page 4 of 5)

BÁCH KHOA E-LEARNING

WEBSITE

HCMUT
MyBK
BKSI
CONTACT
 268 Ly Thuong Kiet Street Ward 14, District 10, Ho Chi Minh City, Vietnam
 (028) 38 651 670 - (028) 38 647 256 (Ext: 5258, 5234)

[email protected]

Copyright 2007-2022 BKEL - Power by Moodle

https://e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=1579199&cmid=200652&page=3 3/3

You might also like