Complexity in tilde notation of nested for loops - java

How do I find the complexity in tilde notation of the following algorithm:
for (int j = 0; j < N; j++) {
for (int k = j + 1; k < N; k++) {
array[k] = array[j];
}
array[j] = k
}
I've made a table with how many times the inner for-loop loops if N = 9:
| j | # of loops |
|:-----------|------------:|
| 0 | 8 |
| 1 | 7 |
| 2 | 6 |
| 3 | 5 |
| 4 | 4 |
| 5 | 3 |
| 6 | 2 |
| 7 | 1 |
| 8 | 0 |

As you evaluate, the number of inner iterations decreases linearly from 8 down to 0, i.e. it is 4 on average, for a total of 4.9=36.
More generally, the average is (N-1)/2 and the total N.(N-1)/2.
Consequently, I(N) ~ N²/2, in terms of the iteration count.
In terms of memory accesses (R+W), it's the double: A(N) ~ N². (The extra access in the outer loop adds a negligible N contribution.)

Related

While loop gets terminated before condition is met when backtracking

I'm trying to write an iterative program that will help me palce 4 queens on a 4x4 board without them hitting each other. The problem is after looping through each position and backtracking a couple of times my main while loop that keeps looping until a solution is found gets terminated and the program ends even though the condition is not yet met.
I tried the following code:
static int[] solve(char[][] board){
int[] position = new int[4];
int row = 0;
int column = 0;
while(row < 4){
for(boolean check; column < board.length; column++){
System.out.println("["+row+","+column+"]");
check = true;
for(int queen= 0; queen < row; queen++){
if (position[queen] == column || queen- position[queen] == row - column || queen + position[queen] == row + column) {
check = false;
break;
}
}
if(check){
position[row] = column;
column = 0;
row++;
}
if(column > 2){
column = position[--row];
}
}
}
return position;
}
I'm currently getting the following output:
| Q | X | X | X |
| X | X | X | Q |
| X | Q | X | X |
| Q | X | X | X |
To check when exactly the while loop is getting terminated I printed the location (row and column)
System.out.println("["+row+","+column+"]"); and got the following:
[0,0][1,0][1,1][1,2][2,0][2,1][2,2][2,3][1,3][2,0][2,1][3,0][3,1][3,2][3,3][2,2][2,3]
After backtracking to [2,3] the while loop ends even though my row count is still less than 4.
I was expecting the following output:
| X | Q | X | X |
| X | X | X | Q |
| Q | X | X | X |
| X | X | Q | X |
I tried the code in a different compiler and still got the same wrong output. Is there a logical mistake that I missed out?
I'm new to programming so I'm still trying to get the hang of the fundamentals.

vice versa coding for value and its variables

