Sum of Products | Practice | GeeksforGeeks

Given an array Arr of N integers.Calculate the sum of Bitwise ANDs(&) all the pairs formed by the given array.
Example 1:
Input:
N=3
Arr={5,10,15}
Output:
15
Explanation:
The bitwise Ands of all pairs are (5&10)=0
(5&15)=5 and (10&15)=10.Therefore,
total Sum=0+5+10=15.
Example 2:
Input:
N=4
Arr={10,20,30,40}
Output:
46
Explanation:
The sum of bitwise Ands 
of all pairs=0+10+8+20+0+8=46.

Your Task:
You don't need to read input or print anything.Your Task is to complete the function pairAndSum() which takes an Integer N and an array Arr as input parameters and returns the sum of bitwise Ands of all pairs.
Expected Time Complexity:O(N)
Expected Auxillary Space:O(1)
Constraints:
1<=N<=105
1<=Arri<=108
0
Nishchay Bhutani
This comment was deleted.
0
Nishchay Bhutani
This comment was deleted.
0
Manjunath Jadhav2 years ago
Manjunath Jadhav
https://practice.geeksforge...
guys anyone say what is the problem in my code..plz,plz,plz....
0
Shreya Patil2 years ago
Shreya Patil
/*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) {//codeScanner sc=new Scanner(System.in);int t=sc.nextInt();while(t-->0){    int n=sc.nextInt();    int arr[]=new int[n];    for(int i=0;i<n;i++) {="" arr[i]="sc.nextInt();" }="" int="" sum="0;" for(int="" i="0;i&lt;n;i++)" {="" for(int="" j="i+1;j&lt;n;j++)" {="" sum="sum+(arr[i]&amp;arr[j]);" }="" }="" system.out.println(sum);="" }="" }="" }<code="">
0
Sreedhar Karanam2 years ago
Sreedhar Karanam
C++ solution (0.01 sec):https://ide.geeksforgeeks.o...
0
penny53402 years ago
penny5340
shortest sol ever ET= 0.01upvote me for more .https://ide.geeksforgeeks.o...
-1
SHIVANI2 years ago
SHIVANI
#include <stdio.h>
int main() {    int t,n,arr[50],i,j,sum,a;    scanf("%d",&t);    while(t--)    {      scanf("%d",&n);      for(i=0;i<n;i++) {="" scanf("%d",&arr[i]);="" }="" sum="0;" i="0;" for(i="0;i&lt;n;i++)" {="" for(j="i+1;j&lt;n;j++)" {="" a="arr[i]&amp;arr[j];" sum="sum+a;" }="" }="" printf("%d\n",sum);="" }="" return="" 0;="" }="">
0
Shashwat Sharma2 years ago
Shashwat Sharma
0.14 sec....
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {//codeScanner sc = new Scanner(System.in);int t = sc.nextInt();while(t-->0){    int n = sc.nextInt();    int[] a = new int[n];    int i,j,prod=1,sum=0;    for(i=0;i<n;i++) a[i]="sc.nextInt();" for(i="0;i&lt;n-1;i++)" {="" for(j="i+1;j&lt;n;j++)" {="" prod="a[i]&amp;a[j];" sum+="prod;" }="" }="" system.out.println(sum);="" }="" }="" }="">
0
Aman chowdhury2 years ago
Aman chowdhury
/* C PROGRAM */time = 0.01
#include <stdio.h>
int main() {//codeint t;scanf("%d",&t);while(t--){    int n,i,j,sum=0;    scanf("%d",&n);    int a[n];    for(i=0;i<n;i++) {="" scanf("%d",&a[i]);="" }="" for(i="0;i&lt;n;i++)" {="" for(j="i;j&lt;n-1;j++)" {="" sum+="a[i]&amp;a[j+1];" }="" }="" printf("%d\n",sum);="" }="" return="" 0;="" }="">
0
Niraj Singh3 years ago
Niraj Singh
t=int(input())for _ in range(t):    n=int(input())    a=list(map(int,input().split()))    s=0    for i in range(n-1):        for j in range(i+1,n):            s+=a[i]&a[j]    print(s)
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.