Find the Mobile Number formed using first digits of arrays of absolute differences of consecutive numbers

21 Jun, 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.
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: 9154301011Explanation:
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.
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
Python3
Javascript
// Java program for above approachpublic class Mobile{   // Function to find lucky phone number  static String phone(String ph, int n)  {     // Converting char to int and storing into array.    int[] ph1 = new int[n];    for (int i = 0; i < n; i++)      ph1[i] = ph.charAt(i) - '0';     // Empty string to store lucky number.    String ph2 = "";     // Loop for performing action    // and adding digit to ph2.    for (int i = 0; i < n; i++) {      // Convert first element into      // string and adding to ph2.      ph2 += ph1[0];       // Creating new ph1 by subtracting      // consecutive element.      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 lucky number ph2    return ph2;  }  // Driver code  public static void main(String[] args)  {     // Original number    String ph = "9827218706";     // Calling phone function.    String num = phone(ph, ph.length());     // Print the lucky number    System.out.println(num);  }} // This code is contributed by Lovely Jain
# Function to find lucky phone numberdef 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 numberph = "9827218706" # Calling phone function.num = phone(ph, len(ph)) # Print the lucky numberprint(num)
<script>// Function to find lucky phone numberfunction phone(ph, n) {     // Converting char to int and storing into array.    let ph1 = [];    for (i of ph)        ph1.push(i)     // Empty string to store lucky number.    let ph2 = ""     // Loop for performing action    // and adding digit to ph2.    for (let _ = 0; _ < n; _++) {         // Convert first element into        // string and adding to ph2.        ph2 += new String(ph1[0])         // Creating new ph1 by subtracting        // consecutive element.        let temp = []        for (let j = 0; j < ph1.length - 1; j++) {            temp.push(Math.abs(ph1[j] - ph1[j + 1]))        }        ph1 = temp    }     // Return lucky number ph2    return ph2} // Original numberlet ph = "9827218706" // Calling phone function.let num = phone(ph, ph.length) // Print the lucky numberdocument.write(num) // This code is contributed by gfgking.</script>
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[].
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[].
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.
C++
Java
Python3
C#
Javascript
// C++ code for the above approach#include <bits/stdc++.h>using namespace std; // Function to find lucky number.string phone(string ph, int n){     // ph2 is empty string to store lucky number.    string ph2 = "";     // For loop for finding lucky number    for (int i = 0; i < ph.length(); i++) {         // Add first element of ph to ph2        ph2 += ph[0];         // S for storing the difference        string S = "";         // Loop to calculate the absolute difference        for (int j = 0; j < ph.length(); j++) {            int x = abs(int(ph[j]) - int(ph[j + 1]));            S += x + '0';        }         // Assigning S to ph.        ph = S;    }     // Return the lucky number    return ph2;} // Driver Codeint main(){     // Original number    string ph = "9827218706";     // Call phone function    string num = phone(ph, ph.length());     // Printing lucky number    cout << (num);} // This code is contributed by Potta Lokesh
// Java program for the above approachimport java.util.*; class GFG {   // Function to find lucky number.  static String phone(String ph, int n)  {     // ph2 is empty string to store lucky number.    String ph2 = "";     // For loop for finding lucky number    for (int i = 0; i < ph.length(); i++)    {       // Add first element of ph to ph2      ph2 += ph.charAt(0);       // S for storing the difference      String S = "";       // Loop to calculate the absolute difference      for (int j = 0; j < ph.length()-1; j++)      {        int x = Math.abs(ph.charAt(j) - ph.charAt(j+1));        S += (char)(x + '0');      }       // Assigning S to ph.      ph = S;    }     // Return the lucky number    return ph2;       }   // Driver Code  public static void main(String args[])  {    // Original number    String ph = "9827218706";     // Call phone function    String num = phone(ph, ph.length());     // Printing lucky number    System.out.println(num);  }} // This code is contributed by avijitmondal1998
# 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 numberph = "9827218706" # Call phone functionnum = phone(ph, len(ph)) # Printing lucky numberprint(num)
// C# code for the above approachusing System;class GFG{   // Function to find lucky number.  static string phone(string ph, int n)  {     // ph2 is empty string to store lucky number.    string ph2 = "";     // For loop for finding lucky number    for (int i = 0; i < ph.Length; i++)    {       // Add first element of ph to ph2      ph2 += ph[0];       // S for storing the difference      string S = "";       // Loop to calculate the absolute difference      for (int j = 0; j < ph.Length; j++)      {        int x = Math.Abs(ph[j] - ph[j + 1]);        S += x + '0';      }       // Assigning S to ph.      ph = S;    }     // Return the lucky number    return ph2;  }   // Driver Code  public static void Main()  {     // Original number    string ph = "9827218706";     // Call phone function    string num = phone(ph, ph.Length);     // Printing lucky number    Console.WriteLine (num);  }} // This code is contributed by ukasp.
<script>// javascript program for the above approach     // Function to find lucky number.     function phone( ph , n) {         // ph2 is empty string to store lucky number.         // Converting char to int and storing into array.    let ph1 = [];    for (i of ph)        ph1.push(i)     // Empty string to store lucky number.    let ph2 = ""     // Loop for performing action    // and adding digit to ph2.    for (let _ = 0; _ < n; _++) {         // Convert first element into        // string and adding to ph2.        ph2 += new String(ph1[0])         // Creating new ph1 by subtracting        // consecutive element.        let S = []        for (let j = 0; j < ph1.length - 1; j++) {            S.push(Math.abs(ph1[j] - ph1[j + 1]))        }        ph1 = S    }         // Return the lucky number        return ph2;     }     // Driver Code             // Original number        var ph = "9827218706";         // Call phone function        var num = phone(ph, ph.length);         // Printing lucky number        document.write(num);         // This code is contributed by umadevi9616</script>
9154301011
Time Complexity: O(N*N)Auxiliary Space: O(N)
lokeshpotta20
ukasp
gfgking
avijitmondal1998
umadevi9616
jainlovely450
Algo-Geek 2021
Algo Geek
Mathematical
Pattern Searching
Mathematical
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.