C++ Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0,
6 min read
C++ Program to Print a 2D Array Here we will see how to print a 2D array using a C++ program. Below are the examples: Input: {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output: 1 2 3 4 5 6 7 8 9 Input: {{11, 12, 13}, {14, 15, 16}}Output: 11 12 13 14 15 16 There are 2 ways to print a 2D array in C++: Using for loop.Using range-based for loop.
2 min read
C++ Program for Sorting array except elements in a subarray Given an array A positive integers, sort the array in ascending order such that element in given subarray (start and end indexes are input) in unsorted array stay unmoved and all other elements are sorted.Examples : Input : arr[] = {10, 4, 11, 7, 6, 20} l = 1, u = 3 Output : arr[] = {6, 4, 11, 7, 10
2 min read
C++ Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:Â Â Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9
3 min read
Sorting Vector of Arrays in C++ Given a vector of arrays, the task is to sort them. Examples: Input: [[1, 2, 3], [10, 20, 30], [30, 60, 90], [10, 20, 10]] Output: [[1, 2, 3], [10, 20, 10], [10, 20, 30], [30, 60, 90]] Input: [[7, 2, 9], [5, 20, 11], [6, 16, 19]] Output: [[5, 20, 11], [6, 16, 19], [7, 2, 9]] Approach: To sort the Ve
2 min read