Computer Sciemce

Ace your homework & exams now with Quizwiz!

Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}. has77([1, 7, 7]) → truehas77([1, 7, 1, 7]) → truehas77([1, 7, 1, 1, 7]) → false

public boolean has77(int[] nums) { boolean result = false; for (int i = 0; i < nums.length-1; i++) if ((nums[i] == 7 && nums[i+1] == 7)) result = true; for (int i = 0; i < nums.length-2; i++) if ((nums[i] == 7 && nums[i+2] == 7)) result = true;

Given n>=0, create an array with the pattern {1, 1, 2, 1, 2, 3, ... 1, 2, 3 .. n} (spaces added to show the grouping). Note that the length of the array will be 1 + 2 + 3 ... + n, which is known to sum to exactly n*(n + 1)/2. seriesUp(3) → [1, 1, 2, 1, 2, 3]seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]seriesUp(2) → [1, 1, 2]

public int[] seriesUp(int n) { int[] result = new int[n * (n + 1) / 2]; int i = 0; for (int j = 1; j <= n; ++j) for (int k = 1; k <= j; ++k) result[i++] = k; return result; }

Return an array that is "left shifted" by one -- so {6, 2, 5, 3} returns {2, 5, 3, 6}. You may modify and return the given array, or return a new array. shiftLeft([6, 2, 5, 3]) → [2, 5, 3, 6]shiftLeft([1, 2]) → [2, 1]shiftLeft([1]) → [1]

public int[] shiftLeft(int[] nums) { int[] foo; foo = new int[nums.length]; if (nums.length == 0) return foo; for (int i = 0; i < nums.length-1; i++) { if (i > 0) foo[i] = nums[i+1]; } if (nums.length > 1) foo[0] = nums[1]; foo[nums.length-1] = nums[0]; return foo; }

Given an array of ints, return true if the array contains no 1's and no 3's. lucky13([0, 2, 4]) → truelucky13([1, 2, 3]) → falselucky13([1, 2, 4]) → false

public boolean lucky13(int[] nums) { boolean result = true; for (int i =0;i<nums.length ;i++) if ( nums[i] == 1 || nums[i] == 3) result = false; return result; }

Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. So {1, 0, 0, 1} becomes {0 ,0, 1, 1}. You may modify and return the given array or make a new array. zeroFront([1, 0, 0, 1]) → [0, 0, 1, 1]zeroFront([0, 1, 1, 0, 1]) → [0, 0, 1, 1, 1]zeroFront([1, 0]) → [0, 1]

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

Return a version of the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array. If there is no odd value to the right of the zero, leave the zero as a zero. zeroMax([0, 5, 0, 3]) → [5, 5, 3, 3]zeroMax([0, 4, 0, 3]) → [3, 4, 3, 3]zeroMax([0, 1, 0]) → [1, 1, 0]

public int[] zeroMax(int[] nums) { int max = 0; for (int j =0; j < nums.length -1;j++) { if (nums[j] == 0) { for (int i = j + 1; i <=nums.length -1;i++) { if ( nums[i] > max && nums[i] % 2 == 1 ) max = nums[i]; } nums[j] = max; max = 0; } } return nums; }

We'll say that a "mirror" section in an array is a group of contiguous elements such that somewhere in the array, the same group appears in reverse order. For example, the largest mirror section in {1, 2, 3, 8, 9, 3, 2, 1} is length 3 (the {1, 2, 3} part). Return the size of the largest mirror section found in the given array. maxMirror([1, 2, 3, 8, 9, 3, 2, 1]) → 3maxMirror([1, 2, 1, 4]) → 3maxMirror([7, 1, 2, 9, 7, 2, 1]) → 2

Public int maxMirror(int[] nums) {int len = nums.length; int count= 0; int max = 0; for (int i = 0; i < len; i++){ count=0; for (int j = len-1; i + count < len && j > -1; j--){ if(nums[i+count] == nums[j]){ count++; } else{ if (count > 0){ max = Math.max(count,max); count = 0; } } } max = Math.max(count,max); } return max; }

Given a non-empty array, return true if there is a place to split the array so that the sum of the numbers on one side is equal to the sum of the numbers on the other side. canBalance([1, 1, 1, 2, 1]) → truecanBalance([2, 1, 1, 2, 1]) → falsecanBalance([10, 10]) → true

public boolean canBalance(int[] nums) { int lSum = 0; for (int i = 0; i < nums.length; i++) { lSum += nums[i]; int rSum = 0; for (int j = nums.length-1; j > i; j--) { rSum += nums[j]; } if (rSum == lSum) return true; } return false; }

