กำหนดอาร์เรย์ขนาด NxM โดยที่ N จำนวนแถวและจำนวนคอลัมน์ M และภารกิจคือการพิมพ์จำนวนศูนย์ในทุกคอลัมน์ของเมทริกซ์ที่สอดคล้องกันหลังจากดำเนินการจัดเรียงตามจำนวนศูนย์ที่มีอยู่ในคอลัมน์ใดๆ
ตัวอย่างเช่น ถ้า 1 st คอลัมน์มี 1 ศูนย์และ 2 nd คอลัมน์ไม่มีเลขศูนย์และ 3 rd คอลัมน์มีศูนย์ 2 ตัว ผลลัพธ์ควรเป็น − 3 1 2
ตัวอย่าง
Input: 0 0 0 1 1 1 1 0 1 Output: 1 3 2
คำอธิบาย

หมายเหตุ − เมทริกซ์ถือว่าเริ่มต้นจากดัชนี 1
ตัวอย่าง
#include <bits/stdc++.h>
#define row 3
#define col 3
using namespace std;
void sorting(int arr[row][col]){
vector<pair<int, int> >count_zero;
for (int i = 0; i < col; i++){
int count = 0;
for (int j = 0; j < row; j++){
if (arr[j][i] == 0)
count++;
}
count_zero.push_back(make_pair(count, i));
}
sort(count_zero.begin(), count_zero.end());
for (int i = 0; i < col; i++)
cout<< count_zero[i].second + 1 << " ";
}
int main(){
int array[row][col] = {
{ 0, 0, 0 },
{ 1, 1, 1 },
{ 1, 0, 1 }
};
cout<<"sorted order of zeroes count is : ";
sorting(array);
return 0;
} ผลลัพธ์
หากเรารันโปรแกรมข้างต้น มันจะสร้างผลลัพธ์ดังต่อไปนี้
sorted order of zeroes count is : 1 3 2