Reverse squared sum | Practice | GeeksforGeeks

Reena had been given an array arr[] of positive integers of size N.Help her to find the value A.
A = arr[n]*arr[n] - arr[n-1]*arr[n-1] + arr[n-2]*arr[n-2] - ......... upto index 1
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains a single integer N.
3. The second line contains N space-separated positive integers represents array arr.

Output: For each test case, print the value of A in a single line

Constraints:
1. 1 <= T <= 10
2. 1 <= N <= 100000
3. 1 <= arr[i] <= 100

Example:
Input:
2
3
1 2 3
4
4 8 1 2
Output:
6
51
Explanation:
Test case 1 : 3*3 - 2*2 + 1*1 = 6
0
skrajenf322 months ago
 
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef unsigned long UL;
typedef unsigned long long ULL;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<vs> vvs;

double PI = acos(-1);

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(a,b,c) for(int (a) = (b); (a) < (c); (a)++)
#define FORD(a,b,c) for(int (a) = (b); (a) > (c); (a)--)
#define FORS(i,n) FOR(i,0,n)
#define FOREACH(a, b) for(auto&(a): (b))
#define COUTI(a,i) cout<<a[i]<<" "
#define COUT(a) cout<<a<<" "
#define pb push_back
#define MAX(a, b) a = max(a,b)
#define MIN(a, b) a = min(a,b)
#define SZ(any) (int)any.size()
#define arsz(arr) sizeof(arr)/sizeof(arr[0])
int main(){
   fastio
   int t;
   cin>>t;
   while(t--){
       int n;
       cin>>n;
       int A=0;
       while(n--){
           int m;
           cin>>m;
           A+=pow(-1,n)*m*m;
       }
       cout<<A<<"\n";
   }
   return 0;
}
0
mohituniyal462 months ago
A simple answer
 
#include <iostream>
#include<cmath>
using namespace std;
int main() {
//code
int t;
cin>>t;
while(t--){
    int n, A=0;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    for(int i=0;i<n;i++){
        A+=pow(-1, i)*arr[n-1-i]*arr[n-1-i];
    }
    cout<<A<<endl;
}
return 0;
}
0
singhalyash813 months ago
#include <iostream>#include <bits/stdc++.h>using namespace std;
int main() {//codeios_base::sync_with_stdio(NULL);cin.tie(NULL);int t;cin>>t;while(t--){    long long n;    cin>>n;    long long sum=0;    long long arr[n];    for(int i=0;i<n;i++)    {        cin>>arr[i];    }    int p=1;    int neg=1;    long long res=0;    for(int i=n-1;i>=0;i--)    {        if(p==1)        {            res+=(arr[i]*arr[i]);            p-=1;            neg=1;        }        else if(neg==1)        {            res-=(arr[i]*arr[i]);            neg-=1;            p=1;        }    }    cout<<res<<endl;}return 0;}
0
durpb12013 months ago
c++ code
int t,n;
    cin>>t;
    while(t--)
    {
       cin>>n;
       int arr[n],sumEvenPos=0,sumOddPos=0;
       for(int i=0;i<n;i++)
       {
          cin>>arr[i];
          if(i%2==0)
          {
              sumEvenPos+=(arr[i]*arr[i]);
          }
          else
          {
              sumOddPos+=(arr[i]*arr[i]);
          }
       }
       if(n%2!=0)
       {
           int ans=sumEvenPos-sumOddPos;
           cout<<ans<<"\n";
       }
       else
       {
           int ans=sumOddPos-sumEvenPos;
           cout<<ans<<"\n";
       }
    }
	
0
shuklaps11193 months ago
/*package whatever //do not write package name here */
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) { //code Scanner sc = new Scanner(System.in); int t=sc.nextInt();   while(t>0){     int s = sc.nextInt();     int a[]=new int[s];     int op = 0;     int tp=0;          for(int i=0;i<s;i++)         a[i]=sc.nextInt();              for (int i=s-1;i>=0;i--){         if(tp%2==0){             op += (a[i]*a[i]);             tp++;         }else{             op -= (a[i]*a[i]);             tp++;         }     }     System.out.println(op);     t--; }}}
0
kewatshubham993 months ago
/*package whatever //do not write package name here */

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
	public static void main (String[] args) {
		//code
		Scanner scan = new Scanner(System.in);
		int test = scan.nextInt();
		
		
		for(int i=0;i<test;i++){
		    int result = 0;
		    int temp = 0;
		    int size = scan.nextInt();
		    int[] arr = new int[size];
		    
		    for(int j=0;j<size;j++){
		        arr[j] = scan.nextInt();
		    }
		    
		    for(int j=size-1;j>=0;j--){
		        if(temp%2==0){
		            result = result+(arr[j]*arr[j]);
		            temp++;
		        }else{
		            result = result-(arr[j]*arr[j]);
		            temp++;
		        }
		    }
		    System.out.println(result);
		}
	}
}
0
krishnavakte255 months ago
#include <iostream>using namespace std;int square(int i){ return i*i;}
int reversesquare(int arr[],int i){ if(i==0){   return square(arr[i]); } if(i==1){   return square(arr[i])-square(arr[i-1]); } return square(arr[i])-square(arr[i-1])+reversesquare(arr,i-2);}
//Compiler version g++ 6.3.0
int main(){   int t;   cin>>t;   int sol[t];   for(int i=0;i<t;i++){     int n;     cin>>n;     int arr[n];     for(int j=0;j<n;j++){       cin>>arr[j];       }        sol[i]=reversesquare(arr,n-1);     }     for(int i=0;i<t;i++){       cout << sol[i]<<endl;     }           return 0;}
0
kamalisrijothi5 months ago
python code
#coden1=int(input())for i in range(n1):   x=int(input())   a1=list(map(int,input().split()))   a=a1[::-1]   o=0   e=0   for j in range(x):       if(j%2==0):           e=e+a[j]**2       else:           o=o+a[j]**2   print(e-o)
0
e20040806 months ago
runtime: 0.1/7.6
for _ in range(int(input())):
    n = int(input())
    arr = input().split()
    s = 0
    for i in range(n):
        s += int(arr[-1-i]) ** 2 * ((1-i%2)*2-1)
    print(s)
+2
c1phani1simha6 months ago
#include<iostream>#include<vector>using namespace std;int main(){int t,n;cin>>t;while(t--){    cin>>n;    int arr[n];    for(int i=0;i<n;i++)    {        cin>>arr[i];    }    int a,b,o=0,e=0,sum;   for(int y=0;y<n;y=y+2)    {        b=arr[y]*arr[y];        e=e+b;        if(y+1<n)        {            a=arr[y+1]*arr[y+1];            o=o+a;        }            }    if(n%2==0)    {        sum=o-e;    }    else    sum=e-o;    cout<<sum<<endl;    }return 0;}
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.