Given a String ph[], the task is to find the absolute difference of consecutive elements and insert the result in between the consecutive elements. By doing this size of phone numbers will increase from 10 to 19. Now we have to compare digits from first and last and select maximum in this way we will get a new phone number.
Examples:
Input: ph = “9647253846”
Output: 9364857553
Explanation:
Input: ph = “7635892563”
Output: 7363535791
Approach: The idea is to use two pointers approach to solve this problem along with using a string variable to add the phone numbers with a difference. Follow the steps below to solve the problem:
- Initialize a string variable S[] as an empty string.
-
Iterate over the range [0, N-1) using the variable i and perform the following tasks:
- Add ph[i] to the variable S.
- Initialize the variables s as int(ph[i]) – int(ph[i+1]) and x as abs(s).
- Add str(x) to the variable S.
- Add ph[9] to the variable S.
- Initialize the variables s as 0 and e as len(S)-1.
- Initialize a string variable ph2[] as an empty string.
-
Traverse in a while loop till s is not equals to e and perform the following tasks:
- Add the max of S[s] or S[e] to the variable ph2[].
- Increase the value of s by 1 and decrease the value of e by 1.
- Add S[e] to the variable ph2[].
- After performing the above steps, print the value of ph2[] as the answer.
Below is the implementation of the above approach.
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to create the phone number string phNumber(string ph, int n)
{ // Empty string to store extended number
string S = "" ;
// Loop for extend phone number
for ( int i = 0; i < n - 1; i++) {
// Add character form string
S += ph[i];
// Finding absolute difference
int x = abs (ph[i] - ph[i + 1]);
// Adding the difference to S
S += to_string(x);
}
// Adding last digit of ph
S += ph[9];
// Using 2 pointer algorithm to form comparison
// s is start index
int s = 0;
// e is end index
int e = S.length() - 1;
// ph2 an empty string for
// storing the phone number
string ph2 = "" ;
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S[s] > S[e]) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
// Increment start index
s += 1;
// Decrement end index
e -= 1;
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S[e];
// Return phone number
return ph2;
} // Driver Code int main()
{ // Original phone number
string ph = "9647253846" ;
// Calling function to create new number
string num = phNumber(ph, ph.length());
// Print the new number
cout << num;
} // This code is contributed by Samim Hossain Mondal. |
// Java program for the above approach class GFG {
// Function to create the phone number
static String phNumber(String ph, int n) {
// Empty string to store extended number
String S = "" ;
// Loop for extend phone number
for ( int i = 0 ; i < n - 1 ; i++) {
// Add character form string
S += ph.charAt(i);
// Finding absolute difference
int x = Math.abs(ph.charAt(i) - ph.charAt(i + 1 ));
// Adding the difference to S
S += Integer.toString(x);
}
// Adding last digit of ph
S += ph.charAt( 9 );
// Using 2 pointer algorithm to form comparison
// s is start index
int s = 0 ;
// e is end index
int e = S.length() - 1 ;
// ph2 an empty string for
// storing the phone number
String ph2 = "" ;
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S.charAt(s) > S.charAt(e)) {
ph2 += S.charAt(s);
} else {
ph2 += S.charAt(e);
}
// Increment start index
s += 1 ;
// Decrement end index
e -= 1 ;
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S.charAt(e);
// Return phone number
return ph2;
}
// Driver Code
public static void main(String args[]) {
// Original phone number
String ph = "9647253846" ;
// Calling function to create new number
String num = phNumber(ph, ph.length());
// Print the new number
System.out.println(num);
}
} // This code is contributed by gfgking |
# Function to create the phone number def phNumber(ph, n):
# Empty string to store extended number
S = ""
# Loop for extend phone number
for i in range (n - 1 ):
# Add character form string
S + = ph[i]
# Finding absolute difference
s = int (ph[i]) - int (ph[i + 1 ])
x = abs (s)
# Adding the difference to S
S + = str (x)
# Adding last digit of ph
S + = ph[ 9 ]
# Using 2 pointer algorithm to form comparison
# s is start index
s = 0
# e is end index
e = len (S) - 1
# ph2 an empty string for
# storing the phone number
ph2 = ""
# Loop till e become equal to s
while s ! = e:
# Comparing element at s and e
# index and adding maximum
ph2 + = max (S[s], S[e])
# Increment start index
s + = 1
# Decrement end index
e - = 1
# When s = e loop will terminate
# so adding element at index s = e
ph2 + = S[e]
# Return phone number
return ph2
# Original phone number ph = "9647253846"
# Calling function to create new number num = phNumber(ph, len (ph))
# Print the new number print (num)
|
// C# program for the above approach using System;
class GFG {
// Function to create the phone number
static string phNumber( string ph, int n)
{
// Empty string to store extended number
string S = "" ;
// Loop for extend phone number
for ( int i = 0; i < n - 1; i++) {
// Add character form string
S += ph[i];
// Finding absolute difference
int x = Math.Abs(ph[i] - ph[i + 1]);
// Adding the difference to S
S += x.ToString();
}
// Adding last digit of ph
S += ph[9];
// Using 2 pointer algorithm to form comparison
// s is start index
int s = 0;
// e is end index
int e = S.Length - 1;
// ph2 an empty string for
// storing the phone number
string ph2 = "" ;
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S[s] > S[e]) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
// Increment start index
s += 1;
// Decrement end index
e -= 1;
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S[e];
// Return phone number
return ph2;
}
// Driver Code
public static void Main()
{
// Original phone number
string ph = "9647253846" ;
// Calling function to create new number
string num = phNumber(ph, ph.Length);
// Print the new number
Console.Write(num);
}
} // This code is contributed by ukasp. |
<script> // JavaScript code for the above approach
// Function to create the phone number
function phNumber(ph, n) {
// Empty string to store extended number
let S = ""
// Loop for extend phone number
for (let i = 0; i < n - 1; i++) {
// Add character form string
S += ph[i]
// Finding absolute difference
let s = ph[i].charCodeAt(0) - ph[i + 1].charCodeAt(0)
let x = Math.abs(s)
// Adding the difference to S
S += x.toString();
}
// Adding last digit of ph
S += ph[9]
// Using 2 pointer algorithm to form comparison
// s is start index
s = 0
// e is end index
e = S.length - 1
// ph2 an empty string for
// storing the phone number
ph2 = ""
// Loop till e become equal to s
while (s != e) {
// Comparing element at s and e
// index and adding maximum
if (S[s].charCodeAt(0) > S[e].charCodeAt(0)) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
// Increment start index
s += 1
// Decrement end index
e -= 1
}
// When s = e loop will terminate
// so adding element at index s = e
ph2 += S[e]
// Return phone number
return ph2
}
// Original phone number
let ph = "9647253846"
// Calling function to create new number
let num = phNumber(ph, ph.length)
// Print the new number
document.write(num)
// This code is contributed by Potta Lokesh
</script>
|
9364857553
Time Complexity: O(N)
Auxiliary Space: O(N)