Java > Array-2 > sum67 (CodingBat Solution)

Problem:

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


Solution:

public int sum67(int[] nums) {
  int sum = 0;
  boolean stop = false;
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 6)
      stop = true;
    if (stop == false)
      sum += nums[i];
    if (nums[i] == 7 && stop == true)
      stop = false;
  }
  return sum;
}


8 comments :

  1. public int sum67(int[] nums) {

    int sum = 0;

    for (int i = 0; i < nums.length; i++) {

    if(nums[i] != 6) sum = sum + nums[i];
    else while(nums[i] != 7) i++;

    }
    return sum;
    }

    ReplyDelete
  2. int sum = 0;
    for(int i = 0;i<nums.length;i++){
    if(nums[i]==6){
    for(int j = i;j<nums.length;j++){
    if(nums[j]==7){
    i = j;
    break;
    }
    }
    continue;
    }
    sum+=nums[i];
    }
    return sum;

    ReplyDelete
  3. public int sum67(int[] nums) {
    if(nums.length == 0) return 0;

    int sum = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 6) {
    i++;
    while(nums[i] != 7 && i < nums.length) i++;
    } else {
    sum += nums[i];
    }
    }
    return sum;
    }

    ReplyDelete
  4. public int sum67(int[] nums) {

    int sum =0;
    int i, j;
    if(nums.length>0){
    for( i=0; i<nums.length;i++){
    if(nums[i]==6){
    for( j=i; nums[j]!=7;j++){
    i=i+1;
    }
    }

    else{
    sum = sum+nums[i];
    }
    }
    }

    return sum;
    }

    ReplyDelete
  5. public int sum67(int[] nums) {
    int sum = 0;

    for (int i = 0; i <= nums.length-1; i++) {

    if(nums[i] != 6)
    sum = sum + nums[i];

    else

    while(nums[i] != 7) i++;

    }
    return sum;
    }

    ReplyDelete
  6. int sum=0;
    for(int i=0;i<nums.length; i++){
    if(nums[i]==6){
    while(nums[i]!=7)
    i++;
    continue;

    }else{
    sum+=nums[i];

    }
    }return sum;

    ReplyDelete
  7. int sum = 0;

    for (int i = 0; i < nums.length; i++){

    if (nums[i]==6){
    while (nums[i] != 7){
    i++;
    }
    sum-=7;
    }

    sum+= nums[i];

    } return sum;

    ReplyDelete
  8. public int sum67(int[] nums) {
    int sum = 0;
    boolean appear = true;
    for(int i=0; i<nums.length; i++) {
    if(nums[i]!=6 && appear==true) {
    sum += nums[i];
    } else if(nums[i]==6) {
    appear = false;
    } else if(nums[i-1]==7 && i<nums.length) {
    appear = true;
    sum += nums[i];
    }
    }
    return sum;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact