Skip to content

Commit 0d72d02

Browse files
Create minIncrementForUniqueArray.cpp
1 parent e59c614 commit 0d72d02

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
int minIncrementForUnique(vector<int>& nums) {
7+
sort(nums.begin(), nums.end());
8+
int ans = 0;
9+
for (int i = 1; i < nums.size(); i++) {
10+
if (nums[i] <= nums[i - 1]) {
11+
ans += nums[i - 1] - nums[i] + 1;
12+
nums[i] = nums[i - 1] + 1;
13+
}
14+
}
15+
return ans;
16+
}
17+
};
18+
19+
int main() {
20+
vector<int> nums;
21+
int n;
22+
23+
cout << "Enter the number of elements in the array: ";
24+
cin >> n;
25+
26+
cout << "Enter the elements of the array: ";
27+
for (int i = 0; i < n; ++i) {
28+
int num;
29+
cin >> num;
30+
nums.push_back(num);
31+
}
32+
33+
Solution s;
34+
int min_increments = s.minIncrementForUnique(nums);
35+
cout << "Minimum increments required: " << min_increments << endl;
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)