
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert an array to reduced form (Using vector of pairs) in C++
In this tutorial, we will be discussing a program to convert an array to its reduced form using vector of pairs.
For this we will be provided with an array. Our task is to convert the given array in its reduced form such that it only contains elements ranging from 0 to n-1.
Example
#include <bits/stdc++.h> using namespace std; //converting array to its reduced form void convert(int arr[], int n){ //creating a vector of pairs vector <pair<int, int> > v; //putting elements in vector //with their indexes for (int i = 0; i < n; i++) v.push_back(make_pair(arr[i], i)); sort(v.begin(), v.end()); for (int i=0; i<n; i++) arr[v[i].second] = i; } //printing the array void print_array(int arr[], int n) { for (int i=0; i<n; i++) cout << arr[i] << " "; } int main(){ int arr[] = {10, 20, 15, 12, 11, 50}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Given Array is :\n"; print_array(arr, n); convert(arr , n); cout << "\nConverted Array:\n"; print_array(arr, n); return 0; }
Output
Given Array : 10 20 15 12 11 50 Converted Array: 0 4 3 2 1 5
- Related Questions & Answers
- Convert an array to reduced form (Hashing) in C++
- Convert a Vector to an array in Java
- Product of given N fractions in reduced form in C
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- How to convert nested array pairs to objects in an array in JavaScript ?
- Rearrange an Array in Maximum Minimum Form using C++
- Binary search in sorted vector of pairs in C++
- Find the Pairs of Positive Negative values in an Array using C++\n
- Sum of XOR of all pairs in an array in C++
- Count divisible pairs in an array in C++
- K-diff Pairs in an Array in C++
- Array of Doubled Pairs in C++
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- how to convert vector to string array in java
Advertisements