Type the Question

Saturday, February 16, 2019

Question Name:Game Of Strengths

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
 
int main() {
     ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 int num;
 cin >> num;          // Reading input from STDIN
 while(num--)
 {
   
     int n;
     cin>>n;
     int A[n];
     int ma=0;
     long long sum=0;
     for(int i=0;i<n;++i)
     {
     cin>>A[i];
     if(ma<A[i])
     ma=A[i];
     sum=sum+A[i];
     }
     sort(A,A+n);
     long long res=0;
     for(int i=0;i<n-1;++i)
     {
         for(int j=i+1;j<n;++j)
         {
             res=res+abs(A[i]-A[j]);
         }
       
     }
     res=res*ma;
     cout<<res%1000000007<<"\n";
 }
}
 

Problem Description

Andrew is very fond of Maths.He has N boxes with him,in each box there is some value which represents the Strength of the Box.The ith box has strength A[i]. He wants to calculate the Overall Power of the all N Boxes.
Overall Power here means Sum of Absolute Difference of the strengths of the boxes(between each pair of boxes) multiplied by the Maximum strength among N boxes. Since the Overall Power could be a very large number,output the number modulus 10^9+7(1000000007).
Input
First line of the input contains the number of test cases T. It is followed by T test cases. Each test case has 2 lines. First line contains the number of boxes N. It is followed by a line containing N elements where ith element is the strength of Andrew’s ith box.
Output
For each test case, output a single number, which is the Overall Power for that testcase.
Constraints
1<=T<= 10
2<=N<=10^5
0<=A[i]<=10^9
  • Test Case 1
    Input (stdin)
    1
    3
    3 1 2
    Expected Output
    12
  • Test Case 2
    Input (stdin)
    2
    2
    1 2
    5
    4 5 3 1 2
    Expected Output
    2
    100

No comments:

Post a Comment

Question Name:TOWER OF HANOI

#include < bits / stdc ++. h > #define lli long long using namespace std ; lli dp [ 202 ]; int main () { int t , n ; ...