6 kyu
Simple Fun #110: Array Operations
174 of 360myjinxin2015
Description:
Task
You are given an array of integers a
and a non-negative number of operations k
, applied to the array. Each operation consists of two parts:
Find the maximum element value of the array;
Replace each element a[i] with (maximum element value - a[i]).
How will the array look like after k
such operations?
Example
For a = [-4, 0, -1, 0]
and k = 2
, the output should be [0, 4, 3, 4]
.
Initial array: [-4, 0, -1, 0]
1st operation:
Find the maximum value --> 0
Replace each element: --> [(0 - -4), (0 - 0), (0 - -1), (0 - 0)]
--> [4, 0, 1, 0]
2nd operation:
Find the maximum value --> 4
Replace each element: --> [(4 - 4), (4 - 0), (4 - 1), (4 - 0)]
--> [0, 4, 3, 4]
For a = [0, -1, 0, 0, -1, -1, -1, -1, 1, -1]
and k = 1
,
the output should be [1, 2, 1, 1, 2, 2, 2, 2, 0, 2]
.
Initial array: [0, -1, 0, 0, -1, -1, -1, -1, 1, -1]
1st operation:
Find the maximum value --> 1
Replace each element:
--> [(1-0),(1- -1),(1-0),(1-0),(1- -1),(1- -1),(1- -1),(1- -1),(1-1),(1- -1)]
--> [1, 2, 1, 1, 2, 2, 2, 2, 0, 2]
Input/Output
[input]
integer array aThe initial array.
Constraints:
1 <= a.length <= 100
-100 <= a[i] <= 100
[input]
integerk
non-negative number of operations.
Constraints:
0 <= k <= 100000
[output] an integer array
The array after
k
operations.
Puzzles
Similar Kata:
Stats:
Created | Feb 8, 2017 |
Published | Feb 8, 2017 |
Warriors Trained | 887 |
Total Skips | 12 |
Total Code Submissions | 1463 |
Total Times Completed | 360 |
JavaScript Completions | 174 |
C# Completions | 58 |
Python Completions | 140 |
Ruby Completions | 24 |
Total Stars | 11 |
% of votes with a positive feedback rating | 95% of 116 |
Total "Very Satisfied" Votes | 106 |
Total "Somewhat Satisfied" Votes | 9 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 10 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |