Analyze the time complexity of below function
Q1)
Q2)
Q3)
Q4)
(maintained by Lakshmi Sarvani Videla, Former Assistant Professor, KLEF and now Computer Science Lecturer, SRR and CVR Government Degree College, Vijayawada
Analyze the time complexity of below function
Q1)
Q2)
Q3)
Q4)
input
6
Problem:
https://www.hackerrank.com/challenges/append-and-delete/problem
Solution:
#include<stdio.h>
#include<string.h>
https://www.codechef.com/problems/SUM1
Explanation:
As n can take a value upto 10^16 in the question and n^2 cannot fit in even long long int, we cannot do n*n and then apply %1000000007
So we cannot do (n*n)%1000000007 so we do like this
((n%1000000007)*(n%1000000007))%1000000007
According to modular arithmetic (a*b)%m = ((a%m)*(b%m))%m
Here is the solution:
#include<stdio.h>
#define MOD 1000000007
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long int n;
scanf("%lld",&n);
printf("%lld\n",(((n%MOD)*(n%MOD))%MOD));
}
return 0;
}
https://www.hackerrank.com/challenges/printing-pattern-2
Solution
#include <stdio.h>
int main()
{
int n,d,min;
scanf("%d", &n);
d=2*n-1;
for(int i=1;i<=d;i++)
{
for(int j=1;j<=d;j++)
{
if(i<j)
min=i-1;
else
min=j-1;
if(min>d-j)
min=d-j;
if(min>d-i)
min=d-i;
printf("%d ",n-min);
}
printf("\n");
}
return 0;
}