Find two non-intersecting String with maximum sum of difference of digits
Last Updated :
02 Jan, 2023
Given a string S of length N, the task is to find two non-intersecting strings of equal length such that the sum of absolute differences of their digits is maximum and one string should start from starting index of the given string.
Note: Both strings will be of the same length.
Examples:
Input: N = 5, S = “87605”
Output: String 1 = “87”, String 2 =”05″, Difference = 10
Explanation: At index 0 of both strings: |8 – 0| = 8
At index 1 of both strings: |7 – 5| = 2
Total maximum possible absolute difference = 8 + 2 = 10, also note that string “87” is starting from starting index of given string in input and both output strings are equal in size.
Input: N = 7, S = “987342”
Output: String 1 = “987”, String 2 = “342”, Difference = 15
Approach: Implement the idea below to solve the problem.
The problem can be solved via traversing on left half part of the given string and applying sliding window at right side of the first string.
Follow the below illustration for a better understanding.
Illustration:
Considered N = 6 and S = 111000
Then all the possible cases for two strings will be such that they both are of equal length:
It should be noted that first string will always start from 0 index of S and maximum end index can be N/2 for even N and (N / 2) – 1 for odd value of N, Only at this condition second string can have equal length of first string and second string will be at right side of first string. So we can obtain second string by applying sliding window technique.
If we get both strings then it is not hard work to get maximum absolute difference between their integer values at each index.
Follow the steps to solve the problem:
- Create variables max_diff, str_1, and str_2 for storing max difference, first string and second string respectively.
- Run a loop from 0 to N/2 and follow the below-mentioned steps inside the scope of the loop:
- Create a temp_1 variable to initialize the first string temporarily.
- Create a temp_2 variable to initialize the second string, Which will be obtained by traversing the input string part at the right side of temp_1.
- Calculate the absolute integer difference of each digit of both the strings(temp_1, temp_2), If the difference is maximized till now, update max_diff with the difference obtained and update str_1 and str_2 with temp_1 and temp_2 respectively.
- Print the value of max_diff, str_1, and str_2 in output representing max possible difference, String 1 and String 2 respectively.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
long Difference(string X, string Y)
{
long counter = 0;
for ( int i = X.size() - 1; i >= 0; i--) {
counter += abs (X[i] - Y[i]);
}
return counter;
}
void findValues(string str, int n)
{
long max_diff = INT_MIN;
string ans_1 = "" , ans_2 = "" ;
for ( int i = 0; i < (n / 2); i++) {
string str1 = "" ;
for ( int j = 0; j <= i; j++) {
str1 += str[j];
}
for ( int k = (i + 1); k < n; k++) {
string str2 = "" ;
if ((k + i) < n) {
for ( int l = k; l <= k + i; l++) {
str2 += str[l];
}
long difference = Difference(str1, str2);
if (difference > max_diff) {
max_diff = difference;
ans_1 = str1;
ans_2 = str2;
}
}
}
}
cout << "String 1: " << ans_1 << endl;
cout << "String 2: " << ans_2 << endl;
cout << "Maximum difference: " << max_diff;
}
int main()
{
string str = "987342" ;
int N = str.size();
findValues(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void findValues(String str, int n)
{
long max_diff = Long.MIN_VALUE;
String ans_1 = "" , ans_2 = "" ;
for ( int i = 0 ; i < (n / 2 ); i++) {
String str1 = "" ;
for ( int j = 0 ; j <= i; j++) {
str1 += str.charAt(j);
}
for ( int k = (i + 1 ); k < n; k++) {
String str2 = "" ;
if ((k + i) < n) {
for ( int l = k; l <= k + i; l++) {
str2 += str.charAt(l);
}
long difference
= Difference(str1, str2);
if (difference > max_diff) {
max_diff = difference;
ans_1 = str1;
ans_2 = str2;
}
}
}
}
System.out.println( "String 1: " + ans_1 + "\n"
+ "String 2: " + ans_2);
System.out.println( "Maximum difference: "
+ max_diff);
}
public static void main(String[] args)
{
String str = "987342" ;
int N = str.length();
findValues(str, N);
}
static long Difference(String X, String Y)
{
long counter = 0 ;
for ( int i = X.length() - 1 ; i >= 0 ; i--) {
counter
+= (Math.abs(X.charAt(i) - Y.charAt(i)));
}
return counter;
}
}
|
Python3
def Difference(X, Y):
counter = 0
for i in range ( len (X) - 1 , - 1 , - 1 ):
counter + = abs ( int (X[i]) - int (Y[i]))
return counter
def findValues( Str , n):
max_diff = float ( "-inf" )
ans_1 = ""
ans_2 = ""
for i in range (n / / 2 ):
str1 = ""
for j in range (i + 1 ):
str1 = "".join([str1, Str [j]])
for k in range (i + 1 , n, 1 ):
str2 = ""
if ((k + i) < n):
for l in range (k, k + i + 1 ):
str2 = "".join([str2, Str [l]])
difference = Difference(str1, str2)
if (difference > max_diff):
max_diff = difference
ans_1 = str1
ans_2 = str2
print ( "String 1:" , ans_1)
print ( "String 2:" , ans_2)
print ( "Maximum Difference:" , max_diff)
Str = "987342"
N = len ( Str )
findValues( Str , N)
|
C#
using System;
public class GFG {
static void findValues( string str, int n)
{
long max_diff = Int64.MinValue;
string ans_1 = "" , ans_2 = "" ;
for ( int i = 0; i < (n / 2); i++) {
string str1 = "" ;
for ( int j = 0; j <= i; j++) {
str1 += str[j];
}
for ( int k = (i + 1); k < n; k++) {
string str2 = "" ;
if ((k + i) < n) {
for ( int l = k; l <= k + i; l++) {
str2 += str[l];
}
long difference
= Difference(str1, str2);
if (difference > max_diff) {
max_diff = difference;
ans_1 = str1;
ans_2 = str2;
}
}
}
}
Console.WriteLine( "String 1: " + ans_1 + "\n"
+ "String 2: " + ans_2);
Console.WriteLine( "Maximum difference: "
+ max_diff);
}
static public void Main()
{
string str = "987342" ;
int N = str.Length;
findValues(str, N);
}
static long Difference(String X, String Y)
{
long counter = 0;
for ( int i = X.Length - 1; i >= 0; i--) {
counter += (Math.Abs(X[i] - Y[i]));
}
return counter;
}
}
|
Javascript
function Difference(X, Y)
{
let counter = 0;
for (let i = X.length - 1; i >= 0; i--) {
counter += Math.abs(X[i] - Y[i]);
}
return counter;
}
function findValues(str, n)
{
let max_diff = Number.MIN_SAFE_INTEGER;
let ans_1 = "" , ans_2 = "" ;
for (let i = 0; i < (n / 2); i++) {
let str1 = "" ;
for (let j = 0; j <= i; j++) {
str1 += str[j];
}
for (let k = (i + 1); k < n; k++) {
let str2 = "" ;
if ((k + i) < n) {
for (let l = k; l <= k + i; l++) {
str2 += str[l];
}
let difference = Difference(str1, str2);
if (difference > max_diff) {
max_diff = difference;
ans_1 = str1;
ans_2 = str2;
}
}
}
}
console.log( "String 1: " + ans_1)
console.log( "String 1: " + ans_2)
console.log( "Maximum difference: " + max_diff);
}
let str = "987342" ;
let N = str.length;
findValues(str, N);
|
Output
String 1: 987
String 2: 342
Maximum difference: 15
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...