Author: Anurag dubey
Tester: Anurag dubey
Editorialist: Anurag dubey
DIFFICULTY:
CAKEWALK, SIMPLE, EASY.
PREREQUISITES:
Math , bruteforce, greedy
PROBLEM:
we have just find out sum of all diagonal element
QUICK EXPLANATION:
First we have to find sum of all the perfect diagonal element and store it in sum1 then find sum of no perfect diagonal element and in last print it .
EXPLANATION:
first we have to find a sum of all the perfect diagonal element that is we have just find out the sum of array element in which row equal to column and store it to sum1
and now we have to find out sum of non diagonal element and add it to sum1 and print it out.
for example for n=2
consider matrix 1 2
3 4
first we will found sum of 1 and 4 and store it in sum1 and later we will find sum of 2 and 3 and store it in sum2 and later we will add sum1 and sum2 and print it thats all.
SOLUTIONS:
Setter's Solution
#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin >> n;
int a[n][n];
int m;
m = n;
int sum1 = 0;
int sum4 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
if (i == j)
{
sum1 += a[i][j];
}
sum4 += a[i][j];
}
}
if (n == 2 || n == 1)
{
cout << sum4;
}
else
{
int sum2 = 0;
int j = m - 1;
int i = 0;
while (i < n && j >= 0)
{
sum2 += a[i][j];
j--;
i++;
}
if (n % 2 == 0)
{
cout << sum1 + sum2;
}
else
{
int sum3 = sum1 + sum2;
int k = n / 2;
sum3 = sum3 - a[k][k];
cout << sum3;
}
}
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}
Tester's Solution
#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin >> n;
int a[n][n];
int m;
m = n;
int sum1 = 0;
int sum4 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
if (i == j)
{
sum1 += a[i][j];
}
sum4 += a[i][j];
}
}
if (n == 2 || n == 1)
{
cout << sum4;
}
else
{
int sum2 = 0;
int j = m - 1;
int i = 0;
while (i < n && j >= 0)
{
sum2 += a[i][j];
j--;
i++;
}
if (n % 2 == 0)
{
cout << sum1 + sum2;
}
else
{
int sum3 = sum1 + sum2;
int k = n / 2;
sum3 = sum3 - a[k][k];
cout << sum3;
}
}
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}
Editorialist's Solution
#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin >> n;
int a[n][n];
int m;
m = n;
int sum1 = 0;
int sum4 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> a[i][j];
if (i == j)
{
sum1 += a[i][j];
}
sum4 += a[i][j];
}
}
if (n == 2 || n == 1)
{
cout << sum4;
}
else
{
int sum2 = 0;
int j = m - 1;
int i = 0;
while (i < n && j >= 0)
{
sum2 += a[i][j];
j--;
i++;
}
if (n % 2 == 0)
{
cout << sum1 + sum2;
}
else
{
int sum3 = sum1 + sum2;
int k = n / 2;
sum3 = sum3 - a[k][k];
cout << sum3;
}
}
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}