Count no. of columns that are not sorted in increasing order
Last Updated :
10 May, 2025
Given an array of strings arr[], where all the strings are of the same length. The task is to count how many columns are not sorted in increasing alphabetical order from top to bottom.
Examples:
Input: arr[] = ["cba", "dah", "ghi"]
Output: 1
Explanation: The 2nd column contains ["b", "a", "h"], which is not in increasing order. The other columns ["c", "d", "g"] and ["a", "h", "i"] are sorted.
Input: arr[] = ["zyx", "wvu", "tsr"]
Output: 3
Explanation: All columns are unsorted:
1st column: ["z", "w", "t"],
2nd column: ["y", "v", "s"],
3rd column: ["x", "u", "r"].
Input: arr[] = ["abc", "bcd", "cde"]
Output: 0
Explanation: All columns are sorted:
1st column: ["a", "b", "c"],
2nd column: ["b", "c", "d"],
3rd column: ["c", "d", "e"].
Approach:
The idea is to treat each vertical slice of the array as a column and check whether it is sorted from top to bottom. The thought process is to loop through each column index and compare characters from one row to the next. If we find any character that is greater than the one below, we know the column is unsorted, so we increment the count.
Steps to implement the above idea:
- Initialize rows, cols from the size of the input array and set count to 0.
- Loop through each column from index 0 to cols - 1 using an outer loop.
- For every column, use an inner loop to check each row from 0 to rows - 2.
- Compare the character at arr[row][col] with the one just below it arr[row + 1][col].
- If the upper character is greater, increment count and break the inner loop.
- Repeat the process for all columns to count how many are not sorted.
- Finally, return the count as the result.
C++
// C++ program to count unsorted columns
// in an array of strings
#include <iostream>
#include <vector>
using namespace std;
// Function to count the number of unsorted columns
int countUnsortedColumns(vector<string> &arr) {
int rows = arr.size();
int cols = arr[0].size();
int count = 0;
// Traverse each column
for (int col = 0; col < cols; col++) {
// Check if column is sorted from top to bottom
for (int row = 0; row < rows - 1; row++) {
// If current character > next row character,
// it's unsorted
if (arr[row][col] > arr[row + 1][col]) {
count++;
break;
}
}
}
return count;
}
// Driver code
int main() {
vector<string> arr = {"cba", "dah", "ghi"};
cout << countUnsortedColumns(arr);
return 0;
}
Java
// Java program to count unsorted columns
// in an array of strings
import java.util.*;
class GfG {
// Function to count the number of unsorted columns
static int countUnsortedColumns(String[] arr) {
int rows = arr.length;
int cols = arr[0].length();
int count = 0;
// Traverse each column
for (int col = 0; col < cols; col++) {
// Check if column is sorted from top to bottom
for (int row = 0; row < rows - 1; row++) {
// If current character > next row character,
// it's unsorted
if (arr[row].charAt(col) > arr[row + 1].charAt(col)) {
count++;
break;
}
}
}
return count;
}
public static void main(String[] args) {
String[] arr = {"cba", "dah", "ghi"};
System.out.println(countUnsortedColumns(arr));
}
}
Python
# Python program to count unsorted columns
# in an array of strings
# Function to count the number of unsorted columns
def countUnsortedColumns(arr):
rows = len(arr)
cols = len(arr[0])
count = 0
# Traverse each column
for col in range(cols):
# Check if column is sorted from top to bottom
for row in range(rows - 1):
# If current character > next row character,
# it's unsorted
if arr[row][col] > arr[row + 1][col]:
count += 1
break
return count
# Driver code
if __name__ == "__main__":
arr = ["cba", "dah", "ghi"]
print(countUnsortedColumns(arr))
C#
// C# program to count unsorted columns
// in an array of strings
using System;
class GfG {
// Function to count the number of unsorted columns
static int countUnsortedColumns(string[] arr) {
int rows = arr.Length;
int cols = arr[0].Length;
int count = 0;
// Traverse each column
for (int col = 0; col < cols; col++) {
// Check if column is sorted from top to bottom
for (int row = 0; row < rows - 1; row++) {
// If current character > next row character,
// it's unsorted
if (arr[row][col] > arr[row + 1][col]) {
count++;
break;
}
}
}
return count;
}
static void Main() {
string[] arr = {"cba", "dah", "ghi"};
Console.WriteLine(countUnsortedColumns(arr));
}
}
JavaScript
// JavaScript program to count unsorted columns
// in an array of strings
// Function to count the number of unsorted columns
function countUnsortedColumns(arr) {
let rows = arr.length;
let cols = arr[0].length;
let count = 0;
// Traverse each column
for (let col = 0; col < cols; col++) {
// Check if column is sorted from top to bottom
for (let row = 0; row < rows - 1; row++) {
// If current character > next row character,
// it's unsorted
if (arr[row][col] > arr[row + 1][col]) {
count++;
break;
}
}
}
return count;
}
// Driver code
let arr = ["cba", "dah", "ghi"];
console.log(countUnsortedColumns(arr));
Time Complexity: O(n*m), as we check all characters column-wise, where n and m are number of rows and columns respectively.
Space Complexity: O(1), as we use only constant extra space.
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem