+ 1

Subtracting all the elememts in an array by X digit for K number of times

I want to print two arrays like, if the array is arr[]={1,2,3,4} K=2-Number of times array must repeatedly be subtracted. X=3-Elements must be subtratacted with this digit. So array must change as, for K=1(A): arr={|1-3|,|2-3|,|3-3|,|4-3|} for K=2(B): arr:{|2-3|,|1-3|,|0-3|,|1-3|} Output: A:2,1,0,1 B:1,2,3,2. arr={3,5,6} K=3 X=2 K=1(A): {1,3,4} K=2(B): {1,1,2} K=3(A): {1,1,0} Output: A:1,1,0 B=1,1,2

5th May 2022, 7:19 AM
Codebeforechill
5 odpowiedzi
0
class Test{ public static void arrCalc(int[] arr, int k, int x){ int[] A = new int[arr.length]; int[] B = new int[arr.length]; for(int i=0;i<k;i++){ for(int j=0;j<arr.length;j++){ arr[j] = Math.abs(arr[j]-x); if(i%2==0) A[j] = arr[j]; else B[j] = arr[j]; } } System.out.print("A: "); for(int i:A) System.out.print(i+" "); System.out.println(); System.out.print("B: "); for(int i:B) System.out.print(i+" "); } public static void main(String[] args) { int[] arr = {3,5,6}; int k = 3; int x = 2; arrCalc(arr, k, x); } }
5th May 2022, 11:52 AM
Thineshan Panchalingam
Thineshan Panchalingam - avatar
5th May 2022, 8:20 AM
Oma Falk
Oma Falk - avatar
+ 4
Interesting task. Can I confirm some things first? 1. In which language, cause it's done differently by language. 2. Should the subtraction result be absolute? cause I see no negative values. 3. You said "to print two arrays ..." but if <K> was more than two, then there would be <K> arrays generated, if I understood it correctly ... P.S. It would be nice to see your tryout code first, so please attach your code link in post's Description ☝ https://www.sololearn.com/post/75089/?ref=app
5th May 2022, 7:56 AM
Ipang
+ 4
Ipang yeah 2. absolute as he wrote |a-b| 3. He expects last 2 Arrays PleaseCodeForMe? Yeeeeeah....maybe Decided to do a special approach that at least is not good for school.😁
5th May 2022, 8:32 AM
Oma Falk
Oma Falk - avatar
0
https://code.sololearn.com/c51YJSIi4kzS/?ref=app Found a more efficient than direct approach
11th May 2022, 8:52 AM
michal