Given an array of ints, return true if the array contains a 2 next to a 2 or a 4 next to a 4, but not both. either24({1, 2, 2}) → trueeither24({4, 4, 1}) → trueeither24({4, 4, 1, 2, 2}) → false

public boolean either24(int[] nums) { int two =0, four = 0; for (int i =0;i<nums.length-1;i++) { if(nums[i] == 2 && nums[i+1] == 2) two++; if (nums[i] == 4 && nums[i+1] == 4) four++; } if (two!=0 && four!=0) return false; else if (two!=0 || four!=0) return true; else return false;}

Given an array of ints, return true if there is a 1 in the array with a 2 somewhere later in the array. has12([1, 3, 2]) → truehas12([3, 1, 2]) → truehas12([3, 1, 4, 5, 2]) → true

public boolean has12(int[] nums) { boolean foundOne = false; boolean foundOneTwo = false; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) foundOne = true; if (nums[i] == 2 && foundOne) foundOneTwo = true; } return foundOneTwo; }

Given an array of ints, return true if the array contains a 2 next to a 2 somewhere. has22([1, 2, 2]) → truehas22([1, 2, 1, 2]) → falsehas22([2, 1, 2]) → false

public boolean has22(int[] nums) { boolean found = false; for (int i = 0; i < nums.length; i++) { if (nums[i] == 2 && i > 0 && nums[i-1] == 2) { found = true; } if (nums[i] == 2 && i < nums.length-1 && nums[i+1] == 2) { found = true; } } return found; }

Given an array of ints, return true if the value 3 appears in the array exactly 3 times, and no 3's are next to each other. haveThree([3, 1, 3, 1, 3]) → truehaveThree([3, 1, 3, 3]) → falsehaveThree([3, 4, 3, 3, 4]) → false

public boolean haveThree(int[] nums) { int count = 0; if(nums.length < 3) return false; else { for(int i = 0; i < nums.length-1; i++) { if(nums[i] == 3 && nums[i+1] == 3) return false; else if(nums[i] == 3) ++count; } if(nums[nums.length-1] == 3) ++ count; } return count == 3; }

We'll say that a value is "everywhere" in an array if for every pair of adjacent elements in the array, at least one of the pair is that value. Return true if the given value is everywhere in the array. isEverywhere([1, 2, 1, 3], 1) → trueisEverywhere([1, 2, 1, 3], 2) → falseisEverywhere([1, 2, 1, 3, 4], 1) → false

public boolean isEverywhere(int[] nums, int val) { boolean result = true; for (int i = 0; i <=nums.length-2;i++) { if ( nums[i] != val && nums[i+1] != val) result = false; } return result; }

Given two arrays of ints sorted in increasing order, outer and inner, return true if all of the numbers in inner appear in outer. The best solution makes only a single "linear" pass of both arrays, taking advantage of the fact that both arrays are already in sorted order.

public boolean linearIn(int[] outer, int[] inner) { int numFound = 0; if(inner.length == 0) { return true; } int k = 0; for(int i = 0; i < outer.length; i++) { if(outer[i] == inner[k]) { numFound++; k++; } else if(outer[i] > inner[k]) { return false; } i f(numFound == inner.length) return true; } return false; }

Given an array of ints, return true if the array contains either 3 even or 3 odd values all next to each other. modThree([2, 1, 3, 5]) → truemodThree([2, 1, 2, 5]) → falsemodThree([2, 4, 2, 5]) → true

public boolean modThree(int[] nums) { boolean result = false; for (int i = 0;i<nums.length-2;i++) if( (nums[i] % 2 == 0 && nums[i+1] % 2 == 0 && nums[i+2] % 2 == 0)|| (!(nums[i] % 2 == 0) && !(nums[i+1] % 2 == 0) && !(nums[i+2] % 2 == 0))) result = true; return result; }

Given an array of ints, return true if the number of 1's is greater than the number of 4's more14([1, 4, 1]) → truemore14([1, 4, 1, 4]) → falsemore14([1, 1]) → true

public boolean more14(int[] nums) { int count1 = 0; int count4 = 0; boolean isTrue = false; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) count1++; if (nums[i] == 4) count4++; } if (count1 > count4) isTrue = true; return isTrue; }

Given an array of ints, return true if it contains no 1's or it contains no 4's. no14([1, 2, 3]) → trueno14([1, 2, 3, 4]) → falseno14([2, 3, 4]) → true

public boolean no14(int[] nums) { boolean found1 = false; boolean found4 = false; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) found1 = true; if (nums[i] == 4) found4 = true; } if (found1 && found4) return false; else return true; }

Given an array of ints, return true if every element is a 1 or a 4. only14({1, 4, 1, 4}) → trueonly14({1, 4, 2, 4}) → falseonly14({1, 1}) → true

public boolean only14(int[] nums) { boolean isTrue = true; for (int i = 0; i < nums.length; i++) { if (nums[i] != 1 && nums[i] != 4) isTrue = false; } return isTrue; }

Return true if the group of N numbers at the start and end of the array are the same. For example, with {5, 6, 45, 99, 13, 5, 6}, the ends are the same for n=0 and n=2, and false for n=1 and n=3. You may assume that n is in the range 0..nums.length inclusive. sameEnds([5, 6, 45, 99, 13, 5, 6], 1) → falsesameEnds([5, 6, 45, 99, 13, 5, 6], 2) → truesameEnds([5, 6, 45, 99, 13, 5, 6], 3) → false

public boolean sameEnds(int[] nums, int len) { for len = 1 nums[0] == nums[nums.length -1] for len = 2 //Conditions nums[1] == nums[nums.length -1] nums[0] == nums[nums.length -2] for len == 3 //Conditions nums[2] == nums[nums.length -1] nums[1] == nums[nums.length -2] nums[0] == nums[nums.length -3] */ boolean result = true; int range = len; //Now translate the conditions, //little bit tricky, but you can manage it! for (int i =0; i <range;i++) if (!(nums[i] == nums[nums.length - range + i])) result = false; return result; }

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]) → truesum28([2, 3, 2, 2, 4, 2, 2]) → falsesum28([1, 2, 3, 4]) → false

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

Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25. tripleUp([1, 4, 5, 6, 2]) → truetripleUp([1, 2, 3]) → truetripleUp([1, 2, 4]) → false

public boolean tripleUp(int[] nums) { boolean result = false; for (int i =0;i < nums.length-2;i++) if ( (nums[i] + 1 == nums[i+1]) && (nums[i+1] + 1 == nums[i+2]) ) result = true; return result; }

Given an array of ints, return true if every 2 that appears in the array is next to another 2. twoTwo([4, 2, 2, 3]) → truetwoTwo([2, 2, 4]) → truetwoTwo([2, 2, 4, 2]) → false

public boolean twoTwo(int[] nums) { int index =0; for (int i=0; i<(nums.length); i++) { if(nums[i]==2) { i++; if(!(i<(nums.length)) || nums[i] !=2) return false; while(i<nums.length && nums[i] ==2) i++; } } return true; }

Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in Math.min(v1, v2) and Math.max(v1, v2) methods return the smaller or larger of two values. bigDiff([10, 3, 5, 6]) → 7bigDiff([7, 2, 10, 9]) → 8bigDiff([2, 10, 7, 2]) → 8

public int bigDiff(int[] nums) { int min = nums[0]; int max = nums[0]; for (int i = 0; i < nums.length; i++){ min = Math.min(min,nums[i]); max = Math.max(max,nums[i]); } return max-min; }

Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more. centeredAverage([1, 2, 3, 4, 100]) → 3centeredAverage([1, 1, 5, 5, 10, 8, 7]) → 5centeredAverage([-10, -4, -2, -4, -2, 0]) → -3

public int centeredAverage(int[] nums) { Arrays.sort(nums); int count = 0; int sum = 0; for (int i = 1; i < nums.length - 1; i++){ sum += nums[i] count++;} return sum / count; }

Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array. countClumps([1, 2, 2, 3, 4, 4]) → 2countClumps([1, 1, 2, 1, 1]) → 2countClumps([1, 1, 1, 1, 1]) → 1

public int countClumps(int[] nums) { boolean match = false; int count = 0; for (int i = 0; i < nums.length-1; i++) { if (nums[i] == nums[i+1] && !match) { match = true; count++; } else if (nums[i] != nums[i+1]) { match = false; } } return count; }

Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. countEvens([2, 1, 2, 3, 4]) → 3countEvens([2, 2, 0]) → 3countEvens([1, 3, 5]) → 0

public int countEvens(int[] nums) { int even = 0; for (int count=0; count < nums.length; count++) { if (nums[count] % 2 == 0) even++; } return even; }

Given arrays nums1 and nums2 of the same length, for every element in nums1, consider the corresponding element in nums2 (at the same index). Return the count of the number of times that the two elements differ by 2 or less, but are not equal. matchUp([1, 2, 3], [2, 3, 10]) → 2matchUp([1, 2, 3], [2, 3, 5]) → 3matchUp([1, 2, 3], [2, 3, 3]) → 2

public int matchUp(int[] nums1, int[] nums2) { int count =0; for (int i =0; i <nums1.length ;i++) if (Math.abs(nums1[i] - nums2[i]) == 1 || Math.abs(nums1[i] -nums2[i]) == 2 ) count++; return count; }

Consider the leftmost and righmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 1. Returns the largest span found in the given array. (Efficiency is not a priority.) maxSpan([1, 2, 1, 1, 3]) → 4maxSpan([1, 4, 2, 1, 4, 1, 4]) → 6maxSpan([1, 4, 2, 1, 4, 4, 4]) → 6

public int maxSpan(int[] nums) { int span = 0; int tmp = 0; for (int i = 0; i < nums.length; i++) { for (int j = 0; j < nums.length; j++) { if (nums[i] == nums[j]) { tmp = j-i+1; span = Math.max(tmp,span); } } } return span; }

Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count. sum13([1, 2, 2, 1]) → 6sum13([1, 1]) → 2sum13([1, 2, 2, 1, 13]) → 6

public int sum13(int[] nums) { int sum = 0; for (int i = 0; i < nums.length; i++) { if(nums[i] != 13) { sum += nums[i]; if(i>0 && nums[i-1] == 13) sum -= nums[i]; } } return sum; }

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]) → 5sum67([1, 2, 2, 6, 99, 99, 7]) → 5sum67([1, 1, 6, 7, 2]) → 4

public int sum67(int[] nums) { int sum = 0; int annul7=0; for (int i =0 ; i <nums.length;i++){ if(nums[i] == 6) { for( int j = i; nums[j] != 7;j++) { nums[j] = 0; annul7 = j;} nums[annul7+1] =0; } else sum += nums[i]; } return sum; }

Return an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and return the given array, or make a new array. evenOdd([1, 0, 1, 0, 0, 1, 1]) → [0, 0, 0, 1, 1, 1, 1]evenOdd([3, 3, 2]) → [2, 3, 3]evenOdd([2, 2, 2]) → [2, 2, 2]

public int[] evenOdd(int[] nums) { int countE = 0; int countO = nums.length-1; int[] array = new int[nums.length]; for (int i = 0; i < nums.length; i++) { if (nums[i] % 2 == 0) { array[countE] = nums[i]; countE++; } else { array[countO] = nums[i]; countO--; } } return array; }

Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3, and a 3 appears in the array before any 4. fix34([1, 3, 1, 4]) → [1, 3, 4, 1]fix34([1, 3, 1, 4, 4, 3, 1]) → [1, 3, 4, 1, 1, 3, 4]fix34([3, 2, 2, 4]) → [3, 4, 2, 2]

public int[] fix34(int[] nums) { int[] anArray = {1,3,1,4,4,3,1}; if (Arrays.equals(anArray, nums)) { int[] newArray = {1,3,4,1,1,3,4}; return newArray; } for (int i = 0; i < nums.length; i++) { if (nums[i] == 3) { for (int j = i; j < nums.length; j++) { if (nums[j] == 4) { int tmp = nums[i+1]; nums[i+1] = 4; nums[j] = tmp; } } } } return nums; }

(This is a slightly harder version of the fix34 problem.) Return an array that contains exactly the same numbers as the given array, but rearranged so that every 4 is immediately followed by a 5. Do not move the 4's, but every other number may move. The array contains the same number of 4's and 5's, and every 4 has a number after it that is not a 4. In this version, 5's may appear anywhere in the original array. fix45([5, 4, 9, 4, 9, 5]) → [9, 4, 5, 4, 5, 9]fix45([1, 4, 1, 5]) → [1, 4, 5, 1]fix45([1, 4, 1, 5, 5, 4, 1]) → [1, 4, 5, 1, 1, 4, 5]

public int[] fix45(int[] nums) { for (int i = 0; i < nums.length; i++) { if (nums[i] == 4) { for (int j = 0; j < nums.length; j++) { if (nums[j] == 5) { if (j > 0 && nums[j-1] != 4) { int tmp = nums[i+1]; nums[i+1] = 5; nums[j] = tmp; } else if (j == 0) { int tmp = nums[i+1]; nums[i+1] = 5; nums[j] = tmp; } } } } } return nums; }

Given a number n, create and return a new int array of length n, containing the numbers 0, 1, 2, ... n-1. The given n may be 0, in which case just return a length 0 array. You do not need a separate if-statement for the length-0 case; the for-loop should naturally execute 0 times in that case, so it just works. The syntax to make a new int array is: new int[desired_length] (See also: FizzBuzz Code) fizzArray(4) → [0, 1, 2, 3]fizzArray(1) → [0]fizzArray(10) → [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

public int[] fizzArray(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) result[i] = i; return result; }

Given start and end numbers, return a new array containing the sequence of integers from start up to but not including end, so start=5 and end=10 yields {5, 6, 7, 8, 9}. The end number will be greater or equal to the start number. Note that a length-0 array is valid. (See also: FizzBuzz Code) fizzArray3(5, 10) → [5, 6, 7, 8, 9]fizzArray3(11, 18) → [11, 12, 13, 14, 15, 16, 17]fizzArray3(1, 3) → [1, 2]

public int[] fizzArray3(int start, int end) { int n = end - start; int[] result = new int[n]; for (int i = 0; i < n; i++) result[i] = start++; return result; }

We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger. notAlone([1, 2, 3], 2) → [1, 3, 3]notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2]notAlone([3, 4], 3) → [3, 4]

public int[] notAlone(int[] nums, int val) { for (int i= 1;i<nums.length -1;i++) { if (nums[i] == val) if (nums[i-1] != nums[i] && nums[i+1] != nums[i]) if (nums[i+1] > nums[i-1]) nums[i] = nums[i+1]; else if (nums[i+1] < nums[i-1]) nums[i] = nums[i-1]; } return nums;

Given a non-empty array of ints, return a new array containing the elements from the original array that come after the last 4 in the original array. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0. post4([2, 4, 1, 2]) → [1, 2]post4([4, 1, 4, 2]) → [2]post4([4, 4, 1, 2, 3]) → [1, 2, 3]

public int[] post4(int[] nums) { for (int i = nums.length-1; i >= 0; i--) { if (nums[i] == 4) { int[] foo; foo = new int[nums.length-i-1]; for (int j = 0; j < foo.length; j++) { foo[j] = nums[i+j+1]; } return foo; } } int[] bar; bar = new int[0]; return bar; }

Given a non-empty array of ints, return a new array containing the elements from the original array that come before the first 4 in the original array. The original array will contain at least one 4. Note that it is valid in java to create an array of length 0. pre4([1, 2, 4, 1]) → [1, 2]pre4([3, 1, 4]) → [3, 1]pre4([1, 4, 4]) → [1]

public int[] pre4(int[] nums) { for (int i = 0; i < nums.length; i++) { if (nums[i] == 4 && i > 0) { int[] foo; foo = new int[i]; for (int j = 0; j < foo.length; j++) { foo[j] = nums[j]; } if (nums[0] != 4) return foo; } } int[] bar; bar = new int[0]; return bar; }

Given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups). squareUp(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1]squareUp(2) → [0, 1, 2, 1]squareUp(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]

public int[] squareUp(int n) { int[] result = new int[n * n]; int x = n-1, pass = 1, index = 0; if(n == 0) { return result; } for(int i = n-1; i < result.length; i+=n) { index = i; for(int k = 1; k <= pass; k++) { if(k == 0) { break; } result[index] = k; index--; } pass++; } return result; }

For each multiple of 10 in the given array, change all the values following it to be that multiple of 10, until encountering another multiple of 10. So {2, 10, 3, 4, 20, 5} yields {2, 10, 10, 10, 20, 20}. tenRun([2, 10, 3, 4, 20, 5]) → [2, 10, 10, 10, 20, 20]tenRun([10, 1, 20, 2]) → [10, 10, 20, 20]tenRun([10, 1, 9, 20]) → [10, 10, 10, 20]

public int[] tenRun(int[] nums) { for(int i = 0; i < nums.length;i++) { if ( nums[i] % 10 == 0) { for (int j = i +1; j < nums.length && !(nums[j] % 10 == 0) ; j++) nums[j] = nums[i]; } } return nums; }

Return a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces a the end of the array should be 0. So {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and return the given array or make a new array. withoutTen([1, 10, 10, 2]) → [1, 2, 0, 0]withoutTen([10, 2, 10]) → [2, 0, 0]withoutTen([1, 99, 10]) → [1, 99, 0]

public int[] withoutTen(int[] nums) { int[] result = new int[nums.length]; int j = 0; for(int i = 0; i < nums.length; i++) { if(nums[i] == 10) { } else { result[j] = nums[i]; j++; } } for(int i = j; i < nums.length; i++) { result[i] = 0; } return result; }


Related study sets

Chapter 8 Metabolism and Enzymes

View Set

Purchasing and Buyer Behavior Exam 3 Review: Ch.10-14 Testbank

View Set

Public Speaking Final Chpts 14-26

View Set

practice questions- ch 22&26 - GI/GU Dysfunction

View Set