Adding numbers in arrays element by element - java

The goal is to add two numbers together that are stored in arrays-element by element. The numbers do not necessarily need to be of equal length. I am having trouble accounting for the possibility of a carry over.
If the number is 1101 it would be represented : [1,0,1,1]- The least significant bit is at position 0. I am doing the addition without converting it to an integer.
I am making a separate method to calculate the sum of binary numbers but I just want to understand how to go about this using the same logic.
Ex: 349+999 or they could even be binary numbers as well such as 1010101+11
Any suggestions?
int carry=0;
int first= A.length;
int second=B.length;
int [] sum = new int [(Math.max(first, second))];
if(first > second || first==second)
{
for(int i =0; i <A.length;i++)
{
for(int j =0; j <B.length;j++)
{
sum[i]= (A[i]+B[j]);
}
}
return sum;
}
else
{
for(int i =0; i <B.length;i++)
{
for(int j =0; j <A.length;j++)
{
sum[i]= (A[i]+B[j]);
}
}
return sum;
}
For the binary addition:
byte carry=0;
int first= A.length;
int second=B.length;
byte [] sum = new byte [Math.max(first, second)+1];
if(first > second || first==second)
{
for(int i =0; i < A.length && i!= B.length ;i++)
{
sum[i]= (byte) (A[i] + B[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
for(int i = B.length; i < A.length; i++) {
sum[i] = (byte) (A[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
sum[A.length] = carry; //Assigning msb as carry
return sum;
}
else
{
for(int i =0; i < B.length && i!= A.length ;i++) {
sum[i]= (byte) (A[i] + B[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
for(int i = A.length; i < B.length; i++) {
sum[i] = (byte) (B[i] + carry);
if(sum[i]>1) {
sum[i] = (byte) (sum[i] -1);
carry = 1;
}
else
carry = 0;
}
sum[B.length] = carry;//Assigning msb as carry
return sum;
}

There is no need to treat binary and decimal differently.
This handles any base, from binary to base36, and extremely
large values -- far beyond mere int's and long's!
Digits need to be added from the least significant first.
Placing the least significant digit first makes the code
simpler, which is why most CPUs are Little-Endian.
Note: Save the code as "digits.java" -- digits is the
main class. I put Adder first for readability.
Output:
NOTE: Values are Little-Endian! (right-to-left)
base1: 0(0) + 00(0) = 000(0)
base2: 01(2) + 1(1) = 110(3)
base2: 11(3) + 01(2) = 101(5)
base2: 11(3) + 011(6) = 1001(9)
base16: 0A(160) + 16(97) = 101(257)
base32: 0R(864) + 15(161) = 101(1025)
Source code: digits.java:
class Adder {
private int base;
private int[] a;
private int[] b;
private int[] sum;
public String add() {
int digitCt= a.length;
if(b.length>digitCt)
digitCt= b.length; //max(a,b)
digitCt+= 1; //Account for possible carry
sum= new int[digitCt]; //Allocate space
int digit= 0; //Start with no carry
//Add each digit...
for(int nDigit=0;nDigit<digitCt;nDigit++) {
//digit already contains the carry value...
if(nDigit<a.length)
digit+= a[nDigit];
if(nDigit<b.length)
digit+= b[nDigit];
sum[nDigit]= digit % base;//Write LSB of sum
digit= digit/base; //digit becomes carry
}
return(arrayToText(sum));
}
public Adder(int _base) {
if(_base<1) {
base= 1;
} else if(_base>36) {
base=36;
} else {
base= _base;
}
a= new int[0];
b= new int[0];
}
public void loadA(String textA) {
a= textToArray(textA);
}
public void loadB(String textB) {
b= textToArray(textB);
}
private int charToDigit(int digit) {
if(digit>='0' && digit<='9') {
digit= digit-'0';
} else if(digit>='A' && digit<='Z') {
digit= (digit-'A')+10;
} else if(digit>='a' && digit<='z') {
digit= (digit-'a')+10;
} else {
digit= 0;
}
if(digit>=base)
digit= 0;
return(digit);
}
private char digitToChar(int digit) {
if(digit<10) {
digit= '0'+digit;
} else {
digit= 'A'+(digit-10);
}
return((char)digit);
}
private int[] textToArray(String text) {
int digitCt= text.length();
int[] digits= new int[digitCt];
for(int nDigit=0;nDigit<digitCt;nDigit++) {
digits[nDigit]= charToDigit(text.charAt(nDigit));
}
return(digits);
}
private String arrayToText(int[] a) {
int digitCt= a.length;
StringBuilder text= new StringBuilder();
for(int nDigit=0;nDigit<digitCt;nDigit++) {
text.append(digitToChar(a[nDigit]));
}
return(text.toString());
}
public long textToInt(String a) {
long value= 0;
long power= 1;
for(int nDigit=0;nDigit<a.length();nDigit++) {
int digit= charToDigit(a.charAt(nDigit));
value+= digit*power;
power= power*base;
}
return(value);
}
}
public class digits {
public static void main(String args[]) {
System.out.println("NOTE: Values are Little-Endian! (right-to-left)");
System.out.println(test(1,"0","00"));
System.out.println(test(2,"01","1"));
System.out.println(test(2,"11","01"));
System.out.println(test(2,"11","011"));
System.out.println(test(16,"0A","16"));
System.out.println(test(32,"0R","15"));
}
public static String test(int base, String textA, String textB) {
Adder adder= new Adder(base);
adder.loadA(textA);
adder.loadB(textB);
String sum= adder.add();
String result= String.format(
"base%d: %s(%d) + %s(%d) = %s(%d)",
base,
textA,adder.textToInt(textA),
textB,adder.textToInt(textB),
sum,adder.textToInt(sum)
);
return(result);
}
}

First off, adding binary numbers will be different then ints. For ints you could do something like
int first = A.length;
int second = B.length;
int firstSum = 0;
for (int i = 0; i < first; i++){
firstSum += A[i] * (10 ^ i);
}
int secondSum = 0;
for (int j = 0; j < second; j++){
secondSum += B[j] * (10 ^ j);
}
int totalSum = firstSum + secondSum;

It should be represented like this in below as we have to add only the values at same position of both the arrays. Also, in your first if condition, it should be || instead of &&. This algorithm should perfectly work. Let me know if there are any complications.
int carry=0;
int first= A.length;
int second=B.length;
int [] sum = new int [Math.max(first, second)+1];
if(first > second || first==second)
{
for(int i =0; i < A.length && i!= B.length ;i++)
{
sum[i]= A[i] + B[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
for(int i = B.length; i < A.length; i++) {
sum[i] = A[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
sum[A.length] = carry; //Assigning msb as carry
return sum;
}
else
{
for(int i =0; i < B.length && i!= A.length ;i++) {
sum[i]= A[i] + B[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
for(int i = A.length; i < B.length; i++) {
sum[i] = B[i] + carry;
if(sum[i]>9) {
sum[i] = sum[i] -9;
carry = 1;
}
else
carry = 0;
}
sum[B.length] = carry //Assigning msb as carry
return sum;
}

Related

Counting number of duplicates in a given array

Input: 1,4,2,6,7,5,1,2
Output:2
Counting the number of duplicated numbers in a given array for java. I first sorted the array and then counted duplicates. It's showing me error that variable c is not used and that this method should return value of int.
public class Duplicates
public static void main(String[] args) {
int[]list;
int[]c;
int[] c = new int[list.length];
int temp;
for (int i = 0; i < list.length - 1; i++) {
for (int j = i + 1; j < list; j++) {
if (list[I] > list[j]) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
c = list;
}
}
}
int n = 0;
int counter = 0;
int a = -1;
for (int i = 0; i < c.length; ++i) {
if (c[i] == a) {
++n;
if (n == 1) {
++counter;
if (counter == 1) {
System.out.print(c[i]);
} else {
System.out.print("," + c[i]);
}
}
} else {
a = c[i];
n = 0;
}
}
System.out.println("\nNumber of Duplicated Numbers in array:" + counter);
}
}
It's showing me error that variable c is not used
This should be a warning. So the code should still run correctly even with this is showing.
this method should return value of int
This is a compilation error and since you are not returning any int array at the end of the method, your method's return type should be void. You should change your method signature as below,
public static void c(int[] list)
Otherwise you will need to return an int array at the end of your method.
After fixing your code,
public class Duplicates {
public static void main(String[] args) {
int[] list = new int[]{1, 4, 2, 6, 7, 5, 1, 2};
int temp;
for (int i = 0; i < list.length; ++i) {
for (int j = 1; j < (list.length - i); ++j) {
if (list[j - 1] > list[j]) {
temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
int n = 0, counter = 0;
int previous = -1;
for (int i = 0; i < list.length; ++i) {
if (list[i] == previous) {
++n;
if (n == 1) {
++counter;
if (counter == 1) {
System.out.print(list[i]);
} else {
System.out.print(", " + list[i]);
}
}
} else {
previous = list[i];
n = 0;
}
}
System.out.println("\nNumber of Duplicated Numbers in array: " + counter);
}
}

Searching for a sum in an array

I have a method which counts how many sums of 3 elements,which are equal to 0, does the array contains. I need help finding the way to stop counting the same triplets in the loop. For instance, 1 + 3 - 4 = 0, but also 3 - 4 +1 = 0.Here is the method:
private static int counter(int A[])
{
int sum;
int e = A.length;
int count = 0;
for (int i=0; i<e; i++)
{
for (int j=i+1; j<e; j++)
{
sum=A[i]+A[j];
if(binarySearch(A,sum))
{
count++;
}
}
}
return count;
edit: I have to use the Binary Search (the array is sorted).
Here is the binarySearch code:
private static boolean binarySearch(int A[],int y)
{
y=-y;
int max = A.length-1;
int min = 0;
int mid;
while (max>=min)
{
mid = (max+min)/2;
if (y==A[mid])
{
return true;
}
if (y<A[mid])
{
max=mid-1;
}
else
{
min=mid+1;
}
}
return false;
You can avoid counting different triplets by making one assumption that we need to look for the triplets (x,y,z) with x < y < z and A[x] + A[y] + A[z] == 0.
So what you need to do is to modify the binarySearch function to return the number of index that greater than y and has A[z] == -(A[x] + A[y])
private static int binarySearch(int A[],int y, int index)
{
y=-y;
int max = A.length-1;
int min = index + 1;
int mid;
int start = A.length;
int end = 0;
while (max>=min)
{
mid = (max+min)/2;
if (y==A[mid])
{
start = Math.min(start, mid);
max = mid - 1;
} else
if (y<A[mid])
{
max=mid-1;
}
else
{
min=mid+1;
}
}
int max = A.length - 1;
int min = index + 1;
while (max>=min)
{
mid = (max+min)/2;
if (y==A[mid])
{
end = Math.max(end, mid);
min= mid + 1;
} else if (y<A[mid])
{
max=mid-1;
}
else
{
min=mid+1;
}
}
if(start <= end)
return end - start + 1;
return 0;
}
So the new function binarySearch will return the total number of index that greater than index and has value equals to y.
So the rest of the job is to count the answer
private static int counter(int A[])
{
int sum;
int e = A.length;
int count = 0;
for (int i=0; i<e; i++)
{
for (int j=i+1; j<e; j++)
{
sum=A[i]+A[j];
count += binarySearch(A,sum, j);
}
}
return count;
}
Notice how I used two binary search to find the starting and the ending index of all values greater than y!
private static int counter(int A[]) {
int e = A.length;
int count = 0;
for (int i = 0; i < e; i++) {
for (int j = 1; (j < e - 1) && (i != j); j++) {
for (int k = 2; (k < e - 2) && (j != k); k++) {
if (A[i] + A[j] + A[k] == 0) {
count++;
}
}
}
}
return count;
}
private static int counter(int ints[]) {
int count = 0;
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints.length; j++) {
if (i == j) {
// cannot sum with itself.
continue;
}
for (int k = 0; k < ints.length; k++) {
if (k == j) {
// cannot sum with itself.
continue;
}
if ((ints[i] + ints[j] + ints[k]) == 0) {
count++;
}
}
}
}
return count;
}
To solve problem with binary search
Your code was almost correct. all you needed to do was just to replace
if (sum == binarySearch(A,sum)) {
with this
if (binarySearch(A,sum)) {
I am assuming that your binarySearch(A, sum) method will return true if it will found sum in A array else false
private static int counter(int A[]) {
int sum;
int e = A.length;
int count = 0;
for (int i=0; i<e; i++) {
for (int j=i+1; j<e; j++) {
sum=A[i]+A[j];
if (binarySearch(A,sum)) {
count++;
}
}
}
return count;
}
Here is my solution assuming the array is sorted and there are no repeated elements, I used the binary search function you provided. Could the input array contain repeated elements? Could you provide some test cases?
In order to not counting the same triplets in the loop, we should have a way of inspecting repeated elements, the main idea that I used here is to have a list of int[] arrays saving the sorted integers of {A[i],A[j],-sum}.Then in each iteration I compare new A[i] and A[j] to the records in the list, thus eliminating repeated ones.
private static int counter(int A[]){
int sum;
int e = A.length;
int count = 0;
List<int[]> elements = new ArrayList<>();
boolean mark = false;
for (int i=0; i<e; i++)
{
for (int j=i+1; j<e; j++)
{
sum=A[i]+A[j];
if (-sum == binarySearch(A,sum)){
int[] sort = {A[i],A[j],-sum};
if(-sum == A[i] || -sum == A[j]){
continue;
}else{
Arrays.sort(sort);
//System.out.println("sort" + sort[0] + " " + sort[1]+ " " + sort[2]);
for (int[] element : elements) {
if((element[0] == sort[0] && element[1] == sort[1]) && element[2] == sort[2])
mark = true;
}
if(mark){
mark = false;
continue;
}else{
count++;
elements.add(sort);
//System.out.println("Else sort" + sort[0] + " " + sort[1]);
}
}
}
}
}
return count;
}
you can use a assisted Array,stored the flag that indicate if the element is used;
Here is the code:
private static int counter(int A[])
{
int sum;
int e = A.length;
int count = 0;
// assisted flag array
List<Boolean> flagList = new ArrayList<Boolean>(e);
for (int k = 0; k < e; k++) {
flagList.add(k, false);// initialization
}
for (int i=0; i<e; i++)
{
for (int j=i+1; j<e; j++)
{
sum=A[i]+A[j];
// if element used, no count
if(binarySearch(A,sum)&& !flagList.get(i)&& !flagList.get(j))
{
count++;
flagList.set(i, true);
flagList.set(j, true);
}
}
}
return count;

Polynomial Class in Java problems

I'm having trouble with this polynomial class, specifically the checkZero and differentiate methods. The checkZero class is supposed to see if there are any leading coefficients in the polynomial, and if so, it should resize the coefficient array. The differentiate method should find the derivative of a polynomial, but I keep getting ArrayIndexOutOfBounds errors.
public class Polynomial {
private float[] coefficients;
public static void main (String[] args){
float[] fa = {3, 2, 4};
Polynomial test = new Polynomial(fa);
}
public Polynomial() {
coefficients = new float[1];
coefficients[0] = 0;
}
public Polynomial(int degree) {
coefficients = new float[degree+1];
for (int i = 0; i <= degree; i++)
coefficients[i] = 0;
}
public Polynomial(float[] a) {
coefficients = new float[a.length];
for (int i = 0; i < a.length; i++)
coefficients[i] = a[i];
}
public int getDegree() {
return coefficients.length-1;
}
public float getCoefficient(int i) {
return coefficients[i];
}
public void setCoefficient(int i, float value) {
coefficients[i] = value;
}
public Polynomial add(Polynomial p) {
int n = getDegree();
int m = p.getDegree();
Polynomial result = new Polynomial(Polynomial.max(n, m));
int i;
for (i = 0; i <= Polynomial.min(n, m); i++)
result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
if (i <= n) {
//we have to copy the remaining coefficients from this object
for ( ; i <= n; i++)
result.setCoefficient(i, coefficients[i]);
} else {
// we have to copy the remaining coefficients from p
for ( ; i <= m; i++)
result.setCoefficient(i, p.getCoefficient(i));
}
return result;
}
public void displayPolynomial () {
for (int i=0; i < coefficients.length; i++)
System.out.print(" "+coefficients[i]);
System.out.println();
}
private static int max (int n, int m) {
if (n > m)
return n;
return m;
}
private static int min (int n, int m) {
if (n > m)
return m;
return n;
}
public Polynomial multiplyCon (double c){
int n = getDegree();
Polynomial results = new Polynomial(n);
for (int i =0; i <= n; i++){ // can work when multiplying only 1 coefficient
results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient
}
return results;
}
public Polynomial multiplyPoly (Polynomial p){
int n = getDegree();
int m = p.getDegree();
Polynomial result = null;
for (int i = 0; i <= n; i++){
Polynomial tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method
if (result == null){
result = tmpResult;
} else {
result = result.add(tmpResult);
}
}
return result;
}
public void checkZero(){
int newDegree = getDegree();
int length = coefficients.length;
float testArray[] = coefficients;
for (int i = coefficients.length-1; i>0; i--){
if (coefficients[i] != 0){
testArray[i] = coefficients[i];
}
}
for (int j = 0; j < testArray.length; j++){
coefficients[j] = testArray[j];
}
}
public Polynomial differentiate(){
int n = getDegree();
int newPolyDegree = n - 1;
Polynomial newResult = new Polynomial();
if (n == 0){
newResult.setCoefficient(0, 0);
}
for (int i =0; i<= n; i++){
newResult.setCoefficient(i, coefficients[i+1] * (i+1));
}
return newResult;
}
}
There might be more problems, but one is a problem with your differentiate method:
int n = getDegree();
...
Polynomial newResult = new Polynomial();
...
for (int i = 0; i <= n; i++)
{
newResult.setCoefficient(i, coefficients[i + 1] * (i + 1)); //This line
}
Your paramaterless constructor initializes an array with length 1, so "newResult" will only have 1 index, and you try to put something into place i, which goes above 1 if the Polynomial you are in have an array of greater length than 1.
First, a few code notes:
New arrays are automatically initialized to 0 in Java. This is not needed.
coefficients = new float[degree+1];
for (int i = 0; i <= degree; i++)
coefficients[i] = 0;
I also see many lines which might become more readable and compact if you use the trinary operator, for example:
int i;
for (i = 0; i <= Polynomial.min(n, m); i++)
result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
if (i <= n) {
//we have to copy the remaining coefficients from this object
for ( ; i <= n; i++)
result.setCoefficient(i, coefficients[i]);
} else {
// we have to copy the remaining coefficients from p
for ( ; i <= m; i++)
result.setCoefficient(i, p.getCoefficient(i));
}
Could become something like
for (int i = 0; i <= result.getDegree(); i++)
result.setCoefficient(i,
i>n?0:coefficients[i] +
i>m?0:p.getCoefficient(i));
The one bug I did spot was here:
int n = getDegree();
....
for (int i =0; i<= n; i++){
newResult.setCoefficient(i, coefficients[i+1] * (i+1));
}
This will always call coefficients[coefficients.length] on the last iteration, which will always fail.
The stack trace of the exception when you ran this program should tell you exactly where the error is, by the way.

How to generate combinations obtained by permuting 2 positions in Java

I have this problem, I need to generate from a given permutation not all combinations, but just those obtained after permuting 2 positions and without repetition. It's called the region of the a given permutation, for example given 1234 I want to generate :
2134
3214
4231
1324
1432
1243
the size of the region of any given permutation is , n(n-1)/2 , in this case it's 6 combinations .
Now, I have this programme , he does a little too much then what I want, he generates all 24 possible combinations :
public class PossibleCombinations {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Entrer a mumber");
int n=s.nextInt();
int[] currentab = new int[n];
// fill in the table 1 TO N
for (int i = 1; i <= n; i++) {
currentab[i - 1] = i;
}
int total = 0;
for (;;) {
total++;
boolean[] used = new boolean[n + 1];
Arrays.fill(used, true);
for (int i = 0; i < n; i++) {
System.out.print(currentab[i] + " ");
}
System.out.println();
used[currentab[n - 1]] = false;
int pos = -1;
for (int i = n - 2; i >= 0; i--) {
used[currentab[i]] = false;
if (currentab[i] < currentab[i + 1]) {
pos = i;
break;
}
}
if (pos == -1) {
break;
}
for (int i = currentab[pos] + 1; i <= n; i++) {
if (!used[i]) {
currentab[pos] = i;
used[i] = true;
break;
}
}
for (int i = 1; i <= n; i++) {
if (!used[i]) {
currentab[++pos] = i;
}
}
}
System.out.println(total);
}
}
the Question is how can I fix this programme to turn it into a programme that generates only the combinations wanted .
How about something simple like
public static void printSwapTwo(int n) {
int count = 0;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < n - 1;i++)
for(int j = i + 1; j < n; j++) {
// gives all the pairs of i and j without repeats
sb.setLength(0);
for(int k = 1; k <= n; k++) sb.append(k);
char tmp = sb.charAt(i);
sb.setCharAt(i, sb.charAt(j));
sb.setCharAt(j, tmp);
System.out.println(sb);
count++;
}
System.out.println("total=" + count+" and should be " + n * (n - 1) / 2);
}

Java permutations

I am trying to run my code so it prints cyclic permutations, though I can only get it to do the first one at the moment. It runs correctly up to the point which I have marked but I can't see what is going wrong. I think it has no break in the while loop, but I'm not sure. Really could do with some help here.
package permutation;
public class Permutation {
static int DEFAULT = 100;
public static void main(String[] args) {
int n = DEFAULT;
if (args.length > 0)
n = Integer.parseInt(args[0]);
int[] OA = new int[n];
for (int i = 0; i < n; i++)
OA[i] = i + 1;
System.out.println("The original array is:");
for (int i = 0; i < OA.length; i++)
System.out.print(OA[i] + " ");
System.out.println();
System.out.println("A permutation of the original array is:");
OA = generateRandomPermutation(n);
printArray(OA);
printPemutation(OA);
}
static int[] generateRandomPermutation(int n)// (a)
{
int[] A = new int[n];
for (int i = 0; i < n; i++)
A[i] = i + 1;
for (int i = 0; i < n; i++) {
int r = (int) (Math.random() * (n));
int swap = A[r];
A[r] = A[i];
A[i] = swap;
}
return A;
}
static void printArray(int A[]) {
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " ");
System.out.println();
}
static void printPemutation(int p[])// (b)
{
System.out
.println("The permutation is represented by the cyclic notation:");
int[] B = new int[p.length];
int m = 0;
while (m < p.length)// this is the point at which my code screws up
{
if (!check(B, m)) {
B = parenthesis(p, m);
printParenthesis(B);
m++;
} else
m++;
}// if not there are then repeat
}
static int[] parenthesis(int p[], int i) {
int[] B = new int[p.length];
for (int a = p[i], j = 0; a != B[0]; a = p[a - 1], j++) {
B[j] = a;
}
return B;
}
static void printParenthesis(int B[]) {
System.out.print("( ");
for (int i = 0; i < B.length && B[i] != 0; i++)
System.out.print(B[i] + " ");
System.out.print(")");
}
static boolean check(int B[], int m) {
int i = 0;
boolean a = false;
while (i < B.length || !a) {
if ((ispresent(m, B, i))){
a = true;
break;
}
else
i++;
}
return a;
}
static boolean ispresent(int m, int B[], int i) {
return m == B[i] && m < B.length;
}
}
Among others you should check p[m] in check(B, p[m]) instead of m:
in static void printPemutation(int p[]):
while (m < p.length){
if (!check(B, p[m])) {
B = parenthesis(p, m);
printParenthesis(B);
}
m++;
}
then
static boolean check(int B[], int m) {
int i = 0;
while (i < B.length) {
if (m == B[i]) {
return true;
}
i++;
}
return false;
}
this does somehow more what you want, but not always i fear...

Categories

Resources