Wednesday, November 29, 2017

Same Unit Digit Adjacent Numbers

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.

Solution:
#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;
}
Share: