Count the maximum inversion count by concatenating the given Strings
Last Updated :
21 Dec, 2023
Given A, the number of "1" strings, B number of "10" strings, and C number of "0" strings. The task is to count the maximum inversion count by concatenating these strings
Note: Inversion count is defined as the number of pairs (i, j) such that 0 ≤ i < j ≤ N-1 and S[i] = '1' and S[j] = '0'.
Examples:
Input: A = 2, B = 1, C = 0
Output: 3
Explanation: Optimal string = "1110", hence total number of inversions is 3.
Input: A = 0, B = 0, C = 1
Output: 0
Explanation: Only possible string = "0", hence total number of inversions is 0.
Approach: This can be solved with the following idea:
It is always optimal to include A and B strings in our answer. Try to form maximum strings from A and B, Increase the inversion by concatinating C at last. As it always contains 0 in it's string.
Below are the steps involved:
- Initialize a integer ans = 0.
- Form A * (B + C), add it to ans.
- Then form B and C, by B * C add it to ans.
- Try forming the ones with B to increase inversion.
- Return ans.
Below is the implementation of the code:
C++
// C++ code for the above approach:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Function to count maximum Inversion
int maxInversion(int A, int B, int C)
{
// Forming ABC
long long ans = A * 1LL * (B + C);
// Forming BC
ans += (B * 1LL * C);
// Checking all Pairs possible from B
ans += (B * 1LL * (B + 1) / 2);
// Return total count
return ans;
}
// Driver Code
int main()
{
int A = 2;
int B = 1;
int C = 0;
// Function call
cout << maxInversion(A, B, C);
return 0;
}
Java
// Java Implementation
public class Main {
public static void main(String[] args) {
int A = 2;
int B = 1;
int C = 0;
// Function call
System.out.println(maxInversion(A, B, C));
}
// Function to count maximum Inversion
public static long maxInversion(int A, int B, int C) {
// Forming ABC
long ans = A * (long) (B + C);
// Forming BC
ans += (B * (long) C);
// Checking all Pairs possible from B
ans += (B * (long) (B + 1) / 2);
// Return total count
return ans;
}
}
// This code is contributed by Sakshi
Python3
def max_inversion(A, B, C):
# Forming ABC
ans = A * (B + C)
# Forming BC
ans += B * C
# Checking all Pairs possible from B
ans += B * (B + 1) // 2
# Return total count
return ans
# Driver Code
if __name__ == "__main__":
A = 2
B = 1
C = 0
# Function call
print(max_inversion(A, B, C))
C#
using System;
class Program
{
// Function to count maximum Inversion
static long MaxInversion(int A, int B, int C)
{
// Forming ABC
long ans = A * 1L * (B + C);
// Forming BC
ans += B * 1L * C;
// Checking all Pairs possible from B
ans += B * 1L * (B + 1) / 2;
// Return total count
return ans;
}
// Driver Code
static void Main()
{
int A = 2;
int B = 1;
int C = 0;
// Function call
Console.WriteLine(MaxInversion(A, B, C));
}
}
JavaScript
function GFG(A, B, C) {
// Forming ABC
let ans = A * (B + C);
// Forming BC
ans += B * C;
// Checking all pairs possible from B
ans += (B * (B + 1)) / 2;
// Return total count
return ans;
}
// Driver Code
function main() {
// Given values
const A = 2;
const B = 1;
const C = 0;
// Function call
console.log(GFG(A, B, C));
}
main();
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Minimum count of Inversion Pairs possible by concatenating N binary strings in any order Given N strings in the form of array str, each of length M and containing only characters 'a' and 'b'. The task is to find the count of minimum number of Inversion Pairs possible in the resultant strings formed by concatenating all N strings in any order, without changing the order of any individual
8 min read
Count of three non-overlapping sub-strings which on concatenation forms a palindrome Given a string str, the task is to count the number of ways a palindromic substring could be formed by the concatenation of three sub-strings x, y and z of the string str such that all of them are non-overlapping i.e. sub-string y occurs after substring x and sub-string z occurs after sub-string y.E
7 min read
Count of valid pairs (X, Y) from given strings such that concatenating X with itself yields Y Given an array arr[] of N strings. Let X and Y be two strings, X and Y are said to be valid pairs if the rearrangement of the resultant string from the concatenation of X with X (i.e., X+X) gives Y. The task is to count the number of such valid pairs. Examples: Input: N = 4, arr[] = {âhackerâ, âacke
7 min read
Count of palindromes that can be obtained by concatenating equal length prefix and substrings Prerequisites: Z-algorithm Given a string S, the task is to find the maximum number of palindromes that can be formed after performing the given steps: Choose a non-empty prefix P and a non-empty substring T of equal length.Reverse either P or T and concatenate them. Note: P and T can be overlapping
6 min read
Maximum Strings Concatenation Given an array of strings, where each string consists of lowercase characters from a to j. You need to find the maximum number of strings that can be concatenated together such that the resulting string can be made out of exactly k distinct characters. Example: Input: n = 4, str = [ "abc", "de", "fg
8 min read