Count non-palindromic array elements having same first and last digit
Last Updated :
03 May, 2021
Given an array arr[] of size N, the task is to print the count of non-palindromic numbers present in the given array whose first and last digit is same.
Examples:
Input:arr[]={121, 134, 2342, 4514}
Output: 2
Explanation: 2342 and 4514 are the non-palindromic numbers having same first and last digits.Therefore, the required output is 2.
Input: arr[]={1, 22, 4545}
Output: 0
Approach: The problem can be solved by checking for each array element, whether it is palindrome or not. Follow the steps to solve the problem.
- Traverse the array arr[].
- For every array element arr[i], check if it is palindrome or not.
- For every array element found to be non-palindromic, extract the last digits before as well as after reversing the number. Once extracted, check if the digits are equal or not.
- If found to be true, increase count.
- Finally, print the count of such numbers.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int revNum( int N)
{
int x = 0;
while (N) {
x = x * 10 + N % 10;
N = N / 10;
}
return x;
}
int ctNonPalin( int arr[], int N)
{
int Res = 0;
for ( int i = 0; i < N; i++) {
int x = revNum(arr[i]);
if (x == arr[i]) {
continue ;
}
else {
Res += (arr[i] % 10 == N % 10);
}
}
return Res;
}
int main()
{
int arr[] = { 121, 134, 2342, 4514 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << ctNonPalin(arr, N);
}
|
Java
import java.io.*;
class GFG{
static int revNum( int N)
{
int x = 0 ;
while (N != 0 )
{
x = x * 10 + N % 10 ;
N = N / 10 ;
}
return x;
}
static int ctNonPalin( int arr[], int N)
{
int Res = 0 ;
for ( int i = 0 ; i < N; i++)
{
int x = revNum(arr[i]);
if (x == arr[i])
{
continue ;
}
else
{
if (arr[i] % 10 == x % 10 )
Res += 1 ;
}
}
return Res;
}
public static void main (String[] args)
{
int arr[] = { 121 , 134 , 2342 , 4514 };
int N = arr.length;
System.out.println(ctNonPalin(arr, N));
}
}
|
Python3
def revNum(N):
x = 0
while (N):
x = x * 10 + N % 10
N = N / / 10
return x
def ctNonPalin(arr, N):
Res = 0
for i in range (N):
x = revNum(arr[i])
if (x = = arr[i]):
continue
else :
Res + = (arr[i] % 10 = = N % 10 )
return Res
if __name__ = = '__main__' :
arr = [ 121 , 134 , 2342 , 4514 ]
N = len (arr)
print (ctNonPalin(arr, N))
|
C#
using System;
class GFG{
static int revNum( int N)
{
int x = 0;
while (N != 0)
{
x = x * 10 + N % 10;
N = N / 10;
}
return x;
}
static int ctNonPalin( int [] arr, int N)
{
int Res = 0;
for ( int i = 0; i < N; i++)
{
int x = revNum(arr[i]);
if (x == arr[i])
{
continue ;
}
else
{
if (arr[i] % 10 == x % 10)
Res += 1;
}
}
return Res;
}
public static void Main ()
{
int [] arr = new int []{ 121, 134, 2342, 4514 };
int N = arr.Length;
Console.WriteLine(ctNonPalin(arr, N));
}
}
|
Javascript
<script>
function revNum(N) {
var x = 0;
while (N) {
x = x * 10 + (N % 10);
N = N / 10;
}
return x;
}
function ctNonPalin(arr, N) {
var Res = 0;
for ( var i = 0; i < N; i++) {
var x = revNum(arr[i]);
if (x == arr[i]) {
continue ;
}
else {
Res += arr[i] % 10 == N % 10;
}
}
return Res;
}
var arr = [121, 134, 2342, 4514];
var N = arr.length;
document.write(ctNonPalin(arr, N));
</script>
|
Time Complexity: O(N * D) where D is the length of the largest number in the array.
Auxiliary Space: O(D)
Share your thoughts in the comments
Please Login to comment...