Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Check if elements of array can be made equal by multiplying given prime numbers
Next article icon

Check if Array elements can be made equal by adding unit digit to the number

Last Updated : 12 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of N integers, the task is to check whether it is possible to make all the array elements identical by applying the following option any number of times:

  • Choose any number and add its unit digit to it.

Examples:

Input: arr[] = {1, 2, 4, 8, 24}
Output: Possible
Explanation: 
For 1: 1 -> 2 -> 4 -> 8 -> 16- > 22 -> 24
For 2: 2 -> 4 -> 8 -> 16 -> 22 -> 24
For 4: 4 -> 8 -> 16 -> 22 -> 24
For 8: 16- > 22 -> 24
For 24: 24 (it's already 24, so, no need to change)

Input: arr[] = {5, 10}
Output: Possible

Approach: The problem can be solved based on the following observation.

Any number can be converted to have either 0 or 2 as its unit digit by repetitively adding its unit digit to itself at most 10 times. 

The idea is to 

  • Initially make the unit digit of every number either 2 or 0.
  • Then for the numbers that have 0 as their unit digit, all of those numbers must be same(because x+0 = 0), and 
  • For every number with 2 as the unit digit, the difference between them must be multiple of 20 so that they can be made equal.

Follow the steps to solve this problem:

  • Add the unit digit of each number to itself until its unit digit becomes 2 or 0.
  • For every number that has 0 as a unit digit, check whether all the numbers are equal.
  • For numbers with 2 as the unit digit, the difference between them must be a multiple of 20.

Below is the implementation of the above approach:

C++
// C++ code to implement the approach  #include <bits/stdc++.h> using namespace std;  // Function to check whether the elements // of array can be equal or not string solve(int* arr, int n) {     // Initialize flag as 1     bool flag = 1;      // Make unit digit of every element of array     // with either 0 or 2     for (int i = 0; i < n; i++) {         while (arr[i] % 10 != 0 && arr[i] % 10 != 2)             arr[i] += arr[i] % 10;     }      // Check if it is possible to make     // the array elements identical or not     for (int i = 0; i < n; i++) {          // Elements for 0 as unit digit         if ((arr[0] % 10 == 0) && (arr[i] != arr[0])) {             flag = 0;             break;         }          // Elements for 2 as unit digit         if ((arr[i] - arr[0]) % 20) {             flag = 0;             break;         }     }      if (flag)         return "Possible";     else         return "Impossible"; }  // Driver Code int main() {     int arr[] = { 1, 2, 4, 8, 24 };     int N = sizeof(arr) / sizeof(arr[0]);      // Function call     cout << solve(arr, N);     return 0; } 
Java
// Java code for the above approach import java.io.*;  class GFG {  // Function to check whether the elements // of array can be equal or not static String solve(int[] arr, int n) {     // Initialize flag as 1     boolean flag = true;      // Make unit digit of every element of array     // with either 0 or 2     for (int i = 0; i < n; i++) {         while (arr[i] % 10 != 0 && arr[i] % 10 != 2)             arr[i] += arr[i] % 10;     }      // Check if it is possible to make     // the array elements identical or not     for (int i = 0; i < n; i++) {          // Elements for 0 as unit digit         if ((arr[0] % 10 == 0) && (arr[i] != arr[0])) {             flag = false;             break;         }          // Elements for 2 as unit digit         if (((arr[i] - arr[0]) % 20) != 0) {             flag = false;             break;         }     }      if (flag )         return "Possible";     else         return "Impossible"; }      // Driver code     public static void main(String[] args)     {         int arr[] = { 1, 2, 4, 8, 24 };     int N = arr.length;      // Function call     System.out.println( solve(arr, N));     } }   // This code is contributed by sanjoy_62. 
Python3
# Python3 code for the above approach  # Function to check whether the elements of array can # be equal or not def solve(arr, n):          # Initialize flag as true     flag = True      # Make unit digit of every element of array with     # either 0 or 2     # for (int i = 0; i < n; i++) {     for i in range(0, n):         while ((arr[i] % 10 != 0) and (arr[i] % 10 != 2)):             arr[i] += arr[i] % 10              # Check if it is possible to make the array             # elements identical or not             # for (int i = 0; i < n; i++) {     for i in range(0, n):               # Elements for 0 as unit digit         if ((arr[0] % 10) == 0 and (arr[i] != arr[0])):             flag = False             break              # Elements for 2 as unit digit         if ((arr[i] - arr[0]) % 20 != 0):              flag = False             break      if (flag):         return "Possible"     else:         return "Impossible"  # Driver Code arr = [1, 2, 4, 8, 24] N = len(arr)  # Function call ans = solve(arr, N) print(ans)  # This code is contributed by akashish. 
C#
// C# code for the above approach  using System;  public class GFG {      // Function to check whether the elements of array can     // be equal or not     static string solve(int[] arr, int n)     {         // Initialize flag as true         bool flag = true;          // Make unit digit of every element of array with         // either 0 or 2         for (int i = 0; i < n; i++) {             while (arr[i] % 10 != 0 && arr[i] % 10 != 2) {                 arr[i] += arr[i] % 10;             }         }          // Check if it is possible to make the array         // elements identical or not         for (int i = 0; i < n; i++) {             // Elements for 0 as unit digit             if ((arr[0] % 10) == 0 && (arr[i] != arr[0])) {                 flag = false;                 break;             }              // Elements for 2 as unit digit             if ((arr[i] - arr[0]) % 20 != 0) {                 flag = false;                 break;             }         }          if (flag)             return "Possible";         else             return "Impossible";     }      static public void Main()     {          // Code         int[] arr = { 1, 2, 4, 8, 24 };         int N = arr.Length;          // Function call         Console.Write(solve(arr, N));     } }  // This code is contributed by lokeshmvs21. 
JavaScript
<script>     // JavaScript code for the approach  // Function to check whether the elements // of array can be equal or not function solve(arr, n) {     // Initialize flag as 1     let flag = true;       // Make unit digit of every element of array     // with either 0 or 2     for (let i = 0; i < n; i++) {         while (arr[i] % 10 != 0 && arr[i] % 10 != 2)             arr[i] += arr[i] % 10;     }       // Check if it is possible to make     // the array elements identical or not     for (let i = 0; i < n; i++) {           // Elements for 0 as unit digit         if ((arr[0] % 10 == 0) && (arr[i] != arr[0])) {             flag = false;             break;         }           // Elements for 2 as unit digit         if (((arr[i] - arr[0]) % 20) != 0) {             flag = false;             break;         }     }       if (flag )         return "Possible";     else         return "Impossible"; }      // Driver code      // Given input     let arr = [ 1, 2, 4, 8, 24 ];     let N = arr.length;       // Function call     document.write( solve(arr, N));  // This code is contributed by code_hunt. </script> 

Output
Possible

Time Complexity: O(N)
Auxiliary Space: O(1)


Next Article
Check if elements of array can be made equal by multiplying given prime numbers
author
akashjha2671
Improve
Article Tags :
  • Mathematical
  • DSA
  • Arrays
Practice Tags :
  • Arrays
  • Mathematical

Similar Reads

  • Check if Arrays can be made equal by Replacing elements with their number of Digits
    Given two arrays A[] and B[] of length N, the task is to check if both arrays can be made equal by performing the following operation at most K times: Choose any index i and either change Ai to the number of digits Ai have or change Bi to the number of digits Bi have. Examples: Input: N = 4, K = 1,
    10 min read
  • Check if all the elements can be made equal on dividing with X and Y
    Given an array arr[] and two integers X and Y. The task is to check whether it is possible to make all the elements equal by dividing them with X and Y any number of times including 0. Examples: Input: arr[] = {2, 4, 6, 8}, X = 2, Y = 3 Output: Yes 2 -> 2 4 -> (4 / X) = (4 / 2) = 2 6 -> (6
    8 min read
  • Check if elements of array can be made equal by multiplying given prime numbers
    Given an array of integers and an array of prime numbers. The task is to find if it is possible to make all the elements of integer array equal by multiplying one or more elements from prime given array of prime numbers.Examples: Input : arr[] = {50, 200} prime[] = {2, 3} Output : Yes We can multipl
    10 min read
  • Check if Array Elemnts can be Made Equal with Given Operations
    Given an array arr[] consisting of N integers and following are the three operations that can be performed using any external number X. Add X to an array element once.Subtract X from an array element once.Perform no operation on the array element.Note : Only one operation can be performed on a numbe
    6 min read
  • Check if even and odd count of elements can be made equal in Array
    Given an array Arr[] of N integers and an integer K, the task is to find if it is possible to make the count of even and odd elements equal by performing the following operations at most K times: Choose any index i such that Arr[i] is even and divide it by 2.Choose any index i such that Arr[i] is od
    9 min read
  • Check if all array elements can be converted to pronic numbers by rotating digits
    Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times. Examples: Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to
    7 min read
  • Check if given Array can be made a permutation of 1 to N by reducing elements by half
    Given an array nums[] of size N, the task is to check whether the given array can be converted into a permutation of 1 to N after performing given operations any number of times (may be 0). An operation is defined as: Pick any element of the array say 'x', and replace it with 'x/2'. Note: In a permu
    6 min read
  • Check if array elements can become same using one external number
    Given an array arr[] consisting of N integers and following are the three operations that can be performed using any external number X: Add X to an array element once.Subtract X from an array element once.Perform no operation on the array element.The task is to check whether there exists a number X,
    11 min read
  • Check if sum of array can be made equal to X by removing either the first or last digits of every array element
    Given an array arr[] consisting of N positive integers and a positive integer X, the task is to check if the sum of the given array can be made equal to X by removing either the first or last digit of every array element. If it is possible to make the sum of array elements equal to X, then print "Ye
    7 min read
  • Check if all elements of a Circular Array can be made equal by increments of adjacent pairs
    Given a circular array arr[] of size N, the task is to check if it is possible to make all array elements of the circular array equal by increasing pairs of adjacent elements by 1. Examples: Input: N = 4, arr[] = {2, 1, 3, 4} Output:YesExplanation:Step 1: {2, 1, 3, 4} -> {3, 2, 3, 4}Step 2: {3, 2
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences