Multiply left and right array sum. | Practice | GeeksforGeeks

Pitsy needs help with the given task by her teacher. The task is to divide an array into two sub-array (left and right) containing n/2 elements each and do the sum of the subarrays and then multiply both the subarrays.
Note: If the length of the array is odd then the right half will contain one element more than the left half.
Example 1:
Input : arr[ ] = {1, 2, 3, 4}
Output : 21
Explanation:
Sum up an array from index 0 to 1 = 3
Sum up an array from index 2 to 3 = 7
Their multiplication is 21.


Example 2:
Input : arr[ ] = {1, 2} 
Output :  2 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function multiply() that takes an array (arr), sizeOfArray (n), and return the sum of the subarrays and then multiply both the subarrays. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ A[i] ≤ 100
0
diagovenk3 days ago
Hello guys . Runtime 0.24 sec. Easy and simple solution;
public static int multiply (int arr[], int n) {       //Complete the function       int l = (n>>1)-1;       int r = n>>1;       int lSum = 0;       int rSum = 0;       int sumOfProduct = 0;       while(l>=0){           lSum += arr[l];           rSum += arr[r];           l--;           r++;       }       if((n&1)==1){       rSum += arr[n-1];       }       sumOfProduct = rSum * lSum;       return sumOfProduct;   }
0
harshscode4 days ago
 int sum1=0,sum2=0;
    if(n%2==0)
    {
    for(int i=0;i<n/2;i++)
    sum1+=a[i];
    for(int i=n/2;i<n;i++)
    sum2+=a[i];
    }
    else
    {
        for(int i=0;i<=(n/2-1);i++)
        sum1+=a[i];
        for(int i=n/2;i<n;i++)
        sum2+=a[i];
    }
    return sum1*sum2;
0
shubhamshrivas16 days ago
def multiply (arr, n) :    #Complete the function   mid = n//2   s1,s2 =0,0   for i in range(0,mid):       s1+=arr[i]   for j in range(mid,n):       s2+=arr[j]   return s1*s2
0
mayank180919991 week ago
int sum1=0,sum2=0;
       for(int i=0;i<n/2;i++){
           sum1=sum1+arr[i];
       }
       for(int i=n/2;i<n;i++){
           sum2=sum2+arr[i];
       }
    return sum1*sum2;
0
rahulfrndz022 weeks ago
JAVA CODE
class Complete{        // Function for finding maximum and value pair   public static int multiply (int arr[], int n) {       //Complete the function              int lSum=0;       int rSum=0;       int multi=1;              for(int i=0; i<n/2; i++)       {           lSum+=arr[i];       }       for(int i=(n/2); i<n; i++)       {           rSum+=arr[i];       }       multi=lSum*rSum;       return multi;   }      }
0
alpha_hydranoid3 weeks ago
C++ solution, 0.0 sec
int multiply(int arr[], int n){   // Complete the function   int sum1=0,sum2=0;   for(int i=0;i<n/2;i++)       sum1=sum1+arr[i];   for(int j=n/2;j<n;j++)       sum2=sum2+arr[j];   return sum1*sum2;}
0
hasnainraza1998hr1 month ago
C++, 0.0
int multiply(int arr[], int n){   int leftSum=0,rightSum=0;   for(int i=0;i<n/2;i++){       leftSum += arr[i];       rightSum += arr[i+n/2];   }   if(n%2!=0)       rightSum += arr[n-1];   return leftSum*rightSum;}
0
atif836141 month ago
 java Solution arrays index based question :
 public static int multiply (int arr[], int n) {       if(arr.length==0){           return 0;       }       int Lsum = 0; int Rsum = 0; int mid = (0 + arr.length) / 2; // for left sub array sum: for (int i = 0; i < mid; i++) {  Lsum = Lsum + arr[i]; }  // for right sub array sum: for (int i = mid; i < arr.length; i++) {  Rsum = Rsum + arr[i]; } int Fmultiply = Lsum * Rsum; return Fmultiply;         }   
0
aakasshuit2 months ago
//Java Solution

        int sum=0;
        int add=0;
        int ans=1;
        for(int i=0;i<n/2;i++){
            sum+=arr[i];
        }
        for(int i=(n/2);i<n;i++){
            add+=arr[i];
        }
        ans=sum*add;
        return ans;
0
vikkrammor2 months ago
// java solution using both while and for loop

public static int multiply (int arr[], int n) {
        int first = 0;
        int second = 0;
        int prod = 0;
        int i = 0;
        int j = n-1;
        if(n%2 == 0){
            while(i<j){
                first += arr[i];
                second += arr[j];
                i++;
                j--;
            }
        }
        else if(n%2 == 1){
            for(i=0 ;i<n/2; i++){
               first += arr[i];
           }
            for(j=n/2 ; j<n; j++){
               second += arr[j];
           }  
        }
        return prod = first*second;
    }
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.