I have 2 variables A and B
if A =1 then B should B=2
and if A=2 then B should B=1
Like this, there are 3 pairs 1-2,3-4,5-6
What's the best way of making a code instead of just if-else
It is possible to use simple addition and subtraction to get the other element of the two (x, x + 1):
int a = 1; // the other is 2, sum is 3
int b = 3 - a; // if a = 2, b = 1
int c = 3; // the other is 4, sum is 7
int d = 7 - c; // if c = 4, d = 3
int m = 5; // the other is 6, sum is 11
int n = 11 - m;
Another approach could be using the following logic:
if (a % 2 == 1) b = a + 1;
else b = a - 1;
So, an array could be used to provide +/- 1:
static int[] signs = {-1, 1};
public static int nextWithArrPositive(int a) {
return a + signs [a % 2];
}
This expression fails to work for negative a as in this case a % 2 == -1 and more advanced logic would be required to calculate the value properly to take into account the negative remainder:
public static int nextWithArr(int a) {
int sign = (a & 0x80000000) >> 31; //-1 if a < 0, 0 otherwise
// a >= 0 : 0 - even, 1 - odd;
// a < 0 : 1 - even, 0 - odd
return a + signs[a % 2 - sign];
}
However, a simpler expression can be designed:
public static int nextWithMod(int a) {
return a + a % 2 - (a - 1) % 2;
}
Let's compare the results of the three implementations including xor solution b = ((a - 1) ^ 1) + 1 offered in the comments by user3386109:
public static int nextXor(int a) {
return ((a - 1) ^ 1) + 1;
}
Tests:
System.out.println("+-----+-----+-----+-----+");
System.out.println("| a | arr | mod | xor |");
System.out.println("+-----+-----+-----+-----+");
for (int i = -6; i < 7; i++) {
System.out.printf("| %2d | %2d | %2d | %2d |%n", i, nextWithArr(i), nextWithMod(i), nextXor(i));
}
System.out.println("+-----+-----+-----+-----+");
Output:
+-----+-----+-----+-----+
| a | arr | mod | xor |
+-----+-----+-----+-----+
| -6 | -5 | -5 | -7 |
| -5 | -6 | -6 | -4 |
| -4 | -3 | -3 | -5 |
| -3 | -4 | -4 | -2 |
| -2 | -1 | -1 | -3 |
| -1 | -2 | -2 | 0 |
| 0 | -1 | 1 | -1 |
| 1 | 2 | 2 | 2 |
| 2 | 1 | 1 | 1 |
| 3 | 4 | 4 | 4 |
| 4 | 3 | 3 | 3 |
| 5 | 6 | 6 | 6 |
| 6 | 5 | 5 | 5 |
+-----+-----+-----+-----+
One simple solution is a table lookup. In an array for each possible value of a I store the corresponding value of b:
private static final int[] B_PER_A = { -1, 2, 1, 4, 3, 6, 5 };
Since array indices always start at 0 in Java, we need to put a dummy value at index 0. This value is never used (or should never be, at least).
Let’s try it out:
for (int a = 1; a <= 6; a++) {
int b = B_PER_A[a];
System.out.format("a: %d; b: %d.%n", a, b);
}
Output:
a: 1; b: 2.
a: 2; b: 1.
a: 3; b: 4.
a: 4; b: 3.
a: 5; b: 6.
a: 6; b: 5.
Generalized to more than 3 pairs
If you need to handle a variable number of pairs, resort to math.
public static int calcB(int a) {
// 0-based index of pair (0 = 1-2, 1 = 3-4, etc.)
int pairNumber = (a - 1) / 2;
// a + b for given pair
int pairSum = 4 * pairNumber + 3;
int b = pairSum - a;
return b;
}
In each pair the sum is equivalent to 3 modulo 4. I am exploiting this fact in finding the sum for a given pair. When I subtract a from that sum, I get b. Let’s see that demonstrated too:
for (int a = 1; a <= 8; a++) {
int b = calcB(a);
System.out.format("a: %d; b: %d.%n", a, b);
}
a: 1; b: 2.
a: 2; b: 1.
a: 3; b: 4.
a: 4; b: 3.
a: 5; b: 6.
a: 6; b: 5.
a: 7; b: 8.
a: 8; b: 7.
The latter solution is more complicated and harder to read. So if you always have got three pairs, no more, no less, I recommend the simpler table lookup presented first.

3 Lines only about for loop, very confused

