Tuesday, 29 December 2020

printing the matrix in spiral format in c

#include<stdio.h>
#include<string.h>
int main() 
{
    int n,i,j,r=0,co=1,c=0,nr=0,nc=0;
    scanf("%d",&n);
    int mat[n][n],nd=n;
    while(n>=1)
    {
        for(i=1;i<n;i++)
            mat[r][c++]=co++;

        for(i=1;i<n;i++)
            mat[r++][c]=co++;

        for(i=1;i<n;i++)
            mat[r][c--]=co++;

        for(i=1;i<n;i++)
            mat[r--][c]=co++;

        n=n-2;
        nr++;
        nc++;
        r=nr;
        c=nc;
    }
    if(nd%2 != 0)
        mat[--nr][--nc]=co;

    for(i=0;i<nd;i++)
    {
        for(j=0;j<nd;j++)
            printf("%3d ",mat[i][j] ); 
        printf("\n");
    }

input 

6

output:
  1   2     3   4    5     6 
 20  21  22  23  24   7 
 19  32  33  34  25   8 
 18  31  36  35  26   9 
 17  30  29  28  27  10 
 16  15  14  13  12  11

Monday, 28 December 2020

Hacker Rank Append and Delete solution in c

Problem:

https://www.hackerrank.com/challenges/append-and-delete/problem

Solution:

#include<stdio.h>

#include<string.h>

int main()
{
    char s[101],t[101];
    int k;
    scanf(" %s",s);
    scanf(" %s",t);
    scanf("%d",&k);
    int ls=strlen(s);
    int lt=strlen(t),i,ans;
    for(i=0;s[i]!='\0'&&t[i]!='\0';i++)
    {
        if(s[i]!=t[i])
            break;
    }
    ans=ls-i+lt-i;
        
    if(ans ==k||(ans < k && ans%2 == k%2)||k>=ls+lt)
        printf("Yes");
    else
        printf("No");
    
}

consider this testcase
y
yu
2
Here we can perform only one operation that is appending u
but given k is 2
so for this test case output must be No
so this is taken care by the condition
number of operations required and k must be even
as delete and append are two operations.

Also when k>=ls+lt, we can remove entire s string and append t string
as we can delete an empty string as many times as we like..so when k>=ls+lt,
output is always "YES"


Friday, 18 December 2020

John and GCD list Hacker Rank solution in C

Problem:
https://www.hackerrank.com/challenges/john-and-gcd-list/problem

Explanation:
GCD(B[1],B[2]) =A[1] that is A[1] divides B[2]
GCD(B[2],B[3]) =A[2] that is A[2] also divides B[2]

so B[2] must be divisible by both A[1] and A[2].
as LCM of A[1],A[2] will be divisible by both A[1] and A[2].
Therefore B[2] must be a multiple of LCM(A[1],A[2])
Since in the question, they wanted minimum value of B[2]
So, B[2] = LCM(A[1],A[2])


#include<stdio.h>
int gcd(int,int);
int lcm(int,int);
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i;
        scanf("%d",&n);
        int a[n];
        for(i=0 ; i<n ; i++)
            scanf("%d",&a[i]);
        printf("%d ",a[0]);
        for(i=0 ; i<n-1 ; i++)
        {
            if(a[i+1] >a[i])
                printf("%d ",lcm(a[i+1],a[i]));
            else
                printf("%d ",lcm(a[i],a[i+1]));
        }
        printf("%d\n",a[n-1]);        
    }
    return 0;
}
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int lcm(int a, int b)
{
    return (a*b)/gcd(a,b);
}

Tuesday, 15 December 2020

Sum The Series Problem Code: SUM1 codechef solution in c

https://www.codechef.com/problems/SUM1 

Explanation:



In the Question, it is mentioned to do %1000000007, to ensure the answer will always a number from 0 to 1000000006 

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;

}

Printing Pattern Using Loops Hacker Rank Solution in C

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;

}