Search code examples
javaarrayssumlimit

Java - Summing up values of Array except a specific section


I got the task to sum up all values of an array except a section that starts with a 6 until the next 7 appears. Values after the 7 shall again be added to my sum.

Here's one of my solutions:

        if(nums == null){
            return 0;
        }else if(nums.length == 0){
            return 0;
        }else{
            int sum = 0;
            int countTill7 = 0;
            boolean six = false;

            for(int i = 0; i < nums.length; i++){
                if(nums[i] != 6){
                    sum += nums[i];
                }else if(nums[i] == 6){
                    six = true;
                    countTill7++;
                }else if(six == true && nums[i - 1] == 7){
                    six = false;
                    sum += nums[i];
                }

            }

            return sum;
        }

I can't find the problem..


Solution

  • Here's how I sumed up all values of an array except a section that starts with a 6 until the next 7 appears

    package codingbat.array3;
    
    public class Sum67
    {
        public static void main(String[] args) 
        {
        }
    
        /**
         * Return the sum of the numbers in the array, 
         * except ignore sections of numbers starting with a 6 and
         * extending to the next 7 
         * (every 6 will be followed by at least one 7). 
         * Return 0 for no numbers.
         *
         * sum67({1, 2, 2}) → 5
         * sum67({1, 2, 2, 6, 99, 99, 7}) → 5
         * sum67({1, 1, 6, 7, 2}) → 4
         */
        public int sum67(int[] nums) 
        {
            int sum = 0;
            boolean got6 = false;
    
            for (int i = 0; i < nums.length; i++)
            {
                if (6 == nums[i])
                {
                    got6 = true;
                }
                else if (got6 && 7 == nums[i])
                {
                    got6 = false;
                }
                else if (!got6)
                {
                    sum += nums[i];        
                }
            }
    
            return sum;
        }
    }