In the code attached, how do I modify it to remove Remove the trailing '+' signs.
int i,j,sum;
sum=1;
for(i=2; i<=10; i++) {
for(j=1; j<(i+1); j++) {
sum = sum + 1;
printf("%d + ",j);
}
printf(" = %d", sum);
printf("\n");
}
return EXIT_SUCCESS;
}
Here is the output:
1 + 2 + = 3
1 + 2 + 3 + = 6
1 + 2 + 3 + 4 + = 10
1 + 2 + 3 + 4 + 5 + = 15
1 + 2 + 3 + 4 + 5 + 6 + = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 + = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + = 45
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + = 55
For example you can do it the following way
for(j=1; j<(i+1); j++) {
sum = sum + 1;
if ( j != 1 ) printf( " + " );
printf("%d",j);
}
You can't 'remove' output; you have to avoid generating it.
One way is to use:
for (int i = 2; i <= 10; i++)
{
int sum = 0;
const char *pad = "";
for (int j = 1; j <= i; j++)
{
sum += j;
printf("%s%d", pad, j);
pad = " + ";
}
printf(" = %d\n", sum);
}
Note that this recalculates sum more directly, setting it to zero before the inner loop. It also minimizes the scope of the variables.
You can set and print the initial value in the outer loop. For my opinion also make it more readable.
Furthermore you can use j instead of sum+1 for the addend
for (int i = 2; i <= 10; i++) {
int sum = 1;
printf("%d", sum);
for (int j = 2; j<(i + 1); j++) {
sum += j;
printf(" + %d", j);
}
printf(" = %d", sum);
printf("\n");
}
I have a 2d array and i want to sort the 1d arrays in descending order. I dont think i can properly explain so here's an example.
I want to turn this:
3 5 6 7 8
1 2 4 9 10
1 3 5 6 10
Into this:
1 2 4 9 10
1 3 5 6 10
3 5 6 7 8
but i just cant get it right. Any help is appreciated, thanks in advance.
intAlloc function just does a malloc and a check for if the allocation went right.
cpyArray coppies whatever is on the second arguement to the first and ANA is the number of elements the array in the first arguement has.
int **sortArray2(int **arr,int r,int c)
{
int k,i,j,tmp;
int *ptmp;
ptmp = intAlloc(ANA);
//this part is just for sorting the numbers in each 1d array
for(i=0; i<r; i++)
{
for(j=0; j<c-1; j++)
{
for(k=j+1; k<c; k++)
{
if(*(*(arr + i) + j) > *(*(arr + i)+ k))
{
tmp = *(*(arr + i) + j);
*(*(arr + i) + j) = *(*(arr + i)+ k);
*(*(arr + i)+ k) = tmp;
}
}
}
}
//this is the part where im trying to do what i explain
for(i=0; i<r-1; i++)
{
for(j=0; j<c; j++)
{
if(*(*(arr + i) + j) > *(*(arr + i+1)+ j))
{
cpyArray(ptmp, *(arr + i), ANA);
cpyArray(*(arr + i), *(arr + i+1), ANA);
cpyArray(*(arr + i),ptmp ,ANA);
break;
}
}
}
free(ptmp);
return arr;
}
How can I draw the function x+y=n with n not greater than 10 and the coordinate system using only matrices without any functions or libraries? I am a beginner and I hope you could help me.The only problem is that my code prints spaces above the line x+y=n and it should not.
#include <stdio.h>
int
main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for(i=1;i<n+1;i++){
if((10-n+i)!=10 && (2+3*i)!=1)
m[10-n+i][2+3*i]='*';
}
m[0][0]='1';
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
//n=5
10+
9 +
8 +
7 +
6 +
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + + + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
So first of all its quite difficult for me to understand your logic behind the if statements in the for loop.
With that said, to get the desired 10+ on the y-axis i set m[0][0] = '1'; which seems to work.
As for drawing the line, it seems that:
if (j == 3 * (n + i - 10) && i > 2 && j > 2) m[i][j] = '*';
was causing the issues. I replaced it with:
m[10 - n][2] = '*';
for (i = 1; i < n+1; i++)
{
m[10 - n + i][2 + 3 * i] = '*';
}
and it works fine and in a more understandable way hopefully!
So in the end the code should look like this:
#include<stdio.h>
int main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1) m[i][j] = '0';
if (i == 11 && j == 61) m[i][j] = '2';
if (j == 0 && i != 0 && i != 11) m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11) m[i][j] = '+';
if (i == 11 && j % 3 == 2) m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59) m[i][j] = '1';
if (i == 10 && j % 3 == 2) m[i][j] = '+';
// if (j == 3 * (n + i - 10) && i > 2 && j > 2) m[i][j] = '*';
}
}
m[10 - n][2] = '*'; // sets + on y-axis to *
for (i = 1; i < n+1; i++)
{
m[10 - n + i][2 + 3 * i] = '*'; // draws * diagonally jumping 3 characters (thus 3*i)
}
m[0][0] = '1'; // to have 10 instead of 0 at the top left
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
} printf("\n");
}
}
For n=7 for example you get the desired:
10+
9 +
8 +
7 *
6 + *
5 + *
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + * + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
To not delete the + by the * just offset the line to the right:
m[10 - n][3] = '*';
for (i = 1; i < n+1 ; i++)
{
m[10 - n + i ][3 + 3 * i] = '*'; // draws * diagonally jumping 3 characters (thus 3*i)
}
m[0][0] = '1'; // to have 10 instead of 0 at the top left
So the n=7 example should now look:
10+
9 +
8 +
7 +*
6 + *
5 + *
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + +* + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
In order to not print chars above or after the line try this:
#include <stdio.h>
int main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for (i = 1; i < n + 1; i++) {
if ((10 - n + i) != 10 && (2 + 3 * i) != 1)
m[10 - n + i][2 + 3 * i] = '*';
}
m[0][0] = '1';
for (i = 0; i < 12; i++)
{
for (j = 0; j < 63; j++)
{
if ((i < 10 - n) || (j > 2 + 3 * n))
{
m[i][j] = '\0';
}
}
}
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
and for n=5 you get:
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + +
0 1 2 3 4 5
Hope this helps!
My task is to draw a coordinate system and line y+x=n, for n<=10.My code gives the correct result, but there is a condition that spaces or any other signs cannot be drawn after the line x+y=n. That is the only problem I have in solving this task and I hope you could help. I am a beginner.
(The only problem is that my code prints spaces above the line x+y=n and it should not)
#include <stdio.h>
int
main()
{
int n, i, j;
scanf("%d", &n);
char m[12][63] = { " " };
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
m[i][j] = ' ';
if (i == 0 && j == 1)
m[i][j] = '0';
if (i == 11 && j == 61)
m[i][j] = '2';
if (j == 0 && i != 0 && i != 11)
m[i][j] = '0' + 10 - i;
if (j == 2 && i != 11)
m[i][j] = '+';
if (i == 11 && j % 3 == 2)
m[i][j] = '0' + ((j - 2) / 3) % 10;
if (i == 11 && j % 3 == 1 && j > 29 && j < 59)
m[i][j] = '1';
if (i == 10 && j % 3 == 2)
m[i][j] = '+';
}
}
for(i=1;i<n+1;i++){
if((10-n+i)!=10 && (2+3*i)!=1)
m[10-n+i][2+3*i]='*';
}
m[0][0]='1';
for (i = 0; i < 12; i++) {
for (j = 0; j < 63; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
}
//n=5
10+
9 +
8 +
7 +
6 +
5 +
4 + *
3 + *
2 + *
1 + *
0 + + + + + + + + + + + + + + + + + + + + +
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sounds like you want to not print the trailing spaces on each line. So you need to find and skip those trailing spaces rather than printing them:
for (i = 0; i < 12; i++) {
int eol = 63;
while (eol > 0 && m[i][eol-1] == ' ') --eol;
for (j = 0; j < eol; j++) {
printf("%c", m[i][j]);
}
printf("\n");
}
Problem Statement:
Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum.
Sample Inputs,
[1,6,5,11] => 1. The 2 subsets are {1,5,6} and {11} with sums being 12 and 11. Hence answer is 1.
[36,7,46,40] => 23. The 2 subsets are {7,46} and {36,40} with sums being 53 and 76. Hence answer is 23.
Constraints
1 <= size of array <= 50
1 <= a[i] <= 50
My Effort:
int someFunction(int n, int *arr) {
qsort(arr, n, sizeof(int), compare);// sorted it for simplicity
int i, j;
int dp[55][3000]; // sum of the array won't go beyond 3000 and size of array is less than or equal to 50(for the rows)
// initialize
for (i = 0; i < 55; ++i) {
for (j = 0; j < 3000; ++j)
dp[i][j] = 0;
}
int sum = 0;
for (i = 0; i < n; ++i)
sum += arr[i];
for (i = 0; i < n; ++i) {
for (j = 0; j <= sum; ++j) {
dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
if (j >= arr[i])
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], arr[i] + dp[i][j + 1 - arr[i]]);
}
}
for (i = 0; i < n; ++i) {
for (j = 0; j <= sum; ++j)
printf("%d ", dp[i + 1][j + 1]);
printf("\n");
}
return 0;// irrelevant for now as I am yet to understand what to do next to get the minimum.
}
OUTPUT
Let's say for input [1,5,6,11], I am getting the dp array output as below.
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
0 1 1 1 1 5 6 7 7 7 7 11 12 12 12 12 12 12 12 12 12 12 12 12
0 1 1 1 1 5 6 7 7 7 7 11 12 12 12 12 16 17 18 18 18 18 22 23
Now, how to decide the 2 subsets to get the minimum?
P.S - I have already seen this link but explanation is not good enough for a DP beginner like me.
You have to solve subset sum problem for SumValue = OverallSum / 2
Note that you don't need to solve any optimization problem (as using max operation in your code reveals).
Just fill linear table (1D array A) of size (SumValue + 1) with possible sums, get the closest to the last cell non-zero result (scan A backward) wint index M and calculate final result as abs(OverallSum - M - M).
To start, set 0-th entry to 1.
Then for every source array item D[i] scan array A from the end to beginning:
A[0] = 1;
for (i = 0; i < D.Length(); i++)
{
for (j = SumValue; j >= D[i]; j--)
{
if (A[j - D[i]] == 1)
// we can compose sum j from D[i] and previously made sum
A[j] = 1;
}
}
For example D = [1,6,5,11] you have SumValue = 12, make array A[13], and calculate possible sums
A array after filling: [0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1]
working Python code:
def besthalf(d):
s = sum(d)
half = s // 2
a = [1] + [0] * half
for v in d:
for j in range(half, v - 1, -1):
if (a[j -v] == 1):
a[j] = 1
for j in range(half, 0, -1):
if (a[j] == 1):
m = j
break
return(s - 2 * m)
print(besthalf([1,5,6,11]))
print(besthalf([1,1,1,50]))
>>1
>>47
I'll convert this problem to subset sum problem
let's take array int[] A = { 10,20,15,5,25,33 };
it should be divided into {25 20 10} and { 33 20 } and answer is 55-53=2
Notations : SUM == sum of whole array
sum1 == sum of subset1
sum2 == sum of subset1
step 1: get sum of whole array SUM=108
step 2: whichever way we divide our array into two part one thing will remain true
sum1+ sum2= SUM
step 3: if our intention is to get minimum sum difference then
sum1 and sum2 should be near SUM/2 (example sum1=54 and sum2=54 then diff=0 )
steon 4: let's try combinations
sum1 = 54 AND sum2 = 54 (not possible to divide like this)
sum1 = 55 AND sum2 = 53 (possible and our solution, should break here)
sum1 = 56 AND sum2 = 52
sum1 = 57 AND sum2 = 51 .......so on
pseudo code
SUM=Array.sum();
sum1 = SUM/2;
sum2 = SUM-sum1;
while(true){
if(subSetSuMProblem(A,sum1) && subSetSuMProblem(A,sum2){
print "possible"
break;
}
else{
sum1++;
sum2--;
}
}
Java code for the same
import java.util.ArrayList;
import java.util.List;
public class MinimumSumSubsetPrint {
public static void main(String[] args) {
int[] A = {10, 20, 15, 5, 25, 32};
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
subsetSumDynamic(A, sum);
}
private static boolean subsetSumDynamic(int[] A, int sum) {
int n = A.length;
boolean[][] T = new boolean[n + 1][sum + 1];
// sum2[0][0]=true;
for (int i = 0; i <= n; i++) {
T[i][0] = true;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (A[i - 1] > j) {
T[i][j] = T[i - 1][j];
} else {
T[i][j] = T[i - 1][j] || T[i - 1][j - A[i - 1]];
}
}
}
int sum1 = sum / 2;
int sum2 = sum - sum1;
while (true) {
if (T[n][sum1] && T[n][sum2]) {
printSubsets(T, sum1, n, A);
printSubsets(T, sum2, n, A);
break;
} else {
sum1 = sum1 - 1;
sum2 = sum - sum1;
System.out.println(sum1 + ":" + sum2);
}
}
return T[n][sum];
}
private static void printSubsets(boolean[][] T, int sum, int n, int[] A) {
List<Integer> sumvals = new ArrayList<Integer>();
int i = n;
int j = sum;
while (i > 0 && j > 0) {
if (T[i][j] == T[i - 1][j]) {
i--;
} else {
sumvals.add(A[i - 1]);
j = j - A[i - 1];
i--;
}
}
System.out.println();
for (int p : sumvals) {
System.out.print(p + " ");
}
System.out.println();
}
}
Working Java code if anyone is interested but the idea remains the same as what #MBo has answered
class Solution{
public int minDifference(int arr[]) {
int sum = 0;
for(int x : arr) sum += x;
int half = (sum >> 1) + (sum & 1);
boolean[] sums = new boolean[half + 1];
sums[0] = true;
for(int i = 0; i < arr.length; ++i){
if(arr[i] > half) continue;
for(int j = half; j >= arr[i]; --j){
if(sums[j - arr[i]]){
sums[j] = true;
}
}
}
for(int i = sums.length - 1; i >= 1; --i){
if(sums[i]) return Math.abs((sum - i) - i);
}
return sum; // for arrays like [2] or [100] etc
}
}