Skip to content

Commit 1ed871a

Browse files
committed
Bishu and Soldiers
1 parent 07f514b commit 1ed871a

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

DSA Crack Sheet/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
- [Zero Sum Subarrays](https://practice.geeksforgeeks.org/problems/zero-sum-subarrays1825/1# "view question") - [Cpp Solution](./solutions/Zero%20Sum%20Subarrays.cpp)
112112
- [Product array puzzle](https://practice.geeksforgeeks.org/problems/product-array-puzzle4525/1 "view question") - [Cpp Solution](./solutions/Product%20array%20puzzle.cpp)
113113
- [Sort by Set Bit Count](https://practice.geeksforgeeks.org/problems/sort-by-set-bit-count1153/1# "view question") - [Cpp Solution](./solutions/Sort%20by%20Set%20Bit%20Count.cpp)
114+
- [Minimum Swaps to Sort](https://practice.geeksforgeeks.org/problems/minimum-swaps/1# "view question") - [Cpp Solution](./solutions/Minimum%20Swaps%20to%20Sort.cpp)
115+
- [Bishu and Soldiers](https://www.hackerearth.com/practice/algorithms/searching/binary-search/practice-problems/algorithm/bishu-and-soldiers/ "view question") - [Cpp Solution](./solutions/Bishu%20and%20Soldiers.cpp)
114116
- []( "view question") - [Cpp Solution](./solutions/.cpp)
115117

116118
### Linked-List
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Bishu and Soldiers
3+
==================
4+
5+
Bishu went to fight for Coding Club. There were N soldiers with various powers. There will be Q rounds to fight and in each round Bishu's power will be varied. With power M, Bishu can kill all the soldiers whose power is less than or equal to M(<=M). After each round, All the soldiers who are dead in previous round will reborn.Such that in each round there will be N soldiers to fight. As Bishu is weak in mathematics, help him to count the number of soldiers that he can kill in each round and total sum of their powers.
6+
7+
1<=N<=10000
8+
1<=power of each soldier<=100
9+
1<=Q<=10000
10+
1<=power of bishu<=100
11+
12+
Sample Input
13+
7
14+
1 2 3 4 5 6 7
15+
3
16+
3
17+
10
18+
2
19+
Sample Output
20+
3 6
21+
7 28
22+
2 3
23+
24+
Time Limit: 1
25+
Memory Limit: 256
26+
27+
Explanation:
28+
In first round bhishu power is 3
29+
So there are 3 soldiers whose power is <=3 and the sum of their power is 1+2+3=6
30+
therefore ans= 3 6
31+
same for the next round
32+
*/
33+
34+
#include <bits/stdc++.h>
35+
using namespace std;
36+
37+
int main()
38+
{
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
42+
int n;
43+
cin >> n;
44+
vector<int> powers(n, 0);
45+
for (auto &i : powers)
46+
cin >> i;
47+
sort(powers.begin(), powers.end());
48+
49+
vector<int> sum(n, 0);
50+
sum[0] = powers[0];
51+
for (int i = 1; i < n; ++i)
52+
sum[i] = sum[i - 1] + powers[i];
53+
54+
int t;
55+
cin >> t;
56+
while (t--)
57+
{
58+
int m;
59+
cin >> m;
60+
int ans = upper_bound(powers.begin(), powers.end(), m) - powers.begin();
61+
cout << ans << " " << sum[ans - 1] << endl;
62+
}
63+
return 0;
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Minimum Swaps to Sort
3+
=====================
4+
5+
Given an array of n distinct elements. Find the minimum number of swaps required to sort the array in strictly increasing order.
6+
7+
Example 1:
8+
Input:
9+
nums = {2, 8, 5, 4}
10+
Output:
11+
1
12+
Explaination:
13+
swap 8 with 4.
14+
15+
Example 2:
16+
Input:
17+
nums = {10, 19, 6, 3, 5}
18+
Output:
19+
2
20+
Explaination:
21+
swap 10 with 3 and swap 19 with 5.
22+
23+
Your Task:
24+
You do not need to read input or print anything. Your task is to complete the function minSwaps() which takes the nums as input parameter and returns an integer denoting the minimum number of swaps required to sort the array. If the array is already sorted, return 0.
25+
26+
Expected Time Complexity: O(nlogn)
27+
Expected Auxiliary Space: O(n)
28+
29+
Constraints:
30+
1 ≤ n ≤ 105
31+
1 ≤ numsi ≤ 106
32+
*/
33+
34+
int minSwaps(vector<int> &nums)
35+
{
36+
int n = nums.size();
37+
vector<pair<int, int>> arr;
38+
for (int i = 0; i < n; ++i)
39+
arr.push_back({nums[i], i});
40+
int ans = 0;
41+
vector<int> visited(n, 0);
42+
sort(arr.begin(), arr.end());
43+
44+
for (int i = 0; i < n; ++i)
45+
{
46+
if (visited[i] || i == arr[i].second)
47+
continue;
48+
49+
int j = i;
50+
int cycleSize = 0;
51+
while (!visited[j])
52+
{
53+
visited[j] = 1;
54+
j = arr[j].second;
55+
cycleSize++;
56+
}
57+
58+
if (cycleSize > 0)
59+
ans += (cycleSize - 1);
60+
}
61+
62+
return ans;
63+
}

0 commit comments

Comments
 (0)