I am fairly new to this so any help would be appreciated. I am trying to subtract elements of array 'b' from array 'a'( not removing but just subtracting) provided if the element of array 'a' is greater than the corresponding element of array 'b'.
I am not getting the required output its just printing the array I have entered
Scanner sc = new Scanner(System.in);
short n = sc.nextShort();
short a[] = new short[n];
short b[] = new short[n];
for (short i = 0; i < n; i++) {// taking elements input
a[i] = sc.nextShort();
}
for (short i = 0; i < n; i++) {// taking elements input
b[i] = sc.nextShort();
}
short m = 0;
for (short i = 0; i < n; i++) {// finding smallest element in array 'a'
for (short j = 0; j < n; j++) {
if (a[i] < a[j]) {
m = a[i];
}
}
}
boolean allequal = false;
while (!allequal) {
for (short i = 0; i < n; i++) {// subtracting elements
if (a[i] == m)
continue;
if (a[i] >= b[i]) {
a[i] -= b[i];
}
}
for (short i = 0; i < n; i++) {
for (short j = 0; j < n; j++) {
if (a[i] == a[j]) {
allequal = true;
} else {
allequal = false;
}
}
}
}
for (int i = 0; i < n; i++) {// printing array 'a'
System.out.print(a[i] + " ");
}
5
5 7 10 5 15
2 2 1 3 5
5 5 9 5 10
Your program does not enter while loop since you mistakenly used = operator in while (allequal = false) { which is assignment, not comparison. The correct form would be allequal == false which rewrites to !allequal. I didn't checked remaining code.
Note you should use good IDE which would prevent you doing such bug and provide debugger from which you could easily discover yourself.
Related
I am working on a code where you subtract respective elements of array 'a' from array'b' till you get all elements in array 'a' equal.
Condition is that a[i]>b[i] for subtraction.
But not every time getting all elements equal is possible, so in that case I would like my code to print '-1', how can I achieve it. I really tried hard to figure it out, don't give complex solutions as I am a beginner. you can subtract as many times you want.
Scanner sc = new Scanner(System.in);
short n = sc.nextShort();
short a[] = new short[n];
short b[] = new short[n];
for (short i = 0; i < n; i++) {// taking elements input
a[i] = sc.nextShort();
}
for (short i = 0; i < n; i++) {// taking elements input
b[i] = sc.nextShort();
}
short minimumOfArraya = 0;
for (short i = 0; i < n; i++) {// finding smallest element in array 'a'
for (short j = 0; j < n; j++) {
if (a[i] < a[j]) {
minimumOfArraya = a[i];
}
}
}
boolean allequal = false;
int counter = 0;
while (!allequal) {
for (short i = 0; i < n; i++) {// subtracting elements
if (a[i] == minimumOfArraya)
continue;
if (a[i] >= b[i]) {
a[i] -= b[i];
counter++;
}
}
for (short i = 0; i < n; i++) {
if (a[0] == a[i]) {
allequal = true;
} else {
allequal = false;
break;
}
}
}
for (int i = 0; i < n; i++) {// printing array 'a'
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println(counter);
4
5 7 4 3//infinite loop
4 1 0 0
working input
5
5 7 10 5 15
2 2 1 3 5
output
5 5 5 5 5
8
The minimum can be found faster:
short minimumOfArraya = Short.MAX_VALUE;
for (short i = 0; i < n; i++) {// finding smallest element in array 'a'
if (a[i] < minimumOfArraya]) {
minimumOfArraya = a[i];
}
}
The for loop to check whether all are equal should initially have an allequal true,
and on finding a false, break. The initial setting to true was missing.
boolean allequal = false;
int counter = 0;
while (!allequal) {
for (short i = 0; i < n; i++) {// subtracting elements
if (a[i] == minimumOfArraya)
continue;
if (a[i] >= b[i]) {
a[i] -= b[i];
counter++;
}
}
allequal = true;
for (short i = 0; i < n; i++) {
allequal = a[0] == a[i];
if (!allequal) {
break;
}
}
}
If counter was not increased inside the while, the code may very well stay looping till overflow. If the minimum was 100 and 102 got 98 for instance.
If some of iterations producing number which is less than minimum, it clearly shows that you cannot make all elements equal. Check that after subtracting
while (!allequal) {
boolean impossible = false;
for (short i = 0; i < n; i++) {
if (a[i] < mimimumofArraya) {
impossible = true;
break;
}
if (a[i] == minimumOfArraya) continue;
if (a[i] >= b[i]) {
a[i] -= b[i];
counter++;
}
}
if (impossible) {
counter = -1;
break;
}
// The rest of your loop
...
}
This question already has answers here:
How do i find and count duplicates in a 2 dimensional array?
(4 answers)
Closed 5 years ago.
I am a beginner coder just learning arrays. I recently learned 2d arrays and I am trying to find out if there is a duplicate in a 2d array. I know how to check if there is a duplicate in the same column or row, but I cannot figure out how to compare a number if it is not in the same column or row as the number I am trying to compare it to. Here is my code as of now:
public static boolean correctNumbers(int[][] values) {
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
for (int k = j + 1; k < values.length; k++) {
if (num == values[i][k] || num > values.length * values.length || num < 1) {
return false;
}
}
for (int l = i + 1; l < values.length; l++) {
if (num == values[l][j] || num > values.length * values.length || num < 1) {
return false;
}
}
}
}
return true;
}
I need to create a method and I cannot use any other methods in creating it.
Thanks for the help!
Edit: It returns false if there is duplicates in the array, a number in the array is less than 1, or greater than total number of elements in the array. In other words this method is checking to see if the array contains all the values of 1 to (i*j) in the array. I realized I did a bad job of explaining that part. Thanks again!
You can simplify the logic by saving the numbers into a hashset and when you're iterating them check each one if it's already there (meaning duplicate) otherwise add it:
public static boolean correctNumbers(int[][] values) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
if (set.contains(num)) {
return false;
}
set.add(num);
}
}
return true;
}
EDIT
We can use an array for the same functionality the hashset is doing in the code above:
public static boolean correctNumbers(int[][] values) {
int n = values.length;
int[] dict = new int[n * n + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
if (num < 1 || num > n || dict[num] != 0) {
return false;
}
dict[num] = 1;
}
}
return true;
}
I want to write a program that looks for three numbers in array that gives an arithmetic sequence of length three - three numbers a,b and c form an arithmetic sequence of length 3 if: b-a = c-b.
The problem is that he code doesn't print "yes" even when it should, it never get into the command if. I guess I have a problem when writing the math command for b-a = c-b.
public static void main (String[] args) {
int [] a = new int [args.length - 1];W
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i+1]);
}
for (int i = 0; i < a.length - 2; i++) {
for (int j = i+1; j < a.length - 1; j++) {
int b = a[i];
for (int k = j + 1; k < a.length; k++) {
int c = a[k];
if (b - a[i] == c - b) {
System.out.println("yes");
break;
}
}
}
}
}
}
I think this is what you want:
Changes:
First sort the array
When you break from the inner most loop, you
must also break from all the outer loops (check code below)
Arrays.sort(a);
outerLoop:
for (int i = 0; i < a.length - 2; i++) {
for(int j = i+1; j < a.length - 1; j++){
for(int k = j+1; k < a.length; k++){
if(a[j] - a[i] == a[k] - a[j]){
System.out.println("yes");
break outerLoop;
}
}
}
}
Update:
You are missing the first element of the array because of this code:
int [] a = new int [args.length - 1]
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i+1]);
}
Change it to:
int [] a = new int [args.length]
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i]);
}
This works:
public static void main(String[] args) {
int [] a =new int [new Integer(args.length)];
for (int i = 0; i<a.length; i++) {
a[i] = Integer.parseInt(args[i]);
}
for(int j=0;j<a.length;j++){
if(j+2<a.length){
if(a[j+1]-a[j] ==a[j+2] - a[j+1])
System.out.println("Yes........." +a[j] +""+ a[j+1] +"" +a[j+2]);
}
}
}
tested with 2 3 4 6(2 3 4 is sequence) and 1 3 5 7 (1 3 5 and 3 5 7 are sequence)
I have a field a[] inside is 1, 5, 3, 2, 4
when i reach number 3 in for loop i want to send it(number 3) to the end of field so it will have index 4 and indexes of other numbers (2 and 4) have to be one less a[2] = 2, a[3] = 4
How can i do that? It can be a field with for example 100 values in.
Thanks for the reply!
My program look like this:
for(int i = 0; i < a.length; i++) {
if(a[i] == 3) {
// i dont know what have to go there
}
}
int match = 3;
for(int i = 0; i < a.length; i++) {
if(a[i] == match) {
a[i] = a[i+1];
a[i+1] = a[i+2];
a[i+2] = match;
}
}
Note that there is ZERO error checking
[edit] With some safety (no 'need' for error checking in this version as I just move the entire array one position to the left and put the match variable at the end.
int match = 3;
for(int i = 0; i < a.length; i++) {
if(a[i] == match) {
for int j = i; j < a.length-1; j++) {
a[j] = a[j+1];
}
a[j] = match;
break;
}
}
}
[/edit]
Creating the array, I am letting the user choose the length:
StartNum = scan.nextInt();
int[] NumBox = new int[StartNum];
for (int i = 1; i < NumBox.length+1; i++)
{NumBox[i - 1] = i;}
NumBox[0]=0;
Assuming there are other methods that can change cells in NumBox to 0, how would I use a for loop to check each cells in the array for any divisor? If there are no divisors for the cell in the array, it will then become a 0. For example, if the array is [0,2,0,4,0,6,7,8,9] 9,2 and 7 would become a 0.
The code below is what I tired but didn't get far.
boolean NoDiv=false;
for (int a=1; a < NumBox.length+1; a++)
{
a++
for (int check=1; a < NumBox.length+1; check++)
{
if (NumBox[a-1]% check == 0 && NumBox[a-1] !=0)
{
NumBox[a-1] = 0;
}
}
}
for (int i = 0; i < NumBox.length; i++) {
if (NumBox[i] == 0) continue;
boolean hasDivisor = false;
for (int j = 0; j < i; j++) {
if (NumBox[j] == 0) continue;
if (NumBox[i] % NumBox[j] == 0) {
hasDivisor = true;
break;
}
}
if (!hasDivisor) NumBox[i] = 0;
}