why is the output 22212345?
Shouldn't it be: "43212345", because when we are keep adding the first values of the string onto the previous version of the string.
So everytime we increment k, we are going from 2,3,4 and adding it onto the previous version.
why is the output 22212345?
String str = "12345";
for (int k = 1; k <= 3; k++)
str = str.charAt(k) + str;
So everytime we increment k, we are going from 2,3,4 and adding it onto the previous version.
No, you're not. You're prefixing str with the char at k.
So, if we get a pen and piece of paper and desk check the code (why don't people desk check anymore 😓) you will see what's actually happening...
+---+-----------+---------+-----------------------+
| k | char at k | str | result (charAt + str) |
+---+-----------+---------+-----------------------+
| 1 | 2 | 12345 | 212345 |
| 2 | 2 | 212345 | 2212345 |
| 3 | 2 | 2212345 | 22212345 |
+---+-----------+---------+-----------------------+
This happends in each iteration:
first_loop_state: {
k : 1
initial_str : "12345";
str.charAt(k) : '2'
final_str : "212345"
}
second_loop_state:{
k : 2
initial_str : "212345";
str.charAt(k) : '2'
final_str : "2212345"
}
third_loop_state:{
k : 3
initial str : "2212345";
str.charAt(k) : '2'
final_str : "22212345"
}

Print A 2D Array As A Grid? (Java)

I've run into a bit of a conundrum in a personal Java project I've been working on. I want to print a two-dimensional array of Strings in the form of a table. Not the Strings by themselves, but with row and column labels as well (think Microsoft Excel). This is what I envision the finished product to be, with asterisks representing where the Strings should go.
| A | B | C | D | E | F | G |
----+---------+---------+---------+---------+---------+---------+---------+
1 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
2 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
3 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
4 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
5 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
6 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
7 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
8 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
9 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
10 | * | * | * | * | * | * | * |
----+---------+---------+---------+---------+---------+---------+---------+
I know that this will use a nested forward loop, and that the logical process would be to put the String values in the inner loop, like "example[i][j]" type format. I'm just so confused as to how I go about getting the design around the cells in the right format, limiting each String to 10 characters like how Excel limits their cell size when shrunken down. Do I use substring for that? Do I use printf to get the 10th row correctly spaced?
Any pointers are greatly appreciated, I've never been stumped quite like this before.
The first line should be easy enough, assuming you don't exceed 26 columns, i.e. column name is just A to Z.
The even lines are all a lead-in of ----+, followed by columnCount repeats of ---------+.
The odd lines (except first), are a lead-in of 999 |, where 999 is a row number, right-justified, with leading spaces. That can be done with printf() or String.format() with a format string of
"%3d |".
Following the lead-in are columnCount repeats of a string value, trimmed and center-aligned to 9 characters, followed by a |.
To center-align to 9 characters, do the following:
If length > 9, trim to 9 (yes, using substring()).
Otherwise, calculate spaces needed, i.e. spacesTotal = 9 - trimmedLength.
Calculate spaces on left: spaceBefore = spacesTotal / 2.
Calculate spaces on right: spaceAfter = spacesTotal - spaceBefore.
By doing it that way, an odd number of spaces such as 5, becomes 2 before and 3 after.
Now print spaceBefore spaces, the (trimmed) text value, then spaceAfter spaces, and a |.
public static void printStringGrid(String[][] array){
System.out.print(" |");
for (int i = 0; i < array[0].length; i++){
System.out.print(" ");
System.out.print((char)('A' + i));
System.out.print(" |");
}
System.out.println();
for (int i = 0; i < array.length; i++){
System.out.print("----+");
for (int j = 0; j < array[0].length; j++){
System.out.print("---------+");
}
System.out.println();
System.out.print(" " + (i + 1) + " |");
for (int j = 0; j < array[0].length; j++){
if (array[i][j].length() < 10){
int spaces = (9 - array[i][j].length()) / 2;
for (int k = 0; k < spaces; k++){
System.out.print(" ");
}
System.out.print(array[i][j]);
for (int k = 0; k < (9 - array[i][j].length()) - spaces; k++){
System.out.print(" ");
}
}
else{
System.out.print(array[i][j].substring(0, 9));
}
System.out.print("|");
}
System.out.println();
}
}

Code to concatenate two numbers' bits not working

The task is to concat the binary of 2 given numbers.
Example:
Given 5 (101) and 3 (011), the result is 46 (concat(101, 011) = 101011)
The code thus far:
public class Concat {
public static void main(String[] args) {
int t = 0;
int k = 5;
int x = 3;
int i = 0;
while (i < 3) {
t = x % 2;
x /= 2;
k <<= 1;
k |= t;
++i;
}
System.out.println(k);
}
}
But the problem is that the above code gives 101110, not 101011.
What's the problem?
Your problem is that you're feeding the bits of the second number in backwards. That's because x%2 is the low order bit:
+---+---+---+ <110
| 1 | 0 | 1 | <-----------------+^
+---+---+---+ |1
+---+---+---+ |1
| 0 | 1 | 1 | ----+0
+---+---+---+ 011>
Cringe at my awesome artistic abilities :-) However, if you already know that it's 3 bits wide, just use:
public class concat {
public static void main (String[] args) {
int t = 0;
int k = 5;
int x = 3;
k <<= 3;
k |= x;
// or, in one line: k = (k << 3) | x;
System.out.println(k);
}
}
In terms of how that looks graphically:
+---+---+---+
k:| 1 | 0 | 1 |
+---+---+---+
+---+---+---+
x:| 0 | 1 | 1 |
+---+---+---+
+---+---+---+---+---+---+
k<<=3:| 1 | 0 | 1 | 0 | 0 | 0 |
+---+---+---+---+---+---+
+---+---+---+
x:| 0 | 1 | 1 |
+---+---+---+
+---+---+---+---+---+---+
k|=3:| 1 | 0 | 1 | 0 | 1 | 1 |
+---+---+---+---+---+---+
^ ^ ^
+---+---+---+
x:| 0 | 1 | 1 |
+---+---+---+
There's no apparent reason for doing it one bit at a time.
You just shift one number and then or with the other number:
int a = 5;
int b = 3;
int c = (a << 3) | b;
I don't know what language you are using, it's almost Java, so I am going with that.
This returns the result you are asking for, though you haven't given rules for determining how you know that 3 should be 011 instead of 11.
I have made the assumption that you want to assume that both numbers have the same number of bits, so 3 is 011 because 5 requires 3 bits.
public class Concat {
public static void main(String[] args) {
System.out.println( joinNums(3,5) );
}
public static int numBits( int n ) {
return (int)Math.ceil( Math.log(n) / Math.log(2) );
}
public static int joinNums( int num1 , int num2 ) {
int bits = Math.max( numBits(num1) , numBits(num2) );
return (num1 << bits) + num2;
}
}

Categories

Resources