Bishu and Soldiers

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 nint a[], int p)
{

    sort(aa + n);

    int sum[n];
    sum[0] = a[0];
    for (int i = 1i < ni++)
        sum[i] = sum[i - 1] + a[i];

    int u = upper_bound(aa + np) - a;
    cout << u << " " << sum[u - 1<< "\n";
}
int main()
{
    int n = 7;
    int a[n] = {1234567};
    int p = 3;

    bishuAndSoldiers(nap);

    return 0;
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


No comments:

Post a Comment