Same Unit Digit Adjacent Numbers
Coding Problem Keys
Same Unit Digit Adjacent Numbers
Problem Statement
A matrix of size R*C (R rows and C columns) containing numbers (positive integer values) is passed as input. The program must print the count C of numbers in a given cell which has at least one adjacent cell (in the same row) containing a number with the same unit digit.
Input Format
The first line contains R and C separated by a space.
Next R lines contains C values separated by a space.
Output Format
The first line contains C.
Boundary Conditions
1 <= R, C <= 100.
Example Input/Output 1
Input
4 3
20 27 67
13 74 100
37 90 97
53 75 44
Output
2
Explanation
The numbers are 27 67 in the first row where both have 7 as their unit digit.
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: C Language
#include<stdio.h> #include <stdlib.h> int main(){ int r,c,i,j,count=0; scanf("%d%d",&r,&c); int a[r][c]; for(i=0;i<r;i++){ for(j=0;j<c;j++){ scanf("%d",&a[i][j]); } } for(i=0;i<r;i++){ for(j=0;j<c;j++){ if(j==0){ if(a[i][j]%10==a[i][j+1]%10){ count++; } }else if(j==c-1){ if(a[i][j]%10==a[i][j-1]%10){ count++; } }else{ if(a[i][j]%10==a[i][j-1]%10||a[i][j]%10==a[i][j+1]%10){ count++; } } } } printf("%d",count); } //Published By PKJCODERS
Programming Language: C++ Language
#include <iostream> using namespace std; int main() { int i,j,r,c,cn=0; cin>>r>>c; int a[r][c]; for(i=0;i<r;i++) for(j=0;j<c;j++) cin>>a[i][j]; for(i=0;i<r;i++){ for(j=1;j<c-1;j++) if(a[i][j]%10==a[i][j+1]%10||a[i][j]%10==a[i][j-1]%10) cn++; if(a[i][0]%10==a[i][1]%10) cn++; if(a[i][c-1]%10==a[i][c-2]%10) cn++; } cout<<cn; } //Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments