You are viewing a single comment's thread. Return to all comments →
Read your input as string and not as integers then the problem can be solved
Thanks that was my mistake
if i take input as string
the how will you print the numbers inside it
you need to use the equivalent of Java's Big Integer after processing the string and parsing it into numbers. Or, in languages like Ruby and Python, you don't need to do anything special to handle very large numbers, much of the time.
Do we really need to parse it into numbers. We can simply take Char 2D array and input each integer as a string. Then divide this string into individual characters (there are multiple ways of doing that). Comparison also works fine if we keep it to char array because ascii value of (char) '9' > '8' > '7' ...'2' > '1'
I tried this one in CPP my code reduced from 75+ lines to 25 lines.
Not always. You can still read it as numbers. I used Java's BigInteger.
What is the point though? It ISN'T an integer, it is a string of integers...
I ended up using BigInteger as well and it ended up being relatively simple. The difficult part was keeping track of the Int/BigInteger operations.
Use Python. You don't have to worry about the type. :P
Ha! So true. I converted the ints to strings to convert them into lists, and solved the problem. I never noticed that I never converted each char in the array back to an int for comparison, but everything worked! Plus, I saw something about 75 lines above?
How did you mutate the str object?
n = int(input())
a = 
for i in range(n):
for i in range(1,n-1):
for j in range(1,n-1):
if a[i][j] > a[i+1][j] and a[i][j] > a[i-1][j] and a[i][j] > a[i][j+1] and a[i][j] > a[i][j+1]:
print("".join(str(x) for x in a[i]))
Can you tell me what the error is?
you can make your code more readable if you stack the if statement like this
for j in range(1,n-1):
if (a[i][j] > a[i+1][j] and a[i][j] > a[i-1][j] and
a[i][j] > a[i][j+1] and a[i][j] > a[i][j-1]):
I also fixed the error in the code. the for loop in your code had j+1 for both the first and second check.
grid[i][j]>=grid[i-1][j-1] && grid[i][j]>=grid[i-1][j] && grid[i][j]>=grid[i-1][j+1]
&& grid[i][j]>=grid[i][j-1] && grid[i][j]>=grid[i][j+1] &&
grid[i][j]>=grid[i+1][j-1] && grid[i][j]>=grid[i+1][j] && grid[i][j]>=grid[i+1][j+1]
how can you compare str values? You first have to convert in into string then you can compare the values
You replace the element with 'X' while iterating, and again whn you are checking the previous element with [j-1] , if it is 'X' it will check if your element is greater than 'X'., so you should check if your [j-1] or [i-1] is 'X' or not. Just add this to your if statement.
Python can evaluate strings including numbers and letters
As you iterate through the grid the cavities are marked with the 'X' where all the adjacent cells are less than. For example
returns with no cavities as 9 and 9 are equal.
If we replace with an X it is because the surrounding cells evaluate as less than.
So if 9 > 8 so too is X > 8 so the relationship between the cells is maintained and there is no need to check for X
9 > 8
X > 8
how does line 3 and 4 work in python??