How to add series of numbers in Java

Shravan Kumar :

When the summing series is implemented, there is a change in the desired output.

The problem is:

Lily has a chocolate bar that she wants to share it with Ron for his birthday. Each of the squares has an integer on it. She decides to share a contiguous segment of the bar selected such that the length of the segment matches Ron's birth month and the sum of the integers on the squares is equal to his birth day. You must determine how many ways she can divide the chocolate.

Consider the chocolate bar as an array of squares, s=2,2,1,3,2. She wants to find segments summing to Ron's birth day, d=4 with a length equalling his birth month, m=2 . In this case, there are two segments meeting her criteria: 2,2 and 3,1.

Function Description

Complete the birthday function in the editor below. It should return an integer denoting the number of ways Lily can divide the chocolate bar.

Birthday has the following parameter(s):

s: an array of integers, the numbers on each of the squares of chocolate
d: an integer, Ron's birth day
m: an integer, Ron's birth month

Input Format

The first line contains an integer n, the number of squares in the chocolate bar. The second line contains space-separated integers s[I], the numbers on the chocolate squares where. The third line contains two space-separated integers, m and d, Ron's birth day and his birth month.

Output Format

Print an integer denoting the total number of ways that Lily can portion her chocolate bar to share with Ron.

Sample Input 0

5
1 2 1 3 2
3 2

Sample Output 0

2

And my code in java

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;

import java.util.Scanner;
public class Solution {

public static void main(String args[])
{
  int a[] = new int[1000];
  Scanner s=new Scanner(System.in);
  int sum=0,count=0;
  int n=s.nextInt();
  for(int i=0;i<n;i++)
  {
    a[i]=s.nextInt();
  }
  int m=s.nextInt();
  int d=s.nextInt();
  for(int i=0;i<n;i++){ 
    for(int j=i;j<d+i;j++)
    {
      sum=sum+a[j];
      if(sum==m)
      {
        count++;

      }
    }sum=0; 
  }
  System.out.println(count);
}}

I get output as 3 instead of 2. How to sum for till the d terms?

mettleap :

In your algorithm (shown below), you are checking the sum before the inner loop has ended:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;

import java.util.Scanner;
public class Solution {

public static void main(String args[])
{
  int a[] = new int[1000];
  Scanner s=new Scanner(System.in);
  int sum=0,count=0;
  int n=s.nextInt();
  for(int i=0;i<n;i++)
  {
    a[i]=s.nextInt();
  }
  int m=s.nextInt();
  int d=s.nextInt();
  for(int i=0;i<n;i++){ 
    for(int j=i;j<d+i;j++)
    {
      sum=sum+a[j];
      //THIS CHECK SHOULD BE OUTSIDE THE CURRENT FOR LOOP
      if(sum==m)
      {
        count++;

      }
    }sum=0; 
  }
  System.out.println(count);
}}

You should be doing it like so,

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;

import java.util.Scanner;
public class Solution {

public static void main(String args[])
{
  int a[] = new int[1000];
  Scanner s=new Scanner(System.in);
  int sum=0,count=0;
  int n=s.nextInt();
  for(int i=0;i<n;i++)
  {
    a[i]=s.nextInt();
  }
  int m=s.nextInt();
  int d=s.nextInt();
  for(int i=0;i<n;i++){ 
    for(int j=i;j<d+i;j++)
    {
      sum=sum+a[j];
    }

      if(sum==m)
      {
        count++;

      }
    sum=0; 
  }
  System.out.println(count);
}}

However, even the solution above is missing an edge case. You are initializing your array named a as an array of length 1000 which will introduce bugs in your code. For example consider the following input

5
1 2 1 3 2
2 2

now since your array a looks something like the following:

1 2 1 3 2 0 0 0 0 ... //till length 1000

your algorithm will give the following answer as valid answer. when in reality there are none:

1 2 1 3 [2 0] 0 0 0 ... //till length 1000

So your outer for loop should only go till the (n-d)th element

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107294&siteId=1