Ways to further optimise the code for PLUS MULTIPLY (codechef) [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 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)

Related

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.

How to get all possible combination of 2xn matrix [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 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;
}

How to display sum of numbers divided by their factorial? [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 7 years ago.
Improve this question
I'm a beginner and I am having a really hard time while doing this program.
The question is:
(1/1!)+(2/2!)+(3/3!)+(4/4!)- - - -n
So here are the n number of terms(in which a number is divided by its factorial) and I have to display the output of the sum of any number of terms which are given in scanf function.
Only one thing I know is that this program can be done by using "Nested for" loop but I haven't perfect grip yet on C language. So you guys have to have help me out in this. :)
#include <stdio.h>
#include <conio.h>
void main(void){
int s,a,b,n,fact=1;
//clrscr();
printf("Enter number of terms=");
scanf("%d",&n);
for(a=1;a<=n;a++) {
fact=fact*a;
b=(a/fact);
printf("Sum=%d",s);
}
getche();
}
P.S It's must for me to do it with "Nested for" loop.
No you do not need any Nested for loops to solve your problem. Here's a procedure you may follow:
function factorial
Input: numbers L.
Output: factorial of L.
function sum
Input: n.
Output: sum.
sum = 0;
for i = 1 to n, do
sum ← sum + (i / factorial(i))
return sum
#include <stdio.h>
int main(void) {
// your code goes here
int n;
float sum = 0,d,fact =1,j,i;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++){
fact = 1;
for (j = 1; j <= i; j++){
fact = fact * j;
}
d = (float) i / (float) fact ;
sum = sum + d;
}
printf("sum = %f", sum);
return 0;
}
Its working ..you can check over here :-https://ideone.com/JVXQVX

Stone codechef: What is wrong with the code? [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 7 years ago.
Improve this question
I am new to coding and finding my way!! I am solving a problem at codechef http://www.codechef.com/problems/RRSTONE.
All tests cases are passed at my side but the answer is still wrong. Any Help???
//codechef stones
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long int a[size], max;
int i,j;
for(i=0;i<size;i++)
scanf("%lu",&a[i]);
i=j=0;
if(k==0)
goto printing;
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=0;i<size-1;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
printing: //removed this comment
for(i=0;i<size-1;i++)
printf("%d ",a[i]);
printf("%d",a[i]);
return 0;
}
I suggest you should look at the criteria properly.
Constraints
1 <= N <= 105
0 <= K <= 109
Ai does not exceed 2 * 10^9 by it's absolute value.
Here is modified code try with this.
And one more thing: Since you are learning understand the code first.
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long long int a[size], max; //CHANGE HERE!!!! and so forth for long long
int i,j;
for(i=0;i<size;i++)
scanf("%lld",&a[i]);
i=j=0;
if(k!=0)
{
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=1;i<size;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
}
for(i=0;i<size;i++)
printf("%lld ",a[i]);
return 0;
}
EDIT: Sorry I forgot to put this note. There is issue sometimes with goto when you are using online compilers, so better to try with a clean approach and make sure you are selecting the correct option for source language (C-gcc or C99 whatever you're using) . Earlier I have experienced the same.

Spoj LastDigit Wrong answer [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 5 years ago.
Improve this question
Link to the problem
Spoj LastDigit problem
I have tested my code on my machine but in SPOJ it is showing wrong answer.
Here is my code-
#include<stdio.h>
int main()
{
int num=0;
scanf("%d",&num);
while(num--)
{
int a=0;
unsigned long int b;
scanf("%d\t%lu",&a,&b);
a%=10;
b%=100;
if(a==0||a==1)
printf("%d\n",a);
else if(b==0)
printf("1\n");
else if(a==5)
printf("5\n");
else
{
int d=b%4,e=1;
while(d--)
e*=a;
printf("%d\n",e%10);
}
}
return 0;
}
Your program is buggy. You are assuming cycle of length 4 for all digits except 0,1,5. That's incorrect
For instance, consider the input1
2 4
Your program outputs 1 whereas the answer should be last digit of Power(2,4) = last digit of 16 = 6
In SPOJ you get a WA because your program is used to calculate the last digit of not so big number. In the problem, it is clearly mentioned that the value of 'b' lies between '0' and '2,147,483,000' inclusive.
The correct way to solve the problem is by using Modular Exponentiation.
# Wasim Thabraze I used the modular approach but my solution is not accepted because
my solution is taking more than than 700bytes
#include<iostream>
using namespace std;
int lastdigit(int a,long int b);
int main()
{
int t,a;
long int b;
cin>>t;
while(t--)
{
cin>>a>>b;
cout<<lastdigit(a,b)<<"\n";
}
return 0;
}
int lastdigit(int a,long int b)
{ if(b==0){return 1;}
if(a%10==0){return 0;}
if(a%10==1){return 1;}
if(a%10==5){return 5;}
if(a%10==6){return 6;}
int ret,mod=b%4;
if(a%10==2||a%10==8||a%10==4)
{
if(a%10==8){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==4){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 2;}
else if(mod==2){ret =4;}
else if(mod==3){ret =8;}
else if(mod==0){ret= 6;}
}
else if(a%10==3||a%10==7||a%10==9)
{
if(a%10==7){if(mod==1){mod=3;}else if(mod==3){mod=1;}}
if(a%10==9){if(mod==1||mod==3){mod=2;}else if(mod==2){mod=0;}}
if(mod==1){ret= 3;}
else if(mod==2){ret= 9;}
else if(mod==3){ret= 7;}
else if(mod==0){ret= 1;}
}
return ret;
}

Resources