Create new Mobile Number by selecting maximum from ends after inserting pairwise absolute difference in middle
Last Updated :
01 Jul, 2022
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++
#include <bits/stdc++.h>
using namespace std;
string phNumber(string ph, int n)
{
string S = "" ;
for ( int i = 0; i < n - 1; i++) {
S += ph[i];
int x = abs (ph[i] - ph[i + 1]);
S += to_string(x);
}
S += ph[9];
int s = 0;
int e = S.length() - 1;
string ph2 = "" ;
while (s != e) {
if (S[s] > S[e]) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
s += 1;
e -= 1;
}
ph2 += S[e];
return ph2;
}
int main()
{
string ph = "9647253846" ;
string num = phNumber(ph, ph.length());
cout << num;
}
|
Java
class GFG {
static String phNumber(String ph, int n) {
String S = "" ;
for ( int i = 0 ; i < n - 1 ; i++) {
S += ph.charAt(i);
int x = Math.abs(ph.charAt(i) - ph.charAt(i + 1 ));
S += Integer.toString(x);
}
S += ph.charAt( 9 );
int s = 0 ;
int e = S.length() - 1 ;
String ph2 = "" ;
while (s != e) {
if (S.charAt(s) > S.charAt(e)) {
ph2 += S.charAt(s);
} else {
ph2 += S.charAt(e);
}
s += 1 ;
e -= 1 ;
}
ph2 += S.charAt(e);
return ph2;
}
public static void main(String args[]) {
String ph = "9647253846" ;
String num = phNumber(ph, ph.length());
System.out.println(num);
}
}
|
Python3
def phNumber(ph, n):
S = ""
for i in range (n - 1 ):
S + = ph[i]
s = int (ph[i]) - int (ph[i + 1 ])
x = abs (s)
S + = str (x)
S + = ph[ 9 ]
s = 0
e = len (S) - 1
ph2 = ""
while s ! = e:
ph2 + = max (S[s], S[e])
s + = 1
e - = 1
ph2 + = S[e]
return ph2
ph = "9647253846"
num = phNumber(ph, len (ph))
print (num)
|
C#
using System;
class GFG {
static string phNumber( string ph, int n)
{
string S = "" ;
for ( int i = 0; i < n - 1; i++) {
S += ph[i];
int x = Math.Abs(ph[i] - ph[i + 1]);
S += x.ToString();
}
S += ph[9];
int s = 0;
int e = S.Length - 1;
string ph2 = "" ;
while (s != e) {
if (S[s] > S[e]) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
s += 1;
e -= 1;
}
ph2 += S[e];
return ph2;
}
public static void Main()
{
string ph = "9647253846" ;
string num = phNumber(ph, ph.Length);
Console.Write(num);
}
}
|
Javascript
<script>
function phNumber(ph, n) {
let S = ""
for (let i = 0; i < n - 1; i++) {
S += ph[i]
let s = ph[i].charCodeAt(0) - ph[i + 1].charCodeAt(0)
let x = Math.abs(s)
S += x.toString();
}
S += ph[9]
s = 0
e = S.length - 1
ph2 = ""
while (s != e) {
if (S[s].charCodeAt(0) > S[e].charCodeAt(0)) {
ph2 += S[s];
}
else {
ph2 += S[e];
}
s += 1
e -= 1
}
ph2 += S[e]
return ph2
}
let ph = "9647253846"
let num = phNumber(ph, ph.length)
document.write(num)
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...