Find the Mobile Number formed using first digits of arrays of absolute differences of consecutive numbers
Last Updated :
08 May, 2023
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.
Java
public class Mobile
{
static String phone(String ph, int n)
{
int [] ph1 = new int [n];
for ( int i = 0 ; i < n; i++)
ph1[i] = ph.charAt(i) - '0' ;
String ph2 = "" ;
for ( int i = 0 ; i < n; i++) {
ph2 += ph1[ 0 ];
int ph3[] = new int [ph1.length - 1 ];
for ( int j = 0 ; j < ph1.length - 1 ; j++)
ph3[j] = Math.abs(ph1[j] - ph1[j + 1 ]);
ph1 = ph3;
}
return ph2;
}
public static void main(String[] args)
{
String ph = "9827218706" ;
String num = phone(ph, ph.length());
System.out.println(num);
}
}
|
Python3
def phone(ph, n):
ph1 = [ int (i) for i in ph]
ph2 = ""
for _ in range (n):
ph2 + = str (ph1[ 0 ])
ph1 = [ abs (ph1[j] - ph1[j + 1 ]) \
for j in range ( len (ph1) - 1 )]
return ph2
ph = "9827218706"
num = phone(ph, len (ph))
print (num)
|
C#
using System;
public class Mobile
{
static String phone(String ph, int n)
{
int [] ph1 = new int [n];
for ( int i = 0; i < n; i++)
ph1[i] = ph[i] - '0' ;
String ph2 = "" ;
for ( int i = 0; i < n; i++) {
ph2 += ph1[0];
int [] ph3 = new int [ph1.Length - 1];
for ( int j = 0; j < ph1.Length - 1; j++)
ph3[j] = Math.Abs(ph1[j] - ph1[j + 1]);
ph1 = ph3;
}
return ph2;
}
public static void Main()
{
String ph = "9827218706" ;
String num = phone(ph, ph.Length);
Console.Write(num);
}
}
|
Javascript
<script>
function phone(ph, n) {
let ph1 = [];
for (i of ph)
ph1.push(i)
let ph2 = ""
for (let _ = 0; _ < n; _++) {
ph2 += new String(ph1[0])
let temp = []
for (let j = 0; j < ph1.length - 1; j++) {
temp.push(Math.abs(ph1[j] - ph1[j + 1]))
}
ph1 = temp
}
return ph2
}
let ph = "9827218706"
let num = phone(ph, ph.length)
document.write(num)
</script>
|
C++
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
string phone(string ph, int n) {
vector< int > ph1;
for ( int i = 0; i < n; i++) {
ph1.push_back(ph[i] - '0' );
}
string ph2 = "" ;
for ( int i = 0; i < n; i++) {
ph2 += to_string(ph1[0]);
vector< int > ph3;
for ( int j = 0; j < ph1.size() - 1; j++) {
ph3.push_back( abs (ph1[j] - ph1[j + 1]));
}
ph1 = ph3;
}
return ph2;
}
int main() {
string ph = "9827218706" ;
string num = phone(ph, ph.length());
cout << num << endl;
return 0;
}
#This code is contributed by Edula Vinay Kumar Reddy
|
Time Complexity: O(N*N), The time complexity is O(N*N) because there are two nested loops in the “phone()” function. The first loop iterates over each digit of the input phone number, which takes O(N) time. The second loop also iterates over each digit of the phone number, but it creates a new vector “ph3” by subtracting consecutive elements. This operation takes O(N) time because it performs N-1 subtractions. Therefore, the overall time complexity of the algorithm is O(N*N).
Auxiliary Space: O(N), The space complexity is O(N) because the algorithm uses two vectors to store the digits of the input phone number and the difference between consecutive digits. Both vectors have a maximum length of N, which is the length of the input phone number. Therefore, the space complexity is 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:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string phone(string ph, int n)
{
string ph2 = "" ;
for ( int i = 0; i < ph.length(); i++) {
ph2 += ph[0];
string S = "" ;
for ( int j = 0; j < ph.length(); j++) {
int x = abs ( int (ph[j]) - int (ph[j + 1]));
S += x + '0' ;
}
ph = S;
}
return ph2;
}
int main()
{
string ph = "9827218706" ;
string num = phone(ph, ph.length());
cout << (num);
}
|
Java
import java.util.*;
class GFG {
static String phone(String ph, int n)
{
String ph2 = "" ;
for ( int i = 0 ; i < ph.length(); i++)
{
ph2 += ph.charAt( 0 );
String S = "" ;
for ( int j = 0 ; j < ph.length()- 1 ; j++)
{
int x = Math.abs(ph.charAt(j) - ph.charAt(j+ 1 ));
S += ( char )(x + '0' );
}
ph = S;
}
return ph2;
}
public static void main(String args[])
{
String ph = "9827218706" ;
String num = phone(ph, ph.length());
System.out.println(num);
}
}
|
Python3
def phone(ph, n):
ph2 = ""
for i in range ( len (ph)):
ph2 + = ph[ 0 ]
S = ""
for j in range ( len (ph) - 1 ):
x = abs ( int (ph[j]) - int (ph[j + 1 ]))
S + = str (x)
ph = S
return ph2
ph = "9827218706"
num = phone(ph, len (ph))
print (num)
|
C#
using System;
class GFG
{
static string phone( string ph, int n)
{
string ph2 = "" ;
for ( int i = 0; i < ph.Length; i++)
{
ph2 += ph[0];
string S = "" ;
for ( int j = 0; j < ph.Length; j++)
{
int x = Math.Abs(ph[j] - ph[j + 1]);
S += x + '0' ;
}
ph = S;
}
return ph2;
}
public static void Main()
{
string ph = "9827218706" ;
string num = phone(ph, ph.Length);
Console.WriteLine (num);
}
}
|
Javascript
<script>
function phone( ph , n) {
let ph1 = [];
for (i of ph)
ph1.push(i)
let ph2 = ""
for (let _ = 0; _ < n; _++) {
ph2 += new String(ph1[0])
let S = []
for (let j = 0; j < ph1.length - 1; j++) {
S.push(Math.abs(ph1[j] - ph1[j + 1]))
}
ph1 = S
}
return ph2;
}
var ph = "9827218706" ;
var num = phone(ph, ph.length);
document.write(num);
</script>
|
Time Complexity: O(N*N), The code has a nested for loop. The outer loop runs N times, where N is the length of the input string. The inner loop also runs N times, where for each iteration, the difference between two adjacent characters of the input string is calculated. Therefore, the total time complexity of the code is O(N*N).
Auxiliary Space: O(N),The code uses a few variables to store the input and output strings. The input string and the lucky number string are stored as strings, and therefore, they use O(N) space, where N is the length of the input string. The difference string S also uses O(N) space. Therefore, the total space complexity of the code is O(N).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...