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 the 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 the total sum of their powers.
Example:
Input: n = 7, a = [1,2,3,4,5,6,7], p = 3
Output: 3 6
Approach
C++
#include <bits/stdc++.h>using namespace std;void bishuAndSoldiers(int n, int a[], int p){sort(a, a + n);int sum[n];sum[0] = a[0];for (int i = 1; i < n; i++)sum[i] = sum[i - 1] + a[i];int u = upper_bound(a, a + n, p) - a;cout << u << " " << sum[u - 1] << "\n";}int main(){int n = 7;int a[n] = {1, 2, 3, 4, 5, 6, 7};int p = 3;bishuAndSoldiers(n, a, p);return 0;}
Read Interview Questions
Exception Handling Interview Questions
DBMS Interview Questions Set -1
DBMS Interview Questions Set -2
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment