Hackerrank Grid Challenge Solution

This is the solution of Grid Challenge in Greedy sub-domain in Hackerrank. For the question you refer Here .

Output :

Code :

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<vector<char>> s;
        for(int j=0;j<n;j++)
        {
            vector<char> t;
            for(int i=0;i<n;i++)
            {
                char ch;
                cin>>ch;
                t.push_back(ch);
            }
            s.push_back(t);
        }
        for(int i=0;i<n;i++)
        {
            sort(s[i].begin(),s[i].end());
        }
        int flag=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                if(s[j][i]>s[j+1][i])
                {
                    flag=1;
                    break;
                }
            }
        }
    if(flag==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;

    }
}


Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution