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++
#include <bits/stdc++.h>
using namespace std;
string solve( int * arr, int n)
{
bool flag = 1;
for ( int i = 0; i < n; i++) {
while (arr[i] % 10 != 0 && arr[i] % 10 != 2)
arr[i] += arr[i] % 10;
}
for ( int i = 0; i < n; i++) {
if ((arr[0] % 10 == 0) && (arr[i] != arr[0])) {
flag = 0;
break ;
}
if ((arr[i] - arr[0]) % 20) {
flag = 0;
break ;
}
}
if (flag)
return "Possible" ;
else
return "Impossible" ;
}
int main()
{
int arr[] = { 1, 2, 4, 8, 24 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << solve(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String solve( int [] arr, int n)
{
boolean flag = true ;
for ( int i = 0 ; i < n; i++) {
while (arr[i] % 10 != 0 && arr[i] % 10 != 2 )
arr[i] += arr[i] % 10 ;
}
for ( int i = 0 ; i < n; i++) {
if ((arr[ 0 ] % 10 == 0 ) && (arr[i] != arr[ 0 ])) {
flag = false ;
break ;
}
if (((arr[i] - arr[ 0 ]) % 20 ) != 0 ) {
flag = false ;
break ;
}
}
if (flag )
return "Possible" ;
else
return "Impossible" ;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 8 , 24 };
int N = arr.length;
System.out.println( solve(arr, N));
}
}
|
Python3
def solve(arr, n):
flag = True
for i in range ( 0 , n):
while ((arr[i] % 10 ! = 0 ) and (arr[i] % 10 ! = 2 )):
arr[i] + = arr[i] % 10
for i in range ( 0 , n):
if ((arr[ 0 ] % 10 ) = = 0 and (arr[i] ! = arr[ 0 ])):
flag = False
break
if ((arr[i] - arr[ 0 ]) % 20 ! = 0 ):
flag = False
break
if (flag):
return "Possible"
else :
return "Impossible"
arr = [ 1 , 2 , 4 , 8 , 24 ]
N = len (arr)
ans = solve(arr, N)
print (ans)
|
C#
using System;
public class GFG {
static string solve( int [] arr, int n)
{
bool flag = true ;
for ( int i = 0; i < n; i++) {
while (arr[i] % 10 != 0 && arr[i] % 10 != 2) {
arr[i] += arr[i] % 10;
}
}
for ( int i = 0; i < n; i++) {
if ((arr[0] % 10) == 0 && (arr[i] != arr[0])) {
flag = false ;
break ;
}
if ((arr[i] - arr[0]) % 20 != 0) {
flag = false ;
break ;
}
}
if (flag)
return "Possible" ;
else
return "Impossible" ;
}
static public void Main()
{
int [] arr = { 1, 2, 4, 8, 24 };
int N = arr.Length;
Console.Write(solve(arr, N));
}
}
|
Javascript
<script>
function solve(arr, n)
{
let flag = true ;
for (let i = 0; i < n; i++) {
while (arr[i] % 10 != 0 && arr[i] % 10 != 2)
arr[i] += arr[i] % 10;
}
for (let i = 0; i < n; i++) {
if ((arr[0] % 10 == 0) && (arr[i] != arr[0])) {
flag = false ;
break ;
}
if (((arr[i] - arr[0]) % 20) != 0) {
flag = false ;
break ;
}
}
if (flag )
return "Possible" ;
else
return "Impossible" ;
}
let arr = [ 1, 2, 4, 8, 24 ];
let N = arr.length;
document.write( solve(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...