Java > Array-2 > sum28 (CodingBat Solution)

Problem:

Given an array of ints, return true if the sum of all the 2's in the array is exactly 8.

sum28({2, 3, 2, 2, 4, 2}) → true
sum28({2, 3, 2, 2, 4, 2, 2}) → false
sum28({1, 2, 3, 4}) → false


Solution:

public boolean sum28(int[] nums) {
  int sum = 0;
  boolean is8 = false;
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 2)
      sum += 2;
  }
  if (sum == 8)
    is8 = true;
  return is8;
}


10 comments :

  1. public boolean sum28(int[] nums) {
    int sum = 0;
    for (int i : nums) {
    if (i==2) sum += 2;
    }
    return sum==8;
    }

    ReplyDelete
  2. public boolean sum28(int[] nums) {
    int count=0;
    for(int i=0; i<nums.length; i++){
    if(nums[i]==2){
    count++;
    }}
    if(count==4){
    return true;
    }

    return false;
    }

    ReplyDelete
  3. public boolean sum28(int[] nums) {
    int sum = 0;
    for(int i = 0; i < nums.length && sum <= 8; i++) {
    if(nums[i] == 2) sum += nums[i];
    }
    return sum == 8;
    }

    if we still left some cells in the array and sum is already grater then 8 then stop and return false

    ReplyDelete
  4. public boolean sum28(int[] nums) {
    int count = 0;
    for(int i = 0; i<nums.length; i++) {
    if(nums[i]==2) count++;
    }
    return count == 4;
    }

    ReplyDelete
  5. public boolean sum28(int[] nums) {
    int sum=0;
    for(int i=0;i<nums.length;i++){
    if(nums[i]==2){
    sum+=nums[i];
    }
    }

    return sum==8?true:false;
    }

    ReplyDelete
  6. public boolean sum28(int[] nums) {
    boolean results = false;
    int sum = 0;

    for(int i = 0; i<nums.length; i++){
    if(nums[i] == 2)
    sum += nums[i];
    }
    if(sum == 8)
    {
    results = true;
    }
    return results;
    }

    ReplyDelete
  7. public boolean sum28(int[] nums) {
    int sum=0;
    boolean counter = false;

    for(int i=0;i<nums.length;i++)
    if(nums[i]==2)
    sum+=nums[i];

    if(sum==8)
    counter=true;

    return counter;
    }

    ReplyDelete
  8. public boolean sum28(int[] nums) {
    int count = 0;

    for(int i=0; i < nums.length; i++){
    if(nums[i]==2){
    count++;
    }
    }

    if(count*2==8){
    return true;
    }

    return false;
    }

    ReplyDelete
  9. public boolean sum28(int[] nums) {
    int sum = Arrays.stream(nums)
    .filter(n -> n == 2).sum();
    if(sum == 8) return true;
    else return false;
    }

    ReplyDelete
  10. public boolean sum28(int[] nums) {
    boolean result = false;
    int count2 = 0;
    for (int i = 0; i < nums.length; i++){
    if (nums[i] == 2)
    count2++;
    }
    if (count2 == 4){
    result = true;
    }
    return result;
    }

    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