Count non-palindromic array elements having same first and last digit

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.



  1. Traverse the array arr[].
  2. For every array element arr[i], check if it is palindrome or not.
  3. 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.
  4. If found to be true, increase count.
  5. Finally, print the count of such numbers.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to reverse a number
int revNum(int N)
{
    // Store the reverse of N
    int x = 0;
    while (N) {
        x = x * 10 + N % 10;
        N = N / 10;
    }
  
    // Return reverse of N
    return x;
}
  
// Function to get the count of non-palindromic
// numbers having same first and last digit
int ctNonPalin(int arr[], int N)
{
  
    // Store the required count
    int Res = 0;
  
    // Traverse the array
    for (int i = 0; i < N; i++) {
  
        // Store reverse of arr[i]
        int x = revNum(arr[i]);
  
        // Check for palindrome
        if (x == arr[i]) {
            continue;
        }
  
        // IF non-palindromic
        else {
            // Check if first and last
            // digits are equal
            Res += (arr[i] % 10 == N % 10);
        }
    }
    return Res;
}
  
// Driver Code
int main()
{
    int arr[] = { 121, 134, 2342, 4514 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << ctNonPalin(arr, N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement 
// the above approach 
import java.io.*; 
  
class GFG{ 
  
// Function to reverse a number
static int revNum(int N)
{
      
    // Store the reverse of N
    int x = 0;
      
    while (N != 0)
    {
        x = x * 10 + N % 10;
        N = N / 10;
    }
  
    // Return reverse of N
    return x;
}
  
// Function to get the count of non-palindromic
// numbers having same first and last digit
static int ctNonPalin(int arr[], int N)
{
  
    // Store the required count
    int Res = 0;
  
    // Traverse the array
    for(int i = 0; i < N; i++) 
    {
          
        // Store reverse of arr[i]
        int x = revNum(arr[i]);
  
        // Check for palindrome
        if (x == arr[i])
        {
            continue;
        }
  
        // IF non-palindromic
        else
        {
              
            // Check if first and last
            // digits are equal
            if(arr[i] % 10 == x % 10)
                Res += 1;
        }
    }
    return Res;
}
  
// Driver code 
public static void main (String[] args) 
    int arr[] = { 121, 134, 2342, 4514 };
    int N = arr.length;
      
    System.out.println(ctNonPalin(arr, N));
  
// This code is contributed by jana_sayantan

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
  
# Function to reverse a number
def revNum(N):
  
    # Store the reverse of N
    x = 0
    while (N):
        x = x * 10 + N % 10
        N = N // 10
  
    # Return reverse of N
    return x
  
# Function to get the count of non-palindromic
# numbers having same first and last digit
def ctNonPalin(arr, N):
  
    # Store the required count
    Res = 0
  
    # Traverse the array
    for i in range(N):
  
        # Store reverse of arr[i]
        x = revNum(arr[i])
  
        # Check for palindrome
        if (x == arr[i]):
            continue
  
        # IF non-palindromic
        else:
              
            # Check if first and last
            # digits are equal
            Res += (arr[i] % 10 == N % 10)
  
    return Res
  
# Driver Code
if __name__ == '__main__':
  
    arr = [ 121, 134, 2342, 4514 ]
    N = len(arr)
      
    print(ctNonPalin(arr, N))
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement 
// the above approach 
using System;
  
class GFG{ 
  
// Function to reverse a number
static int revNum(int N)
{
      
    // Store the reverse of N
    int x = 0;
      
    while (N != 0)
    {
        x = x * 10 + N % 10;
        N = N / 10;
    }
  
    // Return reverse of N
    return x;
}
  
// Function to get the count of non-palindromic
// numbers having same first and last digit
static int ctNonPalin(int[] arr, int N)
{
  
    // Store the required count
    int Res = 0;
  
    // Traverse the array
    for(int i = 0; i < N; i++) 
    {
          
        // Store reverse of arr[i]
        int x = revNum(arr[i]);
  
        // Check for palindrome
        if (x == arr[i])
        {
            continue;
        }
  
        // IF non-palindromic
        else
        {
              
            // Check if first and last
            // digits are equal
            if(arr[i] % 10 == x % 10)
                Res += 1;
        }
    }
    return Res;
}
  
// Driver code 
public static void Main () 
    int[] arr = new int[]{ 121, 134, 2342, 4514 };
    int N = arr.Length;
      
    Console.WriteLine(ctNonPalin(arr, N));
}
  
// This code is contributed by sanjoy_62

chevron_right


Output: 

2

Time Complexity: O(N * D) where D is the length of the largest number in the array.
Auxiliary Space: O(D)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.