Given an array A[] of integers find sum of product of all pairs of array elements
Input:
First line consists of T test cases. First line of every test case
consists of N, denoting number of elements of array. Second line
consists of elements of array.
Output:
Single line output, print the sum of products.
Constraints:
1<=T<=100
1<=N<=100
Solve the problem using pointers and (if possible) using operators
Example:
Input:
2
3
1 3 4
4
2 3 4 5
Output:
19
71
Answer:
Explanation:
Outer loop is for the number of testcases.
Then in each test case, first using a for loop, n array elements are taken input and stored in array a.
Then using nested for loops, each pair is accessed using pointers(as mentioned above) and sum of products is calculated and printed at the end of test case.
Code:
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
for(int k = 0; k<t; k++)
{
int n, sum = 0;
cin>>n;
int a[n];
for(int i=0; i<n; i++)
{
cin>>a[i];
}
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
sum = sum + (*(a+i) * *(a+j));
}
}
cout<<sum<<endl;
}
return 0;
}
Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
Given an array A[] of integers find sum of product of all pairs of array elements...
Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of some of the testsets. Question 2: Understanding Orders Given an array A of size N, find the number of ordered pairs (i, j) such that i < j and A[i] < A[j]. Input: • First line contains one integer, N, size of array. • Second line contains N space separated integers denoting the elements of the array A. Output: Print the total number of...
Java code
INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...
Given an array of integers representing heights of consecutive steps, we say that a tumble is a sequence of strictly decreasing numbers and the height of the tumble is the difference between the height of the top step and the bottom step. For example, if there are consecutive steps with heights 16, 9, 4 then they form a tumble of height 12 (= 16-4). With consecutive steps of heights, 0, 4, 12, 4, 5, 7, 2, 1 there is a...
(JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
(+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100) to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0 (how many) Display the number of integers < 0 (how many) Display the...
Java code
ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing Problem Description Understanding how to interpret test scores is a valuable skill for every teacher. That's because test score interpretation enables teachers to understand how their students' performance for each tests of the course. Average test score is one of the indicators to determine the class performance for the test. For each test, we need to calculate the average score and determine the percentage...
c programming. Given a sorted array of 20 integers and a target integer value, your program needs to print out all pairs of integers which have sum equal to the target value. If there is no pair of integers to return, print out “No pair of integers in the given array can be summed to the target value”. The array of integers and the target value should be given from the keyboard (taken as user input). Inputs and Outputs should...
Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays using pointers. Input: 4 1 2 3 4 5 4 5 6 7 8 where: First line represents the number of elements in the first array. Second line represents the elements in the first array. Third line represents the number of elements in the second array. Fourth line represents the elements in the second array. Output: 4 5 6 7...