Check if Array elements can be made equal by adding unit digit to the number
Last Updated :
12 Sep, 2022
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>
Time Complexity: O(N)
Auxiliary Space: O(1)
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 Array Elements 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 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