How to get all possible combination of 2xn matrix [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I was unable to solve this problem, tried much but logic not working.
Problem is,
I have to calculate all possible combinations of 2 x N matrix.
conditions:
sum of all elements must be N.
elements in a row or a column must be in non increasing way.
all elements must be positive and real numbers.
print all possible combinations.
let for 6, it is 29
thanks.

This problem is already asked,
you can use backtracking to solve it
here's the code,
#include<conio.h>
#include<stdio.h>
int a[2][100],c,sum,num;
int ch;
int check(int x,int y)
{
int i=1;
if(x==1&&a[x][y]>a[0][y])
i=0;
if(y>0&&a[x][y]>a[x][y-1])
i=0;
return i;
}
void print()
{
int i,j;
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<num;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
void fun(int lim,int x,int y)
{
int i;
if(y<num)
for(i=lim;i>0;i--)
{
a[x][y]=i;
if(check(x,y))
{
sum+=a[x][y];
if(sum==num)
{
print();
sum-=a[x][y];
a[x][y]=0;
c++;
}
else
{
fun(num-sum,(x+1)%2,y+(x+1)/2);
a[(x+1)%2][y+(x+1)/2]=0;
fun(num-sum,(x+2)%2,y+(x+2)/2);
a[(x+2)%2][y+(x+2)/2]=0;
}
sum-=a[x][y];
}
}
}
int main()
{
scanf("%d",&num);//num=6
fun(num,0,0);
printf("%d",c);
return 0;
}

Related

I tried wiritng a code to calculate the mean of sopfr(x), but it doesnt work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
sopfr
I tried making a code that calculates the mean from sopfr(c) up to sofr(c+i), but when I input the numbers nothing happens
This is the code ive done
#include <stdio.h>
int sopfr(int x);
int main()
{
int c ,i ,sum1=0, a=0;
scanf("%i",&c);
scanf("i",&i);
for(c;c<=c+i;a++)
{
sum1+=sopfr(c);
c+=a;
}
float sum2=sum1/i;
printf("%f",sum2);
return 727;
}
int sopfr(x)
{
int t, n=2, s=0;
scanf("%i",&t);
while(t!=1)
{
if(t%n==0)
{
t/=n;
s+=n;
continue;
}
n++;
}
return s;
}
Remove the scanf() call and make t the function parameters.
int sopfr(int t)
{
int n=2, s=0;
while(t!=1)
{
if(t%n==0)
{
t/=n;
s+=n;
continue;
}
n++;
}
return s;
}

Ways to further optimise the code for PLUS MULTIPLY (codechef) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
While solving the code for the problem Plus Multiply on CodeChef, I tried to solve it using binary search, but my solution gives a TLE.
I have written the following code:
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
for(int a=0;a<t;a++)
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int s=0,j,k,f,m,mm;
(n%2==0)?(mm=n/2+1):(mm=n/2+1);
for(j=0;j<mm;j++)
{
f=n-j-1;
for(k=j+1;k<n-1;k++)
{
m=n-k-1;
if((arr[j]*arr[k])==(arr[j]+arr[k]))
s++;
if((arr[f]*arr[m])==(arr[f]+arr[m]))
s++;
}
if((arr[j]*arr[k])==(arr[j]+arr[k]))
s++;
}
printf("%d\n",s);
}
return 0;
}
Kindly suggest me on how to improve the time complexity of the above code.
As per your solution, the code looks to me as of complexity O(n^2).
But, as per the given constraints, the question expects me to solve it in O(n) time complexity.
The property, ab = a+b is special and exclusive only to 0 and 2.
Therefore, just by finding the number of pairs of 0's and 2's.
And it can be calculated using combinations.
Have a look at the following code:
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
LL z = 0; //Zero count
LL t = 0; //One count
while(n--){
int x;
cin>>x;
if(x==0){
z++;
}
if(x==2){
t++;
}
}
cout<<((z)*(z-1LL)/2LL) + ((t)*(t-1LL)/2LL)<<endl;
}
return 0;
}
Time Complexity: O(N)

Do not understand memory surfing in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <array.h>
int col, str;
int *point;
void setArr()
{
printf("Input columns="); scanf("%d", &col);
printf("Input strings="); scanf("%d", &str);
int num[str][col];
for(int i = 0; i < str; ++i)
{
for(int j = 0; j < col; ++j)
{
scanf("%d", &num[i][j];
}
}
point = num;
}
int main(void)
{
setArr();
printf("First=%d\n", *point);
printf("Number=%d", *point);
}
Output:
Input columns=2
Input strings=2
1
2
3
4
First=1
Number=1740639104
Here we have code in C, that have to get exact number from array using pointer, but during many attempts I understand that there is something I do not understand or just do not know.So there is a problem(or it have to be like this), namely, I refer to pointer ,which points on first element two times and I get different results in each case. Why it happened and in which way I could solve it? Thanks,everyone.
With
point = num;
you are setting point to an address of a function local variable. All further access of that will be undefined behaviour.

C - Cannot iterate through second string onward [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Can anyone tell what I am doing wrong here?
Problem statement:
https://practice.geeksforgeeks.org/problems/good-or-bad-string/0
My code:
#include <stdio.h>
#include<string.h>
int is_vowel(char a) {
if(a==97||a==101||a==105||a==111||a==117){
return(1);
}
return(0);
}
int main() {
//code
int t,i;
scanf("%d",&t);
for(i=0;i<t;i++){
char str[100];
scanf("%s",str);
printf("%s",str);
int c_cnsnt=0;
int c_vwl=0;
int g_b=1;//suppose good
for(int j=0;j<strlen(str);j++){
//("%c",str[j]);
int num=is_vowel(str[j]);
printf("Debug %c %d %d\n",str[j],num,strlen(str));
if(is_vowel(str[j])) {
c_vwl++;
}
else { c_cnsnt++;}
if(c_vwl==c_cnsnt){
c_cnsnt=0;
c_vwl=0;
}
else {
if(c_vwl>5||c_cnsnt>=3){
g_b=0;
break;
}
}
}
printf("%d\n",g_b);
}
return 0;
}
Sample
Input:
2
aeioup??
bcdaeiou??
Output:
1
0
My solution link:
https://code.hackerearth.com/9bca55K
Why does the for loop not work for the 2nd string?
Hint: You have to clear the the consonant and vowel counts after increment the other (e.g {c_vwl++;c_cnsnt=0;}), not when they are equal, and always tests your BAD condition.
I will not give you a sample code. Good luck

array that accepts a number only once(code doesn't work) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to make an array that accepts each number just once and if the user tries to insert a number more than once,then he must enter an other number...can anyone help me please?
I have tried this so far:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[5];
int i,j,num;
scanf("%d",&num);
a[0]=num;
for(i=1;i<5;++i){
again: scanf("%d",&num);
if(a[i]!=a[i-1])
a[i]=num;
else
goto again;
}
for(i=0;i<5;i++)
printf("%4d\n",a[i]);
system("pause");
return 0;
}
but the code just doesn't work and i don't know why
it is observed from your code that given array must be filled but it should not contain redundant values.
following code iterates until the array is filled with no redundant value, once the array is filled it terminates.
int a[5],i=1,k=0,p;
int num;
scanf("%d",&num);
a[0]=num;
while(i<5)
{
scanf("%d",&num);
for(p=0;p<=k;p++)
{
if(a[p]==num)
{
break;
}
if(p==(k))
{
a[i]=num;
k=i;
i++;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
hope this could help you
You are just comparing the new entered value with the previous one in a[i]!=a[i-1].
You better create a function to test the new value with the entire array, like
int valueExists(int num, int a[], int len) {
int i;
for (i = 0; i < len; i++) {
if (a[i] == num) {
return 1;
}
}
return 0;
}
Make sure you adding the new value to the array only after testing the value is not there.
again: scanf("%d",&num);
if (valueExists(num, a, i)) {
goto again;
} else {
a[i] = num;
}
(The loop you created with the goto can be replaced by a do-while loop, but that is not the issue here)

Resources