Count no. of columns that are not sorted in increasing order
Last Updated :
21 Sep, 2023
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;
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 {
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;
}
public static void main(String[] args) {
String[] A = { "cba" , "daf" , "ghi" };
System.out.println(countUnsorted(A));
}
}
|
Python3
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
A = ["cba", "daf", "ghi"]
print (countUnsorted(A))
|
C#
using System;
using System.Collections.Generic;
class Program {
static int CountUnsorted(List< string > A)
{
int countOfCol = 0;
foreach ( var col in GetColumns(A))
{
bool isUnsorted = false ;
for ( int i = 0; i < col.Length - 1; i++) {
if (col[i] > col[i + 1]) {
isUnsorted = true ;
break ;
}
}
if (isUnsorted)
countOfCol++;
}
return countOfCol;
}
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
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;
}
let A = [ "cba" , "daf" , "ghi" ];
console.log(countUnsorted(A));
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...