Setter: Ashish Kashyap
Tester : Gaurav Kumar, Pratyaksh Gupta
DIFFICULTY
CakeWalk
PREREQUISITES
None
PROBLEM
Problem Link - Mathematical Madness of Spiderman
EXPLANATION
To answer the problem, we must first calculate the sum of all visitor’s powers , then do a modulo operation with M and print it, and finally display the sum of their powers in reverse order on the next line. The sole precaution needed was to make the type of total variable datatype (variable which is storing the resultant sum) large enough to prevent integer overflow due to the problem’s large constraints.
TIME COMPLEXITY
The time complexity is O(N) per test case
SOLUTIONS
Editorialist’s Solution
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n >> m;
long long int sum = 0;
long long int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
sum += arr[i];
}
cout << sum % m << endl;
for (int i = n - 1; i >= 0; i--)
{
cout << arr[i] << " ";
}
cout << endl;
}
return 0;
}
Tester’s Solution
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int test=sc.nextInt();
for(;test>0;test--){
int n=sc.nextInt();
long m=sc.nextLong();
long arr[]=new long[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextLong();
long sum=0;
for(int i=0;i<n;i++){
sum+=arr[i];
sum%=m;
}
System.out.println(sum);
for(int i=n-1;i>=0;i--)
System.out.print(arr[i]+" ");
System.out.println();
}
}
}
Feel free to share your approach. Suggestions are welcomed as always. : )