Max distance between first and last indexed element in Binary String
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
- if (a > 0), Where a = Y[i] -1
- Output the value of max.
Below is the code to implement the approach:
Java
// Java code to implement the approach import java.io.*;import java.lang.*;import java.util.*; class GFG { // Driver Function public static void main(String[] args) throws java.lang.Exception { // Inputs according to statement int N = 6; int K = 6; String S = "010110"; int Y[] = { 2, 1, 3, 3, 4, 5 }; // Function call Max_Distance(N, K, S, Y); } // Method for calculating maximum distance static void Max_Distance(int N, int K, String S, int[] Y) { // Array of characters for // storing characters of S char arr[] = S.toCharArray(); // Variable for holding // sum of distances int sum = 0; int max = 0; // Loop for calculating sum for (int j = 0; j < N - 1; j++) { if (arr[j] == arr[j + 1]) sum = sum + 2; else sum = sum + 1; } // Loop running number of // times query asked 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; // Updating maximum length max = sum > max ? sum : max; if (arr[a] == '1') arr[a] = '0'; else arr[a] = '1'; } // Printing maximum distance System.out.println(max); }} |
9
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.






Please Login to comment...