Given a squared sized grid G of size N in which each cell has a lowercase letter. Denote the character in the ith row and in the jth column as G[i][j].
You can perform one operation as many times as you like: Swap two column adjacent characters in the same row G[i][j] and G[i][j+1] for all valid i,j.
Is it possible to rearrange the grid such that the following condition is true?
G[i]≤G[i]≤⋯≤G[i][N] for 1≤i≤N and
G[j]≤G[j]≤⋯≤G[N][j] for 1≤j≤N
In other words, is it possible to rearrange the grid such that every row and every column is lexicographically sorted?
Note: c1≤c2, if letter c1 is equal to c2 or is before c2 in the alphabet.
The first line begins with T, the number of testcases. In each testcase you will be given N. The following N lines contain N lowercase english alphabet each, describing the grid.
Print T lines. On the ith line print
YES if it is possible to rearrange the grid in the ith testcase or
Gij will be a lower case letter
- Sort each row
- Then check each column to check whether numbers are in increasing order or not
Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right. If there are no elements to the left/right, then the sum is considered to be zero.
Formally, find an i, such that, A1+A2...Ai-1 =Ai+1+Ai+2...AN.
The first line contains T, the number of test cases. For each test case, the first line contains N, the number of elements in the array A. The second line for each test case contains Nspace-separated integers, denoting the array A.
For each test case print
YES if there exists an element in the array, such that the sum of the elements on its left is equal to the sum of the elements on its right; otherwise print
1 2 3
1 2 3 3
For the first test case, no such index exists.
For the second test case, A+A=A, therefore index 3 satisfies the given conditions.
- First calculate the sum of all elements
- Maintain curr as the cumulated sum in the left parts, when traversing the array
- if (curr == sum – curr – arr[i]), then it means the sum in the left part and right part of arr[i] equals
- O(n), as the array has been traversed twice