Max distance between first and last indexed element in Binary String
Last Updated :
24 Mar, 2023
Given a binary string S of length N along with Y[] of size K. Then you have to perform K number of queries for each i (1 <= i <= K) where you have to output the maximum distance between the first and last indexed character of S among all the queries, by doing the following operation for each query:
- Reverse the value of Si, If it is 0 then 1 or vice-versa.
- The distance between two adjacent characters is 2 if the elements are equal otherwise 1.
Note: After each query String S resets to its initial value.
Examples:
Input: N = 4, K = 2, S = 0110, Y[] = {3, 1}
Output: 5
Explanation:
- First Query: After changing S3 from 1 to 0, S = 0100, Then characters at the first and second index are not equal so the distance between them = 1. The second and third characters are also not equal, Then the distance between them is 1. The third and fourth characters are the same, So the distance between them is equal to 2. Therefore, the total distance between the first and last indexed character is 1+1+2 = 4. Then S resets to its initial value.
- Second Query: After changing S1 from 0 to 1, S = 1110, Then characters at the first and second index are equal so the distance between them = 2. The second and third characters are also equal, Then the distance between them is 2. The third and fourth characters are not the same, So the distance between them is equal to 1. Therefore, the total distance between the first and last indexed character is 2+2+1 = 5.
The maximum distance between the first and last characters among all the queries is 5. Therefore, the output is 5.
Input: N = 6, K = 6, S = 010110, Y[] = { 2, 1, 3, 3, 4, 5 }
Output: 9
Explanation: It can be verified that the maximum distance between the first and last indexed character will be 5. Therefore, the output is 5.
Approach: Implement the idea below to solve the problem
The problem is a mathematical concept based and can be solved by using some mathematics.
Steps were taken to solve the problem:
- Create a character array let’s say Arr[] and initialize it with characters of S.
- Create two variables sum=0 and max=0.
- Run a loop for j=0 to less than N-1 and follow the below-mentioned steps under the scope of the loop:
- if(Arr[j] == Arr[j + 1]) then sum+=2 else sum+=1
- Run a loop for j =0 to less than K and follow the below-mentioned steps under the scope of the loop:
- if (a > 0), Where a = Y[i] -1
- if (Arr[a] == Arr[a – 1]) then sum-=1 else sum+=1
- (a < N – 1), Where a = Y[i] -1
- (Arr[a] == Arr[a +1]) then sum-=1 else sum+=1
- Update max, formally max=sum>max?sum: max
- Output the value of max.
Below is the code to implement the approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Max_Distance( int N, int K, string S, int Y[])
{
int sum = 0;
int max = 0;
for ( int j = 0; j < N - 1; j++) {
if (S[j] == S[j + 1])
sum = sum + 2;
else
sum = sum + 1;
}
for ( int j = 0; j < K; j++) {
int a = Y[j] - 1;
if (a > 0) {
if (S[a] == S[a - 1])
sum = sum - 1;
else
sum = sum + 1;
}
if (a < N - 1)
if (S[a] == S[a + 1])
sum = sum - 1;
else
sum = sum + 1;
max = sum > max ? sum : max;
if (S[a] == '1' )
S[a] = '0' ;
else
S[a] = '1' ;
}
cout<< max << endl;
}
int main()
{
int N = 6;
int K = 6;
string S = "010110" ;
int Y[] = { 2, 1, 3, 3, 4, 5 };
Max_Distance(N, K, S, Y);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 6 ;
int K = 6 ;
String S = "010110" ;
int Y[] = { 2 , 1 , 3 , 3 , 4 , 5 };
Max_Distance(N, K, S, Y);
}
static void Max_Distance( int N, int K, String S,
int [] Y)
{
char arr[] = S.toCharArray();
int sum = 0 ;
int max = 0 ;
for ( int j = 0 ; j < N - 1 ; j++) {
if (arr[j] == arr[j + 1 ])
sum = sum + 2 ;
else
sum = sum + 1 ;
}
for ( int j = 0 ; j < K; j++) {
int a = Y[j] - 1 ;
if (a > 0 ) {
if (arr[a] == arr[a - 1 ])
sum = sum - 1 ;
else
sum = sum + 1 ;
}
if (a < N - 1 )
if (arr[a] == arr[a + 1 ])
sum = sum - 1 ;
else
sum = sum + 1 ;
max = sum > max ? sum : max;
if (arr[a] == '1' )
arr[a] = '0' ;
else
arr[a] = '1' ;
}
System.out.println(max);
}
}
|
Python3
def Max_Distance(N, K, S, Y):
sum = 0
max_dist = 0
for j in range (N - 1 ):
if S[j] = = S[j + 1 ]:
sum + = 2
else :
sum + = 1
for j in range (K):
a = Y[j] - 1
if a > 0 :
if S[a] = = S[a - 1 ]:
sum - = 1
else :
sum + = 1
if a < N - 1 :
if S[a] = = S[a + 1 ]:
sum - = 1
else :
sum + = 1
max_dist = max ( sum , max_dist)
if S[a] = = '1' :
S = S[:a] + '0' + S[a + 1 :]
else :
S = S[:a] + '1' + S[a + 1 :]
print (max_dist)
N = 6
K = 6
S = "010110"
Y = [ 2 , 1 , 3 , 3 , 4 , 5 ]
Max_Distance(N, K, S, Y)
|
C#
using System;
public class Program
{
public static void Max_Distance( int N, int K, string S, int [] Y)
{
int sum = 0;
int max = 0;
for ( int j = 0; j < N - 1; j++)
{
if (S[j] == S[j + 1])
{
sum += 2;
}
else
{
sum += 1;
}
}
for ( int j = 0; j < K; j++)
{
int a = Y[j] - 1;
if (a > 0)
{
if (S[a] == S[a - 1])
{
sum -= 1;
}
else
{
sum += 1;
}
}
if (a < N - 1)
{
if (S[a] == S[a + 1])
{
sum -= 1;
}
else
{
sum += 1;
}
}
else
{
sum += 1;
}
max = sum > max ? sum : max;
if (S[a] == '1' )
{
S = S.Remove(a, 1).Insert(a, "0" );
}
else
{
S = S.Remove(a, 1).Insert(a, "1" );
}
}
Console.WriteLine(max);
}
public static void Main()
{
int N = 6;
int K = 6;
string S = "010110" ;
int [] Y = { 2, 1, 3, 3, 4, 5 };
Max_Distance(N, K, S, Y);
}
}
|
Javascript
function Max_Distance(N, K, s, Y)
{
let sum = 0;
let S = s.split( '' );
let max = 0;
for (let j = 0; j < N - 1; j++) {
if (S[j] == S[j + 1])
sum = sum + 2;
else
sum = sum + 1;
}
for (let j = 0; j < K; j++) {
let a = Y[j] - 1;
if (a > 0) {
if (S[a] == S[a - 1])
sum = sum - 1;
else
sum = sum + 1;
}
if (a < N - 1)
if (S[a] == S[a + 1])
sum = sum - 1;
else
sum = sum + 1;
max = sum > max ? sum : max;
if (S[a] == '1' )
S[a] = '0' ;
else
S[a] = '1' ;
}
console.log(max);
}
let N = 6;
let K = 6;
let S = "010110" ;
let Y = [2, 1, 3, 3, 4, 5];
Max_Distance(N, K, S, Y);
|
Time Complexity: O(N), As String S is traversed to calculate distance.
Auxiliary Space: O(N), As an array for storing characters, is used because the string is immutable in Java.
Share your thoughts in the comments
Please Login to comment...