Tuesday, January 14, 2025
Google search engine
HomeData Modelling & AICount no. of columns that are not sorted in increasing order

Count no. of columns that are not sorted in increasing order

Given an array A of N lowercase letter strings of the same length. The task is to find the count of columns that are not sorted in increasing order. Examples:

Input: A = ["cba", "dah", "ghi"]
Output: 1
2nd Column ["b", "a", "h"] is not sorted in increasing order.
Input: A = ["zyx", "wvu", "tsr"]
Output: 3
All columns are not sorted in increasing order.

Approach: Traverse each column one by one and check if the next element is greater than the previous element in the same column. If not the increment the countOfCol by 1 and keep traversing till all the columns get traversed. Print the value of countOfCol

C++




#include <iostream>
#include <vector>
 
using namespace std;
 
// Function to count the unsorted columns
int countUnsorted(vector<string>& A) {
    int countOfCol = 0;
 
    for (int col = 0; col < A[0].length(); col++) {
        bool isUnsorted = false;
        for (int row = 1; row < A.size(); row++) {
            if (A[row][col] < A[row - 1][col]) {
                isUnsorted = true;
                break;
            }
        }
        if (isUnsorted) {
            countOfCol++;
        }
    }
 
    return countOfCol;
}
 
int main() {
    vector<string> A = {"cba", "daf", "ghi"};
    cout << countUnsorted(A) << endl;
    return 0;
}


Java




import java.util.Arrays;
 
public class Main {
    // Function to count the unsorted columns
    static int countUnsorted(String[] A) {
        int countOfCol = 0;
 
        for (int col = 0; col < A[0].length(); col++) {
            boolean isUnsorted = false;
            for (int row = 1; row < A.length; row++) {
                if (A[row].charAt(col) < A[row - 1].charAt(col)) {
                    isUnsorted = true;
                    break;
                }
            }
            if (isUnsorted) {
                countOfCol++;
            }
        }
 
        return countOfCol;
    }
 
    // Driver code
    public static void main(String[] args) {
        String[] A = {"cba", "daf", "ghi"};
        System.out.println(countUnsorted(A));
    }
}


Python3




# function to count the unsorted columns
def countUnsorted(A):
 
    countOfCol = 0
 
    for col in zip(*A):
        if any(col[i] > col[i + 1] for i in range(len(col) - 1)):
            countOfCol += 1
 
    return countOfCol
 
# Driver code
A = ["cba", "daf", "ghi"]
print(countUnsorted(A))


C#




using System;
using System.Collections.Generic;
 
class Program {
    // Function to count the unsorted columns
    static int CountUnsorted(List<string> A)
    {
        int countOfCol = 0;
 
        // Iterate over each column
        foreach(var col in GetColumns(A))
        {
            bool isUnsorted = false;
 
            // Check if any element in the column is greater
            // than the next element
            for (int i = 0; i < col.Length - 1; i++) {
                if (col[i] > col[i + 1]) {
                    isUnsorted = true;
                    break;
                }
            }
 
            if (isUnsorted)
                countOfCol++;
        }
 
        return countOfCol;
    }
 
    // Helper function to get the columns
    static IEnumerable<string> GetColumns(List<string> A)
    {
        int maxLength = A[0].Length;
 
        for (int i = 0; i < maxLength; i++) {
            string column = "";
 
            foreach(string row in A)
            {
                if (i < row.Length)
                    column += row[i];
            }
 
            yield return column;
        }
    }
 
    static void Main(string[] args)
    {
        List<string> A
            = new List<string>{ "cba", "daf", "ghi" };
        Console.WriteLine(CountUnsorted(A));
    }
}


Javascript




// Javascript code addition
 
// Function to count the unsorted columns
function countUnsorted(A) {
  let countOfCol = 0;
 
  for (let col of A[0].split("").keys()) {
    for (let row = 0; row < A.length - 1; row++) {
      if (A[row][col] > A[row + 1][col]) {
        countOfCol++;
        break;
      }
    }
  }
 
  return countOfCol;
}
 
// Driver code
let A = ["cba", "daf", "ghi"];
console.log(countUnsorted(A));
 
// The code  is contributed by Nidhi goel.


Output

1





Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
21 Sep, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments