C++ Arrangement of smaller values in another array
Two arrays A and B are provided in this tutorial. For example, we need to output any arrangement of A such that the index of A[ I ] > B[ I ] is maximized, for example
Input: A = [12, 22, 41, 13], B = [1, 20, 10, 12] Output: 12, 22, 41, 13 Input: A = [2, 5, 9, 7], B = [1, 12, 4, 54] Output: 2 7 5 9 Multiple answers can be present in that case we are simply going to print any one of the answers.
In this problem, we need to maximize A[ i ] > B[ i ], so we'll solve this problem greedily.
Method to find the solution
In this method, we now sort the two arrays first; we greedily check each index of array B such that A[i] is larger than It's more important then to put that element into a vector.
Example
#include <bits/stdc++.h> using namespace std; int main(){ int A[] = { 2, 5, 9, 7 }; int B[] = { 1, 12, 4, 54 }; int n = sizeof(A) / sizeof(int); // size of our arrays vector<pair<int, int> > A_pair, B_pair; /***********************We are linking element to its position***********/ for (int i = 0; i < n; i++) A_pair.push_back({A[i], i}); for (int i = 0; i < n; i++) B_pair.push_back({B[i], i}); /***********************************************************************/ /*****Sorting our pair vectors********************/ sort(A_pair.begin(), A_pair.end()); sort(B_pair.begin(), B_pair.end()); int i = 0, j = 0, ans[n]; memset(ans, -1, sizeof(ans)); // initializing all the elements with value -1 vector<int> remaining; // this will store our elements which have lesser value than elemnt present in B. while (i < n && j < n) { // as our array is sorted then if we find any element in //current index which has less value than B of current index then // automatically it will have less value than other elements of B // that's why we push such indices in remaining otherwise we push element in ans if (A_pair[i].first > B_pair[j].first) { ans[B_pair[j].second] = A_pair[i].first; i++; j++; } else { remaining.push_back(i); i++; } } j = 0; for (int i = 0; i < n; ++i){ // now if any index of answer is unchanged so that means //we need to fill that position with the remaining elements if (ans[i] == -1){ ans[i] = A_pair[remaining[j]].first; j++; } } for (int i = 0; i < n; i++) // printing our answer cout << ans[i] << " "; return 0; }
Output
2 7 5 9
Explanation of the above code
In this approach we first link all the elements to their index so that Their old indexes are still retained when sorting. We sort the two vector pairs and now we greedily search for the answer while looping through the two arrays and if we get the index of A_pair it has a superior value than B_pair so we store it in our array (and at the position of B_pair) Otherwise, since we have already sorted both vectors, we know we won't be able to use this value of A_pair, so we push that element index into the remaining vector and now we fill the array with the help of the remaining vector and print the answer.
Conclusion
In this tutorial we solved a problem to find the permutation of an array with smaller values from another array. We also learned a C program for this problem and our complete approach to solving it. We can write the same program in other languages such as C, java, python and other languages. We hope you found this tutorial helpful.
The above is the detailed content of C++ Arrangement of smaller values in another array. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the popularity of cryptocurrencies, virtual currency trading platforms have emerged. The top ten virtual currency trading platforms in the world are ranked as follows according to transaction volume and market share: Binance, Coinbase, FTX, KuCoin, Crypto.com, Kraken, Huobi, Gate.io, Bitfinex, Gemini. These platforms offer a wide range of services, ranging from a wide range of cryptocurrency choices to derivatives trading, suitable for traders of varying levels.

There are many ways to center Bootstrap pictures, and you don’t have to use Flexbox. If you only need to center horizontally, the text-center class is enough; if you need to center vertically or multiple elements, Flexbox or Grid is more suitable. Flexbox is less compatible and may increase complexity, while Grid is more powerful and has a higher learning cost. When choosing a method, you should weigh the pros and cons and choose the most suitable method according to your needs and preferences.

How to adjust Sesame Open Exchange to Chinese? This tutorial covers detailed steps on computers and Android mobile phones, from preliminary preparation to operational processes, and then to solving common problems, helping you easily switch the Sesame Open Exchange interface to Chinese and quickly get started with the trading platform.

The top ten cryptocurrency trading platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

The calculation of C35 is essentially combinatorial mathematics, representing the number of combinations selected from 3 of 5 elements. The calculation formula is C53 = 5! / (3! * 2!), which can be directly calculated by loops to improve efficiency and avoid overflow. In addition, understanding the nature of combinations and mastering efficient calculation methods is crucial to solving many problems in the fields of probability statistics, cryptography, algorithm design, etc.

Top Ten Virtual Currency Trading Platforms 2025: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

A safe and reliable digital currency platform: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.

Recommended safe virtual currency software apps: 1. OKX, 2. Binance, 3. Gate.io, 4. Kraken, 5. Huobi, 6. Coinbase, 7. KuCoin, 8. Crypto.com, 9. Bitfinex, 10. Gemini. Security, liquidity, handling fees, currency selection, user interface and customer support should be considered when choosing a platform.
