Modify the array
Description
You are given an array A, consisting of N integers.
You have to modify the array, such that, the values stored at even indexes are incremented by one, and the values stored at odd indexes are incremented by 2.
Input
The first line of the input contains T, the number of test cases.
The first line of each test case, contains N the size of the array.
The next line contains N space separated integers, denoting the elements of the array.
Constraints
1 <= T <= 10
1 <= N <= 100
1 <= A[i] <= 100
Output
For each test case, modify the array according to the conditions given in the problem statement, and then print on a single line, on a new line.
Hint
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
for (int i =0;i<times;i++){
int lodd = 0;
int leven = 101;
int length = sc.nextInt();
int []array = new int[length];
int []array2 = new int[length];
for (int j =0;j<length;j++){
array[j]=sc.nextInt();
}
for (int k =0;k<length;k++){
if(k%2==0){
array2[k]=array[k]+1;
}else {
array2[k]=array[k]+2;
}
}
for (int f =0;f<length;f++){
System.out.print(array2[f]+" ");
}
System.out.println();
}
}
}
No comments:
Post a Comment