

Find the Mobile Number formed using first digits of arrays of absolute differenc...
source link: https://www.geeksforgeeks.org/find-the-mobile-number-formed-using-first-digits-of-arrays-of-absolute-differences-of-consecutive-numbers/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find the Mobile Number formed using first digits of arrays of absolute differences of consecutive numbers
- Last Updated : 12 Jan, 2022
Given a String ph[], the task is to find a new number for the user, based on following conditions:
- The new number will also start from the same digit as of original number.
- The digits of the new number will be the first digits of a series of arrays of absolute differences of the consecutive elements.
Examples:
Input: ph = “9827218706”
Output: 9154301011
Explanation:
Input: ph =”9647253846″
Output: 9310100011
Approach: Consider the following steps to solve this problem:
- Convert every character from the string into integer and store it into the array ph1[] using list comprehension.
- Declare an empty string ph2.
- Convert first element of array ph1[ ] into a string and add it to ph2.
- Using List comprehension create an array by storing the absolute difference of consecutive elements.
- Assign this array to ph1.
- Repeat step 3-5, ten times as the phone number have ten digits.
Below is the implementation of the above approach.
- Python3
# Function to find lucky phone number
def
phone(ph, n):
# Converting char to int and storing into array.
ph1
=
[
int
(i)
for
i
in
ph]
# Empty string to store lucky number.
ph2
=
""
# Loop for performing action
# and adding digit to ph2.
for
_
in
range
(n):
# Convert first element into
# string and adding to ph2.
ph2
+
=
str
(ph1[
0
])
# Creating new ph1 by subtracting
# consecutive element.
ph1
=
[
abs
(ph1[j]
-
ph1[j
+
1
]) \
for
j
in
range
(
len
(ph1)
-
1
)]
# Return lucky number ph2
return
ph2
# Original number
ph
=
"9827218706"
# Calling phone function.
num
=
phone(ph,
len
(ph))
# Print the lucky number
print
(num)
9154301011
Time Complexity: O(N*N)
Auxiliary Space: O(N)
Efficient Approach: In this approach, no extra space is required for storing elements in the array. First, declare an empty string ph2 in which lucky number will be stored, now create a for loop in which the first character of the string will be added to ph2 and again another for loop to find the absolute difference of consecutive element. Now the string of absolute difference will be assigned to ph1 which is the original number and the same steps will be followed. Follow the steps below to solve the problem:
- Initialize a string variable ph2[] as an empty string.
- Iterate over the range [0, N) using the variable i and perform the following tasks:
- Add ph[0] to the variable ph2[].
- Initialize a string variable S[] as an empty string.
- Iterate over the range [0, N-1) using the variable j and perform the following tasks:
- Add the value of str(abs(int(ph[j])-int(ph[j+1]))) to the variable S[].
- Set the value of ph as S[].
- After performing the above steps, print the value of ph2[] as the answer.
Below is the implementation of the above approach.
- Python3
# Function to find lucky number.
def
phone(ph, n):
# ph2 is empty string to store lucky number.
ph2
=
""
# For loop for finding lucky number
for
i
in
range
(
len
(ph)):
# Add first element of ph to ph2
ph2
+
=
ph[
0
]
# S for storing the difference
S
=
""
# Loop to calculate the absolute difference
for
j
in
range
(
len
(ph)
-
1
):
x
=
abs
(
int
(ph[j])
-
int
(ph[j
+
1
]))
S
+
=
str
(x)
# Assigning S to ph.
ph
=
S
# Return the lucky number
return
ph2
# Original number
ph
=
"9827218706"
# Call phone function
num
=
phone(ph,
len
(ph))
# Printing lucky number
print
(num)
9154301011
Time Complexity: O(N*N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Recommend
-
33
circle visualization of a number's digits in Go Introduction In the image below, each dot represents a deci...
-
16
Yesterday, I was writing about collecting the mistakes that create interoperability issues in Web brow...
-
14
How to specify fractional digits for formatted number string in Swift Fractional digits are a number of digits after the decimal separator (.). When you want to present a floating...
-
8
Improve Article Reorder digits of a given number to make it a power of 2Difficulty Level : MediumLast Updated : 28 Jun, 2021...
-
9
Improve Article Rotate digits of a given number by KDifficulty Level : EasyLast Updated : 29 Apr, 2021Given two intege...
-
15
Validation of the telephone number not having all the digits repeated advertisements I want to validate a telephone number of foramt (xxx) xxx...
-
11
Problem Code:- FLOW006 Problem Statement:- You're given an integer N. Write a program to calculate the sum of all the digits of N. Input The first line contains an integer T, the tot...
-
6
How To Generate A Valid Credit Card Number For A Bin (First 6 Digits) March 25, 2014 There is plenty of generators that can p...
-
9
Largest Number formed from an ArrayLargest Number formed from an Array | SDE Sheet | ArraysVideo Player is loading.Loaded: 0%Remaining Time -8:28
-
8
Count the number of arrays formed by rearranging elements such that one adjacent element is multiple of other...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK