King's War | Practice | GeeksforGeeks

King is getting ready for a war. He know his strengths and weeknesses so he is taking required number of soldiers with him. King can only defeat enemies with strongest and weakest power, and one soldier can only kill one enemy. Your task is to calculate that how many soldiers are required by king to take with him for the war.
Note: The array may contain duplicates.
 
Example 1:
Input:
N = 2
A[] = {1, 5}
Output:
0
Explanatation:
The king will itself be able to defeat all the enemies.
 
Example 2:
Input:
N = 3
A[] = {1, 2, 5}
Output:
1
Explanatation:
The king will defeat enemies with power
1 and 5, and he will need 1 soldier to
defeat the enemy with power 2.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function SoldierRequired() which takes the array A[] and its size N as inputs and returns the number of soldiers, king will need to take with him for the war.

Expected Time Complexity: O(NLogN)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 106 
1 ≤ Ai ≤ 106
Array may contain duplicates
0
hasnainraza1998hr1 month ago
C++, 0.5, O(n), without sorting
long long SoldierRequired(long long a[], long long n)   {        int weakest = *min_element(a,a+n);        int strongest = *max_element(a,a+n);        long long soldiers = 0;        for(int i=0;i<n;i++){            if(a[i]!=weakest && a[i]!=strongest)               soldiers++;        }        return soldiers;   }
0
rahulrs2 months ago
JAVA CODE................................ 
public static long SoldierRequired(long arr[], long n){       // Your code goes here        Arrays.sort(arr);       int strong = (int)arr[(int)(n-1)];       int weak = (int)arr[0];       long count=0;       if(n==2){           return 0;       }else{           for(int i=0;i<arr.length;i++){               if(arr[i]!=strong && arr[i]!=weak){                   count++;               }           }       }       return (long)count;   }
+1
princejee20192 months ago
C++ Code ||EASY TO UNDERSTAND|| 0(N*log(n))
class Solution{   public:       long long SoldierRequired(long long a[], long long n)   {       // Your code goes here        if(n<=2){           return 0;       }       sort(a,a+n);       long long  count =2;       for(int i =1;i<(n-1);i++){           if(a[0]==a[i]){               count++;           }           if(a[n-1]==a[i]){               count++;           }       }       return (n-count);   }};
0
vidhigupta123 months ago
long long SoldierRequired(long long a[], long long n)   {       // Your code goes here        long long min=0,max=0;       if(n<=1)       return 0;       sort(a,a+n);       for(long long i=1;i<n-1;i++)       {           if(a[0]==a[i])           {               min++;           }           else if(a[n-1]==a[i])           {               max++;           }       }       return n-2-min-max;   }
0
shashwatsatna3 months ago
I don't understand this question.
0
deveshamway4 months ago
O(N) C++ Solution :       long long SoldierRequired(long long arr[], long long n)   {       int max=INT_MIN, min =INT_MAX;       for(int i =0;i<n;i++){           if(arr[i]>max){               max= arr[i];           }           if(arr[i]<min){               min = arr[i];           }       }       long long count=n;       for(int i =0;i<n;i++){           if(arr[i]==min || arr[i]==max){               count--;           }       }       return count;   }
0
Rahul Kakkar1 year ago
Rahul Kakkar
long long SoldierRequired(long long a[], long long n)        {            // Your code goes here             sort(a,a+n);            int max = a[n-1];            int min = a[0];            long long count = 0;            int i = 0;            int j = n-1;            while(i<j) {="" if(a[i]="=min)" {="" count++;="" i++;="" }="" else="" if(a[j]="=max)" {="" count++;="" j--;="" }="" else="" break;="" }="" if(n="=2" ||="" n="=1)" {="" return="" 0;="" }="" return="" n-count;="" }<="" code="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.