Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmArray sum after dividing numbers from previous

Array sum after dividing numbers from previous

Find the sum of number of series after dividing the element of array from previous element.

Examples: 

Input : 3 7 9 10 12 18
Explanation:  3 + 7/3 + 9/7 + 10/9 + 
12/10 + 18/12 = 9 (taking only integer 
part)   
Output : 9

Input : 1 12 24 30 60
Output : 18 

Approach: We take elements in an array and divide the element from previous element. We do this process for all the elements of an array except very first element. Add the result after division and very first element.

Note: If any element is zero in an array then it fails to do the task and return minus one.  

Implementation:

C++




// C++  program for divide and
// sum the number of series
#include <bits/stdc++.h>
using namespace std;
 
int divideAndSum(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
         
        // checking whether element
        // is zero or not
        if (arr[i] == 0)
            return -1;
         
        if (i == 0)
            sum += arr[i];
        else
 
            // divide element from
            // previous element
            sum += arr[i] / arr[i - 1];
    }
     
    return sum;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 7, 9, 10, 12, 18 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << divideAndSum(arr, n);
    return 0;
}


Java




// java program for divide and
// sum the number of series
import java.io.*;
 
class GFG
{
     
    static int divideAndSum(int arr[], int n)
    {
        int sum = 0;
        for (int i = 0; i < n; i++) {
             
            // checking whether element
            // is zero or not
            if (arr[i] == 0)
                return -1;
             
            if (i == 0)
                sum += arr[i];
            else
     
                // divide element from
                // previous element
                sum += arr[i] / arr[i - 1];
        }
         
        return sum;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 3, 7, 9, 10, 12, 18 };
        int n = arr.length;
        System.out.println( divideAndSum(arr, n));
 
    }
}
 
// This code is contributed by vt_m


Python3




# Python 3 program for divide and
# sum the number of series
 
def divideAndSum(arr, n):
  
    sum = 0
    for i in range(0,n): 
         
         # checking whether element
         # is zero or not
        if (arr[i] == 0):
            return -1
         
        if (i == 0):
            sum += arr[i]
        else:
 
                     # divide element from
             # previous element
             sum += int(arr[i] / arr[i - 1])
      
     
    return int(sum)
  
 
# Driver code
arr =   [3, 7, 9, 10, 12, 18]  
 
n = len(arr)
print(divideAndSum(arr, n))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# program for divide and
// sum the number of series
using System;
 
class GFG
{
     
    static int divideAndSum(int []arr, int n)
    {
        int sum = 0;
        for (int i = 0; i < n; i++) {
             
            // checking whether element
            // is zero or not
            if (arr[i] == 0)
                return -1;
             
            if (i == 0)
                sum += arr[i];
            else
     
                // divide element from
                // previous element
                sum += arr[i] / arr[i - 1];
        }
         
        return sum;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr = { 3, 7, 9, 10, 12, 18 };
        int n = arr.Length;
        Console.WriteLine( divideAndSum(arr, n));
 
    }
}
 
// This code is contributed by vt_m


PHP




<?php
// php program for divide and
// sum the number of series
 
function divideAndSum($arr, $n)
{
    $sum = 0;
    for ($i = 0; $i < $n; $i++) {
         
        // checking whether element
        // is zero or not
        if ($arr[$i] == 0)
            return -1;
         
        if ($i == 0)
            $sum += $arr[$i];
        else
 
            // divide element from
            // previous element
            $sum += floor($arr[$i] /
                    $arr[$i - 1]);
    }
     
    return $sum;
}
 
// Driver code
{
    $arr = array(3, 7, 9, 10, 12, 18);
    $n = sizeof($arr) / sizeof($arr[0]);
    echo divideAndSum($arr, $n);
    return 0;
}
 
// This code is contributed by nitin mittal.
?>


Javascript




<script>
// javascript program for divide and
// sum the number of series
 
     
    function divideAndSum(arr , n)
    {
        var sum = 0;
        for (i = 0; i < n; i++) {
             
            // checking whether element
            // is zero or not
            if (arr[i] == 0)
                return -1;
             
            if (i == 0)
                sum += arr[i];
            else
     
                // divide element from
                // previous element
                sum += parseInt(arr[i] / arr[i - 1]);
        }
         
        return sum;
    }
     
    // Driver code
      
        var arr = [ 3, 7, 9, 10, 12, 18 ];
        var n = arr.length;
        document.write( divideAndSum(arr, n));
 
 
// This code contributed by umadevi9616
</script>


Output

9

Time Complexity: O(N )
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments