Interview Question

Quality Assurance Engineer Interview

-Seattle, WA

Amazon

Given an array with numbers 1-5, how would you subtract one number from every EVEN number in each index?

AnswerAdd Tags

Interview Answers

8 Answers

3

public class Animal { public static void main(String[] args) { int[] nums={3,4,5,6,7,8}; System.out.println(Arrays.toString(substract(nums,6))); } static int[] substract(int[] nums, int n){ //int[] nums2=nums; for (int i=0; i

Anonymous on

0

for(int i=0;i

Anonymous on

1

public class am2 { public static void main(String[] args) { int[] a = {1,2,3,4,5}; int sub = 0; for (int i = 0; i < a.length; i++) { if(i%2==0 && i!=0) { sub = a[i]-1; a[i] = sub; } } for (int A:a) { System.out.println(A); } }

Anonymous on

0

private static ArrayList subtractOneNumberinEachIndex(int[] inputArrayValue) { ArrayList outputArrayValue = new ArrayList(); ArrayList evenNumbers = new ArrayList(); ArrayList oddNumbers = new ArrayList(); for (int i = 0; i < inputArrayValue.length - 1; i++) { if (inputArrayValue[i] % 2 == 0) { evenNumbers.add(inputArrayValue[i] - 1); } else { oddNumbers.add(inputArrayValue[i]); } } outputArrayValue.addAll(oddNumbers); outputArrayValue.addAll(evenNumbers); return outputArrayValue; }

KD on

0

import java.util.*; public class ArraySubtract { // Given an array with numbers 1-5, how would you subtract one number from every // EVEN number in each index public static void main(String[] args) { int arr[] = { 2, 4, 5, 7, 10 }; List output = new ArrayList(); for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { output.add(arr[i] - 1); } else { output.add(arr[i]); } } System.out.println("Output Array : " + output.toString()); } } OUTPUT: Output Array : [1, 3, 5, 7, 9]

Question is simple - Make ODD NUMBERS ARRAY. So for that subtract "1" from all even numbers. on

0

public class Array1 { /* * Given an array with numbers 1-5, how would you subtract one number from each * index? */ public static void main(String[] args) { int[] arr1 = { 1, 2, 3, 4, 5 }; Array1.substractNum(arr1); } private static void substractNum(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] - 1 + " "); } } }

Jwala Kumar on

0

def subtractAteven(dataset): for i in range(len(dataset)): if dataset[i]%2==0: dataset[i]-=1 return dataset

Anonymous on

1

public class Animal { public static void main(String[] args) { int[] nums={3,4,5,6,7,8}; System.out.println(Arrays.toString(substract(nums,6))); } static int[] substract(int[] nums, int n){ for (int i=0; i

Anonymous on

Add Answers or Comments

To comment on this, Sign In or Sign Up.