I need a simple java program that can generate me the custom sets for a set,say for {'1','2','3','4'}. The result should be:
{'1','2'},{'2','3'},{'3','4'},{'1','2','3'},{'2','3','4'}.
I have tried codes for powerset,but the output isn't desirable. It would be appreciable if the code could be something like:
for(j=2;j<set.size()-1;j++)
{
for(i=0;i<set.size()-1;i++)
{
//a[i],a[i+1] when j=2
//a[i],a[i+1],a[i+2] when j=3
}
}
I know .size() is for ArrayList and a[i] is for simple array and i've written both as any approach will do!! Thanks In Advance!! :)
This code should print the values you want:
final int[] values = {1, 2, 3, 4};
for (int size = 2; size < values.length; size++) {
for (int i = 0; i + size <= values.length; i++) {
for (int j = 0; j <= size - 1; j++) {
System.out.print(values[i + j]);
}
System.out.println();
}
}
From the example, we see that you want to print sets of values whose length is greater than 1 and smaller than the total set, so that 's what the following line does:
for (int size = 2; size < values.length; size++) {
After that we compute the starting index of the subset, watching not to run into a IndexArrayOutOfBounds exception (see the line below)
for (int i = 0; i + size <= values.length; i++) {
From there we just print the values starting at i index and with the subset length of size
for (int j = 0; j <= size - 1; j++)
This is the sample code which is generating the desired result:
int[] array = { 1, 2, 3, 4 };
int size = 2;
for (int j = 0; j < array.length; j++) {
for (int i = 0; i <= array.length - size; i++) {
int[] temp = Arrays.copyOfRange(array, i, i + size);
for (int x : temp) {
System.out.print(x + ",");
}
System.out.println();
}
size++;
if (size == array.length) {
break;
}
}
Output:
1,2,
2,3,
3,4,
1,2,3,
2,3,4,
Related
Is there some elegant ways to remove duplicated list (list with same values) from a list of list?
I tried with Set<List<Integer>> but it's not working, it still shows the duplicated lists. It seems that Set only checks the object reference.
the example code:
int[] nums = {-1, 0, 1, 2, -1, -4};
Set<List<Integer>> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
set.add(Arrays.asList(nums[i], nums[j], nums[k]));
}
}
}
}
then the set will contain 3 elements but two of them have the same values.
A repair:
record Triple(int x, int y, int z) {};
int[] nums = {-1, 0, 1, 2, -1, -4};
Set<Triple> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
set.add(new Triple(nums[i], nums[j], nums[k]));
}
}
}
}
However the algorithm is suboptimal O(n³).
record Triple(int x, int y, int z) {};
int[] nums = {-1, 0, 1, 2, -1, -4};
Arrays.sort(nums);
Set<Triple> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
int numI = num[i];
for (int j = i + 1; j < nums.length; j++) {
int numJ = num[j];
int numK = -(numI + numK);
int k = Arrays.binarySearch(nums, j + 1, nums.length, numK);
if (k >= 0) { // Found
set.add(new Triple(numI, numJ, numK));
}
}
}
binarySearch delivers a non-negative index when it finds the search key. When negative, it delivers the 1s complement of the "insert" position.
This has complexity O(n².log n). You determine the found resp. insert position in the for-i loop and in the for-j loop decrement k a couple of times to find the sum. That would not be difficult but less readable.
Mind the resulting numbers are ordered.
Given a matrix with m-rows and n-columns, finding the maximum sum of
elements in the matrix by removing almost one row or one column
Example:
m=2, n=3
matrix :
**[[1,2,-3]
[4,5,-6 ]
]**
output: 12 , by removing the third column then sum of elements in
[[1,2][4,5]]
How to solve this problem in java8 using dynamic programming
Based on the kadane algorithm, below code works fine
public static void main(String[] args) {
int[][] m = {
{1, 2, -3},
{4, 5, -5},
};
int N = m.length;
for (int i = 0; i < N; ++i)
m[0][i] = m[0][i];
for (int j = 1; j < N; ++j)
for (int i = 0; i < N; ++i)
m[j][i] = m[j][i] + m[j - 1][i];
int totalMaxSum = 0, sum;
for (int i = 0; i < N; ++i) {
for (int k = i; k < N; ++k) {
sum = 0;
for (int j = 0; j < N; j++) {
sum += i == 0 ? m[k][j] : m[k][j] - m[i - 1][j];
totalMaxSum = Math.max(sum, totalMaxSum);
}
}
}
System.out.println(totalMaxSum);
}
I've got the following code, I'm trying to get it to multiply array a by array b and produce array c. I've browsed several questions on here and for some reason I can't get the array to produce the correct results, I believe because it's not multiplying the right indices by one another. Any help would be greatly appreciated, as I am still trying to grasp how array multiplication works. Here is the code:
class Main
{
public static void main (String[] args)
{
int[][]a =
{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
//print array a
};
for(int i = 0; i < a[0].length-1; i++)
{
for(int j = 0; j < a[0].length; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int[][]b =
{
{-1,-2,-3},
{-4,-5,-6},
{-7,-8,-9},
{-10,-11,-12},
};
System.out.println();
//print array b
for(int i = 0; i < b.length; i++)
{
for(int j = 0; j < b.length-1; j++)
{
System.out.print(b[i][j] + " ");
}
System.out.println();
}
System.out.println();
int[][]c = new int[a.length][b[0].length];
if(b.length == a[0].length)
{
for(int i = 0; i < a.length; i++)
{
for (int j = 0; j < b[0].length; j++)
{
for (int k = 0; k < a[0].length; k++)
{
c[i][j] = a[i][k]*b[k][j];
}
}
}
}
else
{
System.out.println("Dimension requirements not satisfied for accurate calculation");
}
for(int i = 0; i < c.length; i++)
{
for(int j = 0; j < c.length; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}
Change c[i][j] = a[i][k]*b[k][j]; to c[i][j] += a[i][k]*b[k][j];
The answer by #Jeffrey Chen is correct because in the matrix multiplication each row of the first matrix multiply by each column of second matrix
In your case row 1 of matrix a is 1, 2, 3, 4
Column 1 of matrix b is -1, -4, -7, -10
So the answer is (1*-1)+(2*-4)+(3*-7)+(4*-10)
= -1-8-21-40 = -70
Then the first row of matrix a multiply by second column of matrix b
(1*-2)+(2*-5)+(3*-8)+(4*-11)
= -2-10-24-44 = -80
And so on
So the following is correct
c[i][j] += a[i][k] * b[k][j];
I have an input String, which has a size of 9 or 16 or 25... (So always a number which has an integer root.)
I need to create a 2 dimensional matrix from it, which I would like to store in a two dimensional array. I know how to store it in a one dimensional array, but I don't know how to upload the two dimensional array with the elements of the string in a correct way.
It would be something like this:
(Let's assume we have 9 characters now)
String Matrix[][] = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
But the order would be this:
1 2 3
4 5 6
7 8 9
I guess I would use two 2 for loop but not sure about it.
A good approach would be calculating the square root of the string length: that way you would find out which is the Matrix sizes, and then split up the string in the "rows" or "columns" you want for your matrix.
int matrixSize = Math.sqrt(input.length());
for(int i = 0; i<matrixSize; i++) {
for(int j = 0; j<matrixSize; j++){
matrix[i][j] = input[j];
j++;
}
}
Swap i and j in the matrix to switch rows for columns.
Hope this helps.
try this
string s="123456789";
int n=new int[3][3],p=0;
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++){
matrix[i][j] =Integer.parseInt(s[p]);
P++;
}
you may edit dimensions of 2-d array accordingly.
String input = "1,2,3,4,5,6,7,8,9";
String[] numbers = input.split(",");
int size = (int) Math.sqrt(numbers.length);
String[][] matrix = new String[size][size];
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
matrix[i][j] = numbers[i * size + j];
}
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Lets guess your input will be like :
String input="1,2,3,4,5,6,7,8,9";
Then you can do something like :
String[] inputArray=input.replace("\"", "").split(",");
for (int i = 0; i < inputArray.length; i++) {
System.out.println(inputArray[i]);
}
int matrixSize=(int) Math.ceil(Math.sqrt(inputArray.length));
int i,j;
int index=0;
for(i=0;i<matrixSize;i++)
{
for(j=0;j<matrixSize && index<inputArray.length;j++,index++)
System.out.print(inputArray[index]);
System.out.println("");
}
It will work for any number of input.
This is the assignment: Write a method that sorts the elements of a matrix with 2 dimensions. For example
sort({{1,4}{2,3}})
would return a matrix
{{1,2}{3,4}}.
I dont know what im doing wrong in my code cause the output i get is 3.0, 3.0, 4.0, 4.0.
This is what i have so far any help would be appreciated.
public static void main(String[] args) {
double[][] array = { {1, 4}, {2, 3} };
double[][] new_array = sort(array);
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length; j++) {
System.out.print(new_array[i][j] + " ");
}
}
}
public static double[][] sort(double[][] array) {
double[] storage = new double[array.length];
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length; j++) {
storage[i] = array[i][j];
}
}
storage = bubSort(storage);
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array.length; j++) {
array[i][j] = storage[i];
}
}
return array;
}
public static double[] bubSort(double[] list) {
boolean changed = true;
double temp;
do {
changed = false;
for (int j = 0; j < list.length -1; j++)
if (list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
changed = true;
}
} while (changed);
return list;
}
}
The main problem that you are experiencing is how you are copying the values from the 2d array into the 1d array. You are actually only copy two values into an array of length 2. The length of a 2d array is not the full m x n length.
I will give a small hint how you can go about copying from the 2d array into the 1d array, but it is up to you to figure out how to copy back from the 1d array into the 2d array. Also, how would you go about finding the full length of the array?
double[] storage = new double[4];//You should calculate this value
int k = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
storage[k++] = array[i][j];
}
}
Your bubble sort works fine, but then you are copying the values back wrong. Try printing the array storage after the sort and you will see that it is now correct.
You are overwriting your storage array you have it set to array[i]. Because it is in the for loop you are setting storage[0] = array[0][0] then setting storage[0] = array[0][1]. This causes you to only pick up the last number in that dimension of the array. Likewise, when you are reading them back out you are inserting the same number twice. Since 4 and 3 are the last two numbers in their respective dimensions this shows that you are sorting the array. You need a for loop for storage that is set < array.length and store your values inside that.