Open In App

Count the maximum inversion count by concatenating the given Strings

Last Updated : 21 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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();


Output

3









Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads