find both the smallest and largest elements, then swap them.

0
Mayank Kolwal
Mayank Kolwal
Aug 27, 2026 03:47 PM 0 Answers C++
Member Since Jun 2026
Subscribed Subscribe Not subscribe
Flag(0)

Simple

1 Subscribers
Mayank Kolwal

Select Programming Language

Submit Answer
0 Answers
Sort By:

C++
#include 
using namespace std;

void swapMaxMin(int arr[], int sz) {
    int smallest = 0;
    int largest = 0;

    for (int i = 1; i < sz; i++) {
        if (arr[i]  arr[largest]) {
            largest = i;
        }
    }

    swap(arr[smallest], arr[largest]);
}

int main() {
    int arr[] = {3, 67, 83, 76, 6, 9};
    int sz = 6;

    swapMaxMin(arr, sz);

    for (int i = 0; i < sz; i++) {
        cout << arr[i] << " ";
    }

    return 0;
}
0
🎬 YoKod Reel
Your Text
🎵 Add Music
Aa Add Text
🖼 Add Overlay
✨ Filters
🎚 Edit
⛶ Video Ratio
⏱ Reel Timer
</> Programming Code
Select one or multiple languages
HTML
CSS
JavaScript
TypeScript
PHP
Python
Java
C
C++
C#
SQL
React
JSON
XML
Bash
Kotlin
Swift
Dart
Your programming code is saved separately from the video so visitors can open the Reel and view the code.
Add Music
🎵
the_mountain-phonk-phonk-music-496450 (1)
Original Audio