Count the maximum inversion count by concatenating the given Strings Last Updated : 21 Dec, 2023 Comments Improve Suggest changes Like Article Like 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 = 0Output: 3Explanation: Optimal string = "1110", hence total number of inversions is 3. Input: A = 0, B = 0, C = 1Output: 0Explanation: 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(); Output3Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count the maximum inversion count by concatenating the given Strings Anonymous Improve Article Tags : Mathematical Geeks Premier League DSA Data Structures Geeks Premier League 2023 +1 More Practice Tags : Data StructuresMathematical Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Like