I have two arrays A & B (size of the array is 1 to 100,000) that can take values only 1,2,3,4,5,6.
Now my task is to make minimum numbers to be changed in arrays such that the sum of both the arrays is the same.
Example 2:
A=[5,4,1,2,6,6] & B=[2], we have to make A as [1,1,1,1,1,1] so we have to change A 5 times and then B=[6] once, so function should return 6.
Example 3:
A=[1,2,3,4,3,2,1] and B[6], function should return -1.
Method signature looks like this.
public int task(int[] A, int[] B) {
int diff = Math.abs(A.length - B.length);
if(diff >=6) return -1;
//
}
I am able to get answer for example 3 with simple condition. Bit not for first 2 examples.
What approach should I follow to solve this program? As we can turn each and every element in A & B and do a comparison, but that is more complex.
The alghorithm can be as following:
Prepare data part
Go through all items in array A and B and find out how many of each number (1,2,3,4,5,6) is in each array. Preferable have 2d array that stores the indexes of those numbers, so you can easily access it later.
i.e. array A=[1,1,1,4,6,4] will be translated into new 2d array as
2darr=
[]
[0,1,2]
[]
[]
[3,5]
[]
[4]
so when you i.e. want to see how many 1 are there you can see that 2darr[1].length is 3. And when you want to find out where it is i.e. the 2darr[1][0] will get you index in source array and A[0] is indeed 1
In process you can also count the sum, but even without it, the sum now can be easily found out just going through lengths of each subarray in 2darray.
Alghoritm
To find the minimum amount of changes, you will first find out which sum is smaller and which bigger. Then the best change is to start changing 1 values to 6 in smaller array or changing 6 values to 1 in bigger arrays. Then 2 to 6 in smaller array and 5 to 1 in bigger array. And then continue with other numbers.
In process you can changing the arrays based on indexes you already have and do it as long as needed to get both arrays to same sum. This is detailed alghoritm that will show you how actually both arrays will look like to satisfy your needs. Its O(n), so there is definitely no way how to make it faster as you have to go through all fields just to get the sum.
I suggest to do it so you can see the actual result. On the other hand, if you think more deeply, it can be done more easily, if you just seek the right answer - you just need to know how many times each number in each array is and then just find out how many changes are needed just by simple math. You already know, you are just changing up to all 1 to 6 in smaller array and up to all 6 to 1 in bigger array and it can be just counted easily how many of them you need to change and if it is sufficient or you change all of them and then you will continue with 5 to 1 and 2 to 6 etc.
Example
Imagine that A.length is 500 and B.length is 300. The sum of A=1000and B=700. You find out that A has 30 repetitions of number 6 and B has 20 repetitions of number 1. In A you change all those 6 to 1, therefore reducing it by 30*5=150 to total of A=850 and in B you change all those 1 to 6 and increasing the value 20*5=100 and therefore B=800. You did 50 changes in total.
Then you continue with higher number in smaller array and with lower number in bigger array. You find out that A has 100 numbers of 5. Reducing 5 to 1 decreases value by 4 for each. Right now you have only 50 value difference. 50/4=12.5, therefore you need to change 13 numbers and you are done. The answer is that minimum amount of changes is 63.
The impossibility-criteria is a simple one as you suspect, but it is different from what you guess: it depends on the length of the arrays, which determines their minimal and maximal sums. The shorter array can not produce a sum which is greater than 6 times its length (all elements are 6s) and the longer array can not produce a sum which is less than its length (all elements are 1s):
if( Math.min(A.length, B.length) * 6 < Math.max(A.length ,B.length) )
return -1;
Then you need the sums and the statistics what the other answer describes, but maybe there is place for a slightly different explanation. In order to have the two sums meet, the smaller one can be increased and the larger one can be decreased. For having the minimum amount of steps, you always want to make the largest steps possible, via starting to replace 1s with 6s in the smaller sum (each replacement increasing the sum by 5) and 6s with 1s in the larger sum (each replacement decreasing it by 5), and so on.
As you do not want to generate the steps (at least to my understanding), actually you can track the difference only and also count the pairs together (6s in the larger-sum-array and 1s in the smaller-sum-array, then the same with 5-2, etc.). And in fact you can do this pairing even at the beginning, without knowing which one is the larger/smaller sum, because the pairs will stay pairs, just their direction changes.
Example is JavaScript so it can run here, but I try to write it as Java as possible:
function task(A,B){
if( Math.min(A.length, B.length) * 6 < Math.max(A.length, B.length) )
return -1;
var diff=0;
var statistics=[0,0,0,0,0,0]; // would be a new int[6] in Java
for(var item of A){ // for(int item : A) in Java
// this loop guesses that A has the larger sum
diff+=item;
statistics[item-1]++; // 1s are counted in [0], 2s in [1], ... 6s in [5]
}
for(var item of B){ // for(int item : B) in Java
// this loop guesses that B has the smaller sum
diff-=item;
statistics[6-item]++; // 1s are counted in [5], 2s in [4], ... 6s in [0]
}
if(diff<0){
// the guess was wrong, swaps are needed
diff=-diff;
for(var i=0;i<3;i++){
var swap=statistics[i];
statistics[i]=statistics[5-i];
statistics[5-i]=swap;
}
}
var log=[A.join()," ",B.join()," ",diff," ",statistics.join()].join(); // <-- this one...
// at this point
// - array indices are conveniently denoting step sizes
// - diff is non-negative
// - and we know there is a solution (so we won't run out of array indices for example)
var changes=0;
var i=5;
while(diff>0){
var step = Math.min(statistics[i], Math.ceil(diff/i));
// would better be "int step = Math.min(statistics[i], (diff+i-1)/i);" in Java
// as Math.ceil() produces a double
changes += step;
diff -= i*step;
i--;
}
return [changes," ",log].join(); // <-- ... and this
// are only visuals
return changes;
}
console.log(task([1,2,3,4,3,2,1],[6]));
console.log(task([6],[1,2,3,4,3,2,1]));
console.log(task([2,3,1,1,2],[5,4,6]));
console.log(task([5,4,6],[2,3,1,1,2]));
console.log(task([5,4,1,2,6,6],[2]));
console.log(task([2],[5,4,1,2,6,6]));
At the end I've just thrown it together in Java too: https://ideone.com/mP3Sel
As others have noted, we can solve this with a greedy algorithm. Count the frequencies of numbers for each array, then iterate from the outside in. For the array with the larger sum, iterate over the negative multipliers; for the array with the smaller sum, the positive. Choose the greatest absolute multiplier, then the max frequency available (and needed) each time, and as soon as the sum difference is equal or reverses sign, stop.
2 3 1 1 2 = 9
mult 5 4 3 2 1 0
freq 2 2 1 0 0 0
^ -->
5 4 6 = 15
mult 0 -1 -2 -3 -4 -5
freq 0 0 0 1 1 1
<-- ^
function f(A, B){
let freqSm = [0, 0, 0, 0, 0, 0];
let freqLg = [0, 0, 0, 0, 0, 0];
let smSum = 0;
let lgSum = 0;
let sm = 'A';
let lg = 'B';
A.map(x => {
freqSm[x-1]++;
smSum += x;
});
B.map(x => {
freqLg[x-1]++;
lgSum += x;
});
if (lgSum < smSum){
sm = 'B';
lg = 'A';
let [_freq, _sum] = [freqSm, smSum];
freqSm = freqLg;
freqLg = _freq;
smSum = lgSum;
lgSum = _sum;
}
const smMult = [5, 4, 3, 2, 1, 0];
const lgMult = [0,-1,-2,-3,-4,-5];
const changes = [];
let diff = lgSum - smSum;
function numTxt(count, num){
const ws = [, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
const countTxt = count < 10 ? ws[count] : count;
return `${ countTxt } ${ num }${ count > 1 ? 's' : '' }`;
}
function incSm(i){
const rem = diff % smMult[i];
const mult = Math.min(freqSm[i], Math.ceil(diff / smMult[i]));
diff -= mult * smMult[i];
let txt;
if (diff < 0 && rem){
if (mult > 1)
txt = `Change ${ numTxt(mult-1, i+1) } to 6 and one to ${ i + 1 + rem } in ${ sm }.`;
else
txt = `Change one ${ i + 1 } to ${ i + 1 + rem } in ${ sm }.`;
} else {
txt = `Change ${ numTxt(mult, i+1) } to 6 in ${ sm }.`;
}
changes.push(txt);
}
function decLg(j){
const rem = diff % -lgMult[j];
const mult = Math.min(freqLg[j], Math.ceil(-diff / lgMult[j]));
diff += mult * lgMult[j];
let txt;
if (diff < 0 && rem){
if (mult > 1)
txt = `Change ${ numTxt(mult-1, j+1) } to 1 and one to ${ j + 1 - rem } in ${ lg }.`;
else
txt = `Change one ${ j + 1 } to ${ j + 1 - rem } in ${ lg }.`;
} else {
txt = `Change ${ numTxt(mult, j+1) } to 1 in ${ lg }.`;
}
changes.push(txt);
}
for (let i=0; i<6; i++){
const j = 5 - i;
if (freqSm[i] >= freqLg[j]){
if (freqSm[i]){
incSm(i);
if (diff <= 0)
return changes.join('\n');
}
if (freqLg[j]){
decLg(j);
if (diff <= 0)
return changes.join('\n');
}
} else {
if (freqLg[j]){
decLg(j);
if (diff <= 0)
return changes.join('\n');
}
if (freqSm[i]){
incSm(i);
if (diff <= 0)
return changes.join('\n');
}
}
}
return -1;
}
var input = [
[[2,3,1,1,2], [5,4,6]],
[[5,4,1,2,6,6], [2]],
[[1,2,3,4,3,2,1], [6]]
];
for (let [A, B] of input){
console.log(`A: ${ A }`);
console.log(`B: ${ B }`);
console.log(f(A, B));
console.log('');
}
Related
The task is to find lost element in the array. I understand the logic of the solution but I don't understand how does this formula works?
Here is the solution
int[] array = new int[]{4,1,2,3,5,8,6};
int size = array.length;
int result = (size + 1) * (size + 2)/2;
for (int i : array){
result -= i;
}
But why we add 1 to total size and multiply it to total size + 2 /2 ?? In all resources, people just use that formula but nobody explains how that formula works
The sum of the digits 1 thru n is equal to ((n)(n+1))/2.
e.g. for 1,2,3,4,5 5*6/2 = 15.
But this is just a quick way to add up the numbers from 1 to n. Here is what is really going on.
The series computes the sum of 1 to n assuming they all were present. But by subtracting each number from that sum, the remainder is the missing number.
The formula for an arithmetic series of integers from k to n where adjacent elements differ by 1 is.
S[k,n] = (n-k+1)(n+k)/2
Example: k = 5, n = 10
S[k,n] = 5 6 7 8 9 10
S[k,n] = 10 9 8 7 6 5
S[k,n] = (10-5+1)*(10+5)/2
2S[k,n] = 6 * 15 / 2
S[k,n] = 90 / 2 = 45
For any single number missing from the sequence, by subtracting the others from the sum of 45, the remainder will be the missing number.
Let's say you currently have n elements in your array. You know that one element is missing, which means that the actual size of your array should be n + 1.
Now, you just need to calculate the sum 1 + 2 + ... + n + (n+1).
A handy formula for computing the sum of all integers from 1 up to k is given by k(k+1)/2.
By just replacing k with n+1, you get the formula (n+1)(n+2)/2.
It's simple mathematics.
Sum of first n natural numbers = n*(n+1)/2.
Number of elements in array = size of array.
So, in this case n = size + 1
So, after finding the sum, we are subtracting all the numbers from array individually and we are left with the missing number.
Broken sequence vs full sequence
But why we add 1 to total size and multiply it to total size + 2 /2 ?
The amount of numbers stored in your array is one less than the maximal number, as the sequence is missing one element.
Check your example:
4, 1, 2, 3, 5, 8, 6
The sequence is supposed to go from 1 to 8, but the amount of elements (size) is 7, not 8. Because the 7 is missing from the sequence.
Another example:
1, 2, 3, 5, 6, 7
This sequence is missing the 4. The full sequence would have a length of 7 but the above array would have a length of 6 only, one less.
You have to account for that and counter it.
Sum formula
Knowing that, the sum of all natural numbers from 1 up to n, so 1 + 2 + 3 + ... + n can also be directly computed by
n * (n + 1) / 2
See the very first paragraph in Wikipedia#Summation.
But n is supposed to be 8 (length of the full sequence) in your example, not 7 (broken sequence). So you have to add 1 to all the n in the formula, receiving
(n + 1) * (n + 2) / 2
I guess this would be similar to Missing Number of LeetCode (268):
Java
class Solution {
public static int missingNumber(int[] nums) {
int missing = nums.length;
for (int index = 0; index < nums.length; index++)
missing += index - nums[index];
return missing;
}
}
C++ using Bit Manipulation
class Solution {
public:
int missingNumber(vector<int> &nums) {
int missing = nums.size();
int index = 0;
for (int num : nums) {
missing = missing ^ num ^ index;
index++;
}
return missing;
}
};
Python I
class Solution:
def missingNumber(self, nums):
return (len(nums) * (-~len(nums))) // 2 - sum(nums)
Python II
class Solution:
def missingNumber(self, nums):
return (len(nums) * ((-~len(nums))) >> 1) - sum(nums)
Reference to how it works:
The methods have been explained in the following links:
Missing Number Discussion
Missing Number Solution
I have N numbers, and a range, over which I have to permute the numbers.
For example, if I had 3 numbers and a range of 1-2, I would loop over 1 1 1, 1 1 2, 1 2 1, etc.
Preferably, but not necessarily, how could I do this without recursion?
For general ideas, nested loops don't allow for an arbitrary number of numbers, and recursion is undesireable due to high depth (3 numbers over 1-10 would be over 1,000 calls to the section of code using those numbers)
One way to do this, is to loop with one iteration per permuation, and use the loop variable to calculate the values that a permuation is made off. Consider that the size of the range can be used as a modulo argument to "chop off" a value (digit) that will be one of the values (digits) in the result. Then if you divide the loop variable (well, a copy of it) by the range size, you repeat the above operation to extract another value, ...etc.
Obviously this will only work if the number of results does not exceed the capacity of the int type, or whatever type you use for the loop variable.
So here is how that looks:
int [][] getResults(int numPositions, int low, int high) {
int numValues = high - low + 1;
int numResults = (int) Math.pow(numValues, numPositions);
int results[][] = new int [numResults][numPositions];
for (int i = 0; i < numResults; i++) {
int result[] = results[i];
int n = i;
for (int j = numPositions-1; j >= 0; j--) {
result[j] = low + n % numValues;
n /= numValues;
}
}
return results;
}
The example you gave in the question would be generated with this call:
int results[][] = getResults(3, 1, 2);
The results are then:
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
This is the problem to be solved:
John is assigned a new task today. He is given an array A containing N integers. His task is to update all elements of array to some minimum value x , that is, A[i] = x; 1 <= i <= N; such that sum of this new array is strictly greater than the sum of the initial array.
Note that x should be as minimum as possible such that the sum of the new array is greater than the sum of the initial array.
Input Format:
First line of input consists of an integer N denoting the number of elements in the array A.
Second line consists of N space separated integers denoting the array elements.
Output Format:
The only line of output consists of the value of x.
Sample Input:
5
12345
Sample Output:
4
Explanation:
Initial sum of array= 1 + 2 + 3 + 4 + 5 = 15
When we update all elements to 4, sum of array = 4 + 4 + 4 + 4 + 4 = 20 which is greater than 15.
Note that if we had updated the array elements to 3, sum = 15 which is not greater than 15. So, 4 is the minimum value to which array elements need to be updated.
** ==> Here is my code. How can I improve it? or What is the problem in this code? **
import java.util.Scanner;
public class Test2 {
public static void main(String []args){
Scanner s=new Scanner(System.in);
int check=0, sum=0, biggest=0;
int size=s.nextInt();
if(size>=1 && size<=100000) {
int[] arr=new int[size];
for(int i=0; i<size; i++){
int temp=s.nextInt();
if(temp>=1 && temp<=1000) {
arr[i] = temp;
biggest=biggest > temp ? biggest:temp;
sum=sum+temp;
}
else break;
}
for(int i=1; i<biggest; i++){
check=(size*i)>sum ? i:0;
}
System.out.print(check);
}
else System.err.print("Invalid input size");
}
}
Issue:
for(int i=1; i<biggest; i++){
check=(size*i)>sum ? i:0;
}
There are 2 problems with this, hence it doesn't work. They are as follows-
(size*i)>sum ? i - The problem statement states that it needs minimum possible sum greater than sum of array of elements. Your code blindly assigns i to check without checking the minimality.
check=(size*i)>sum ? i:0 - So, even if you had come across some integer previously, you lost it because you assigned it to 0 if the condition is not satisfied.
I will share my idea of how would I go about this.
Approach 1
Sum all elements like you did.
Now, take average of elements - sum / size of the array. Let's say we store it in a variable average.
Print average + 1 as your answer, as that is the value that could give you minimum possible sum > sum of array itself.
Time Complexity: O(n), where n is size of the array.
Space Complexity: O(1)
Approach 2
Sum all elements like you did.
Calculate min and max for the array and store it in variables, say mini and maxi.
Now, do a binary search between mini and maxi and keep checking the minimum sum > sum criteria.
In this process, you will have variables like low, mid and high.
low = mini,high = maxi
while low <= high:
mid = low + (high - low) / 2
If mid * size <= sum,
low = mid + 1
else
high = mid - 1
Now, print low as your answer.
Let range = maxi - mini.
Time Complexity: O(n) + O(log(range)) = O(n) asymptotically, where n is size of the array.
Space Complexity: O(1)
Not sure if I completely followed what your attempt was, but there should be a pretty straightfoward solution. You know the size of the array and you can easily iterate through the array to get the value of the elements stored in it. All you need to do to find your min x is to take sumOfArray/size of array and then add one to the result to make your result higher.
In your example 15/5=3. 3+1 = 4 so that's your answer. If the numbers summed to 43, 43/5 = 8 r 3, so your answer is 9 (9*5=45). Etc.
When trying some other test cases, then the results are wrong. Try:
Input:
5
1 1 1 1 5
Expected Output: 2 Actual Output: 4
and
Input:
5
5 5 5 5 5
Expected Output: 6 Actual Output: 0
I'm doing some tests in Java to warm up, and I just made this one:
A non-empty zero-indexed array A consisting of N integers is given.
The consecutive elements of array A represent consecutive cars on a
road.
Array A contains only 0s and/or 1s:
0 represents a car traveling east, 1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q),
where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q
is traveling to the west.
For example, consider array A such that:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1 We have five
pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty zero-indexed array A of N integers, returns
the number of pairs of passing cars.
The function should return −1 if the number of pairs of passing cars
exceeds 1,000,000,000.
For example, given:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1 the function
should return 5, as explained above.
Assume that:
N is an integer within the range [1..100,000]; each element of array A
is an integer that can have one of the following values: 0, 1.
Complexity:
expected worst-case time complexity is O(N); expected worst-case space
complexity is O(1), beyond input storage (not counting the storage
required for input arguments). Elements of input arrays can be
modified.
My code is as follows:
public int solution(int[] A) {
// write your code in Java SE 8
int result = 0;
long mult = 0;
for(int value : A){
if(value == 0){
mult ++;
}
else {
result += mult;
}
}
return result;
}
The link with the result is this one:
https://codility.com/demo/results/trainingFFF4BS-AZ3/
If the link die, the result said:
Performance tests
▶ medium_random random, length = ~10,000 ✔OK ▶ large_random random,
length = ~100,000 ✘WRONG ANSWER got 1248768710 expected -1 ▶
large_big_answer
0..01..1, length = ~100,000 ✘WRONG ANSWER got -1794967296 expected -1 ▶ large_alternate
0101..01, length = ~100,000 ✘WRONG ANSWER got 1250025000 expected -1 ▶ large_extreme large test with all 1s/0s, length = ~100,000 ✔OK
Any ideas what is wrong in my code?.
Your rules say,
The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.
And you don't test that condition. You could with a ternary operation and something like
return result > 1000000000 ? -1 : result;
or (debateably) more readable
if (result > 1000000000) {
return -1;
}
return result;
and for improved performance you might add that test like
for (int value : A) {
if (value == 0) {
mult++;
} else {
result += mult;
if (result > 1000000000) {
return -1;
}
}
}
return result;
I have strings of the form 000011122222. That is, consecutive digits repeated random no. of times. Some other examples could be:
0011122223333
01222
00011234444
001122222
and so on. I know, say for a string 01222, that a total of 5!/3! permutations are possible. I need to generate all these permutations for each such string.
I have tried generating permutations by various methods. One is by generating all the possible permutations (just as for strings without repetition), but since the strings that I would be using can be very large this can waste time generating too many redundant permutations.
Secondly, I have tried placing the digits at random indices of a character array equal to the size of the string and terminating the loop when the count of digits is same as in the input string. However, this way I am wasting a lot of memory and also taking up a lot of time.
I need an efficient way to generate permutations for such strings. Just an algorithm or code, either is welcome. I am using Java.
Thanks!
One of the standard algorithms for generating permutations is an algorithm for listing them in lexicographically increasing order. This algorithm, which is used by most implementations of the C++ std::next_permutation algorithm, generates permutations in at most O(n) time per permutation and skips over all permutations that are duplicates of one another. It's also extremely easy to code up.
Hope this helps!
Instead of permuting the original string of digits, permute the digit groups. I don't know how best to describe it so I'll try some psuedocode.
For the string "001222" the digit groups are two 0s, one 1, and three 2s.
permute(groups, permutation):
if there are no non-empty groups
print permutation
else
for each non-empty group
permutation += group.digit
--group.count
permute(groups, permutation)
By looping over groups rather than all digits, it avoids generating duplicates because each digit can be chosen only once for the next position rather than multiple times. Walking through a random permutation you get
Permutation Digit Groups
0: 2, 1: 1, 2: 3 // start
0 0: 1, 1: 1, 2: 3
02 0: 1, 1: 1, 2: 2 // *
021 0: 1, 1: 0, 2: 2 // the 1 group is removed from the set
0212 0: 1, 1: 0, 2: 1
02120 0: 0, 1: 0, 2: 1 // the 0 group is removed from the set
021202 0: 0, 1: 0, 2: 0 // the 2 group is removed from the set
Now unroll back to *.
02 0: 1, 1: 0, 2: 1
Because you are looping over digit groups rather than all the (repeated) digits from the original string, you cannot choose 2 again. This means all the permutations beginning with "02" will be unique because the prefix "02" is generated only once. The same applies throughout the algorithm.
Update
Here's a quick PHP implementation which produces 60 permutations for the input "001222":
function permute(&$groups, &$count, $permutation) {
$done = true;
foreach ($groups as &$group) {
if ($group[1] > 0) {
--$group[1];
permute($groups, $count, $permutation . $group[0]);
++$group[1];
$done = false;
}
}
if ($done) {
echo $permutation . PHP_EOL;
++$count;
}
}
$groups = array(
array(0, 2),
array(1, 1),
array(2, 3),
);
$count = 0;
permute($groups, $count, '');
echo "\nTotal: $count\n";
You can create the strings by randomly choosing the count of digits. Like this:
length : int - Total string length
digits : int - maximum digit to include in the string
string : String - the return value
for(i : int from 0 to digits)
{
remainingChars : int = length - length(string) //remaining chars in string
remainingDigits : int = digits - i + 1
count : int = Random from 1 to (remainingChars - remainingDigits + 1)
Append count times i to the string
}
i don't know exactly what you're trying to say, but i once needed a version of permutation where i had a set of numbers like 012 and all the permutations were:
012 021 102 120 201 210
in order to achieve this, i looked up on wikipedia http://en.wikipedia.org/wiki/Permutation
to find the algorithm, then i just created a method for it like this:
public static boolean Permute(int[] a) {
int k, l, n = a.length -1;
for (k = n -1; ; k--) {
if (k == -1)
return false;
if (a[k] < a[k + 1])
break;
}
for (l = n; l >= 0; l--) {
if (a[k] < a[l]) {
int opt = a[l];
a[l] = a[k];
a[k] = opt;
break;
}
}
for (int i = k + 1, j = n; i < j; i++, j--) {
int opt = a[i];
a[i] = a[j];
a[j] = opt;
}
return true;
}
I can help you if you're more specific