Problem
A function on a binary string T of length M is defined as follows:
F(T) = number of indices such that
You are given a binary string S of length N. You have to divide string S into two subsequences such that:
- Each character of string S belongs to one of and .
- The characters of and .must occur in the order they appear in S.
Find the maximum possible value of
NOTE: A binary string is a string that consists of characters `0` and `1`. One of the strings , can be empty.
Input format
- The first line contains T denoting the number of test cases. The description of T test cases is as follows:
- For each test case:
- The first line contains N denoting the size of string S.
- The second line contains the string S.
Output format
For each test case, print the maximum possible value of in a separate line.
Constraints
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
The first test case
- One possible division is (empty string). Here There is no way to divide the given string to obtain a greater answer.
The second test case
- The optimal division is Here There is no way to divide the given string to obtain a greater answer.
The third test case
- For any possible division of S,
Code(C++):-
#include<bits/stdc++.h>
using namespace std;
int solve (int n, string s) {
// Write your code here
string s1 = "", s2 = "";
s1 += s[0];
bool flag = 0;
if(s[0]=='0') flag = 1;
for(int i=1;i<n;i++){
if(flag){
if(s[i] == '1'){
s1 += '1';
flag = 0;
}
else {
s2 += '0';
}
}
else{
if(s[i] == '0'){
s1 += '0';
flag = 1;
}
else {
s2 += '1';
}
}
}
//cout<<s1<<" "<<s2;
int ans = 0,n2 = s2.length();
for(int i=1;i<n2;i++){
if(s2[i]!=s2[i-1]) ans++;
}
ans += s1.length()-1;
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i = 0; t_i < T; t_i++)
{
int N;
cin >> N;
string S;
cin >> S;
int out_;
out_ = solve(N, S);
cout << out_;
cout << "\n";
}
}
Code(JAVA):-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
//BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = (int)Integer.parseInt(br.readLine());
for(int x = 0;x<t;x++){
int n = (int)Integer.parseInt(br.readLine());
String s = br.readLine();
String[] subs = subStrings(s);
int subsval = function(subs[0]) + function(subs[1]);
System.out.println(subsval);
}
}
public static int function(String s){
if(s.length()<2) return 0;
char[] carray = s.toCharArray();
int result = 0;
for(int i=0;i<s.length()-1;i++){
if(carray[i]!=carray[i+1]) result++;
}
return result;
}
public static String[] subStrings(String s){
String[] subs = new String[2];
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
sb1.append(s.charAt(0));
char prev = s.charAt(0);
for(int i =1;i<s.length();i++){
if(s.charAt(i)!=prev){
sb1.append(s.charAt(i));
prev = s.charAt(i);
}
else sb2.append(s.charAt(i));
}
subs[0] = sb1.toString();
subs[1] = sb2.toString();
return subs;
}
}
Recommended Post :-
HCL Coding Questions:-
Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-
- Swap the adjacent characters of the string
- Double the vowel characters in the string
- Character with their frequency
- Program to find the closest value
Must check this:-
Companies interview:-
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Full C course:-
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
- How to check that given number is power of 2 or not
- How to fix limit in double and floating numbers after dot (.) in c++
- How to print a double or floating point number in scientific notation and fixed notation
- How to take input a string in c
- How to reduce the execution time of program in c++.
Cracking the coding interview:-
Array and string:-
Tree and graph:-
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Sum of Digits of a Five Digit Number | hackerrank practice problem solution
- 1D Arrays in C | hackerrank practice problem solution
- Array Reversal | hackerrank practice problem solution
- Printing Tokens | hackerrank practice problem solution
- Digit Frequency | hackerrank practice problem solution
- Calculate the Nth term | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments