How to find the maximum value of pairs a string could have?

user3703441

You are given two s: N and K. Lun the dog is interested in strings that satisfy the following conditions:

  • The string has exactly N characters, each of which is either 'A' or 'B'.
  • The string s could have K pairs (i, j) (0 <= i < j <= N-1) such that s[i] = 'A' and s[j] = 'B'.

What's the maximum value of K a string of N characters could have?

JuniorCompressor

We can assume that 'A' are before 'B', since in each solution we can reorder the 'A' to the beginning of the string and get the same or bigger number of pairs. For example 'BAA' has no pairs, 'ABA' has one pair and 'AAB' has two pairs.

If we have a A at the beginning and b B then we have K = a * b pairs. So we need to optimise K = a * b given that a + b = N.

If N is even then we have:

a = b = N / 2, K = N * N / 4

and if N is odd we have:

a = (N - 1) / 2, b = (N + 1) / 2, K = (N * N - 1) / 4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to find a string already have the value in php

From Dev

how to find a string already have the value in php

From Dev

How to find the maximum number of pairs having difference less than a particular value?

From Dev

How to find the maximum number of pairs having difference less than a particular value?

From Dev

How can I find the key-value pairs that have smallest value in Spark-streaming?

From Dev

How can I find the maximum value within a string? excel

From Dev

How can I find the maximum value within a string?

From Dev

How to find maximum value in a table

From Dev

How to find maximum value in an array

From Dev

how to select a complex xml tag containing key value pairs that have a specific string using xslt

From Dev

Find the maximum amount of pairs in a list of pairs

From Dev

How to select rows that have a maximum value for a column

From Dev

How to parse json string into key value pairs?

From Dev

How to best "dynamically" make javascript have an conditional to find matches based on multiple possible key value pairs? Looking for a solution

From Dev

MongooseJS - How to find the element with the maximum value?

From Dev

How to find the maximum value of a function in Sympy?

From Dev

How to find the maximum value in entire Excel workbook

From Dev

How to find the maximum value among a range of count

From Dev

How to find maximum value of a parameter in an object in an array?

From Dev

How to group nodes that have common edges from given string of pairs?

From Dev

How to convert a string string of key-value pairs into an array

From Dev

How to convert a string string of key-value pairs into an array

From Dev

Find two pairs of pairs that sum to the same value

From Dev

How to find the number of (key , value) pairs in a map in scala?

From Dev

Find the maximum in a string

From Dev

How do I find the maximum value of an attribute value in an XML file?

From Dev

How to find maximum value in each row and convert it to specific value

From Dev

How to find maximum value in each row and convert it to specific value

From Dev

How can I have a group of JSpinner objects with a maximum total value?

Related Related

  1. 1

    how to find a string already have the value in php

  2. 2

    how to find a string already have the value in php

  3. 3

    How to find the maximum number of pairs having difference less than a particular value?

  4. 4

    How to find the maximum number of pairs having difference less than a particular value?

  5. 5

    How can I find the key-value pairs that have smallest value in Spark-streaming?

  6. 6

    How can I find the maximum value within a string? excel

  7. 7

    How can I find the maximum value within a string?

  8. 8

    How to find maximum value in a table

  9. 9

    How to find maximum value in an array

  10. 10

    how to select a complex xml tag containing key value pairs that have a specific string using xslt

  11. 11

    Find the maximum amount of pairs in a list of pairs

  12. 12

    How to select rows that have a maximum value for a column

  13. 13

    How to parse json string into key value pairs?

  14. 14

    How to best "dynamically" make javascript have an conditional to find matches based on multiple possible key value pairs? Looking for a solution

  15. 15

    MongooseJS - How to find the element with the maximum value?

  16. 16

    How to find the maximum value of a function in Sympy?

  17. 17

    How to find the maximum value in entire Excel workbook

  18. 18

    How to find the maximum value among a range of count

  19. 19

    How to find maximum value of a parameter in an object in an array?

  20. 20

    How to group nodes that have common edges from given string of pairs?

  21. 21

    How to convert a string string of key-value pairs into an array

  22. 22

    How to convert a string string of key-value pairs into an array

  23. 23

    Find two pairs of pairs that sum to the same value

  24. 24

    How to find the number of (key , value) pairs in a map in scala?

  25. 25

    Find the maximum in a string

  26. 26

    How do I find the maximum value of an attribute value in an XML file?

  27. 27

    How to find maximum value in each row and convert it to specific value

  28. 28

    How to find maximum value in each row and convert it to specific value

  29. 29

    How can I have a group of JSpinner objects with a maximum total value?

HotTag

Archive