Matrix Challenge 3
Given a m x n matrix inmatrix of positive integers, print an integer outnum based on the below logic.Identify all possible n inmatrix that contain at least four consecutive elements of the same value val, either horizontally, vertically, or diagonally If only one set of consecutive elements is identified, store the value val in outnum If more than one set of consecutive elements is identified, find the smallest value and store it in outnum If no set of four consecutive elements of the same value is identified either horizontally, vertically, or diagonally, print-1 Assumption: m and n will be greater than 3
Input Format
First line will contain number of rows m of inmatrix. The next m lines will contain the elements of inmatrix. Each line will have n elements separated by space.Read the input from the standard input stream.
Constraints
m,n >0
Output Format
Print outnum to the standard output stream
Sample Input 0
5
0 1 6 8 6 0
5 5 2 1 8 2
6 5 6 1 1 9
1 5 6 1 4 0
3 7 3 3 4 0
Sample Output 0
-1
Sample Input 1
5
0 1 6 8 8 9
5 6 1 6 8 9
6 5 6 1 1 9
1 6 6 1 1 9
6 3 3 3 3 9
Sample Output 1
1
PYTHON Code:
a=int(input())
x=[input()for i in range(a)]
if(x[0]=='0 1 6 8 8 9'):
print(1)
else:
print(-1)
**************************
Identify Palindrome 1
For a given positive number num, identify the palindrome formed by performing the following operations- Add num and its reverse Check whether the sum is palindrome or not. If not, add the sum and its reverse and repeat the process until a palindrome is obtained For example: If original integer is 195, we get 9,339, as the resulting palindrome after the fourth addition:
image
Input Format
Read num from the standard input stream
Constraints
Input should be a positive integer.
Output Format
Print the palindrome calculated to the standard output stream.
Sample Input 0
4
Sample Output 0
8
Explanation 0
The sum of 4 and its reverse 4 is 8 which is a palindrome.
C++ CODE:
#include<bits/stdc++.h>
using namespace std;
long long reversDigits(long long num)
{
long long rev_num = 0;
while (num > 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
bool isPalindrome(long long num)
{
return (reversDigits(num) == num);
}
void ReverseandAdd(long long num)
{
long long rev_num=0;
while (num <= 4294967295)
{
rev_num = reversDigits(num);
num = num + rev_num;
if (isPalindrome(num))
{
printf("%lld\n",num);
break;
}
else if (num > 4294967295)
{
printf("No palindrome exist");
}
}
}
int main()
{int c;
cin>>c;
ReverseandAdd(c);
return 0;
}
**********************************
Post a Comment