Hack Solutions


Pattern - Star Pyramid

C program to print following pyramid pattern of stars n=5
     *
  * * *
* * * * *
Sample Input 1 5 Sample Output 1
  * 
* * * 
* * * * *
Input Format
Input contains n
Constraints
1 <= n <= 20
Output Format
Print the pattern
Sample Input 0
5
Sample Output 0
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int n,i,j,k; scanf("%d",&n); for(i=0;i<n;i++) { for(k=0;k<n-1-i;k++) printf(" "); for(j=0;j<=i;j++) printf("* "); for(j=0;j<i;j++) printf("* "); printf("\n"); } return 0; } Run Code 

Compete Cell
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day, otherwise it becomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumed to be always inactive. Even after updating the cell state. consider its previous state for updating the state of other cells. Update the cell information of all cells simultaneously. Write a function cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing the number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
Sample Input 1 1 0 0 0 0 1 0 0 1 Sample Output 1 0 1 0 0 1 0 1 0
Input Format
Input will have 8 array values and the no of days
Constraints
array size is 8 integers
Output Format
print the array

Sample Input 0
1 0 0 0 0 1 0 0
1
Sample Output 0
0 1 0 0 1 0 1 0 
#include <math.h>
#include <stdio.h>
int main(){
    int i,j,k,n,arr[8];
    for(i=0;i<8;i++)
        scanf("%d",&arr[i]);
    scanf("%d",&n);
    int arr1[8];
    for(i=0;i<n;i++)
    {
        if(arr[1]==0) arr1[0]=0;
        if(arr[1]==1) arr1[0]=1;
        if(arr[6]==0) arr1[7]=0;
        if(arr[6]==1) arr1[7]=1;
        for(k=1;k<=6;k++) 
        {
            if(arr[k-1]==arr[k+1])
                arr1[k]=0;
            else 
                arr1[k]=1;
        } 
        for(k=0;k<8;k++)
            arr[k]=arr1[k];
    }
    for(i=0;i<8;i++)
        printf("%d ",arr[i]); 
    return 0;
}                                                          Run Code
Eliminate Repeated Elements
Given two positive integer arrays ary1 and arr2 of lengths len1 and len2 respectively. write a program to count the number of elements which are not common In the arrays.
The input to the function distinctElementCount of two arrays arr1 and arr2 and their lengths len1 and len2 respectively.
The function return the number of elements which are not common in both arrays.
Example.
arr1 = {1, 2,3, 4, 5, 6, 7, 8, 9, 10}, lent = 10
arr2 = {11, 12, 13, 4, 5, 6, 7, 18, 19, 20}, len2 = 10
The distinct elements are 1, 2, 3, 8, 9, 10, 11, 12, 13, 18, 19 and 20 so the function should return 12.
Sample Input 1 5 6 34 89 12 45 93 12 93 45 23 78 35 
Sample Output 1 5
Input Format
Input contains the length of the arrays and the values
Constraints
1 ≤ n ≤105
1 ≤ values ≤ 109
Output Format
Print the count
Sample Input 0
5 6
34 89 12 45 93
12 93 45 23 78 35
Sample Output 0
5
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
      
    int l1,l2,i,cnt=0;
    scanf("%d %d",&l1,&l2);
    
    int a[105],b[110];
    for(i=0;i<l1;i++)    
        scanf("%d",&a[i]);    
    for(i=0;i<l2;i++)
        scanf("%d",&b[i]);

    for(i=0;i<l1;i++)
        for(int j=0;j<l2;j++)
        {
          if(a[i]==b[j])
          {
              a[i]=-1;
              b[j]=-1;
          }
        }
     for(i=0;i<l1;i++)
         if(a[i]!=-1)
             cnt++;
     for(i=0;i<l2;i++)
         if(b[i]!=-1)
             cnt++;
    printf("%d",cnt);
}                                                          Run Code
GCD Of Two Numbers
Write a 'C' program to find the GCD of two numbers
Sample Input 1 28690 5126 Sample Output 1 2
Input Format
Input contains two integers separated by space
Constraints
1 ≤ n1,n2 ≤ 105
Output Format
print the GCD of two numbers

Sample Input 0
28690 5126
Sample Output 0
2
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int num1, num2; 
    scanf("%d %d", &num1, &num2);
    while(num1!=num2)
    {
    if (num1 > num2)    
        num1 = num1-num2;
    else    
        num2 = num2-num1; 
    }
    printf("%d ",num1);
    return 0;
}                                                                                            Run Code
Less than Key element
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

     int n,num,cnt=0;
    scanf("%d %d",&n,&num);
    int a[1000];
    for(int i=0;i<n;i++)
    {
            scanf("%d ",&a[i]);
         
    }
     for(int i=0;i<n;i++)
    {
         if(a[i]<num)
         {
cnt++;         }
    }
    printf("%d",cnt);
    return 0;
}                                                                               Run Code
4
Matrix Rotation
Write a program to rotate a matrix in 90 degree.
Case 1:
Flag = 1
Input:
2 3 1
4 6 3
5 4 2
Output: 5 4 2
4 6 3
2 3 1

Case 2:
Flag = 0
Input:
2 1
3 4
Output:
4 1
3 2
Sample Input 1 3 3 1 2 3 1 4 6 3 5 4 2 Sample Output 1 5 4 2 4 6 3 2 3 1
Input Format
Input contains rowCount , columnCount, flag value and the array value
Constraints
1 ≤ array_size ≤ 1000
Output Format
print the rotated array
Sample Input 0
3 3 1
2 3 1
4 6 3
5 4 2
Sample Output 0
5 4 2 
4 6 3 
2 3 1             
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int n1,n2,i,j,a[1000][1000],flag;
    scanf("%d %d %d",&n1,&n2,&flag);
    for(i=0;i<n1;i++)
        for(j=0;j<n2;j++)
            scanf("%d",&a[i][j]);
            
    if(flag==1)
    {
     for(i=0;i<n1;i++){
        for(j=n2-1;j>=0;j--){
            printf("%d ",a[j][i]);
        }
         printf("\n");
     }
    }
    if(flag==0)
    {
     for(i=n1-1;i>=0;i--){
        for(j=0;j<n1;j++) {       
            printf("%d ",a[j][i]); 
        }
         printf("\n");
     }
     }
    return 0;
}
                                                                                        Run Code
Increment matrix
You are given an initial value as s and dimensions of the increment matrix as m and n.
An increment matrix is the matrix whose elements are the incremented values of the initial
value s. N
For example -
if initial value s = 1 and dimesions are: m=3,n=3
Increment Matrix would be:
1 2 3
4 5 6
7 8 9
Multiply the original increment matrix with its transpose.
The input to the method transposeMultMatrix shall consist of
the initial value s and the dimensions of the increment matrix m and n (s, m and n all
should be positive integers).
The method should return a 2-dimesional matrix for the multiplication matrix.
Sample Input 1 1 3 3 Sample Output 1 14 32 50 32 77 122 50 122 194 Sample Input 2 4 3 2 Sample Output 2 41 59 77 59 85 111 77 111 145
Input Format
Input contains rowCount,colCount and the array values
Constraints
1 ≤ array_size ≤ 1000
Output Format
Print the array

Sample Input 0
3 3
1 2 3
4 5 6
7 8 9
Sample Output 0
1 4 7 
2 5 8 
3 6 9    
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n1,n2;
    scanf("%d %d",&n1,&n2);
    int a[n1][n2];
    for(int i=0;i<n1;i++)
    {
        for(int j=0;j<n2;j++)
        {
            scanf("%d ",&a[i][j]);
        }
    }
     for(int i=0;i<n2;i++)
    {
        for(int j=0;j<n1;j++)
        {
            printf("%d ",a[j][i]);
        }
         printf("\n");
    }
    return 0;
}                                                       Run Code
Merge Linked Lists
Merge two sorted singly linked lists into one sorted list
Sample Input 1 22 15 6 5 27 1 8 18 42 16 2 -1 28 25 41 12 22 4 38 26 30 24 11 31 -1 Sample Output 1 1 2 4 5 6 8 11 12 15 16 18 22 22 24 25 26 27 28 30 31 38 41 42

Input Format
Given two lists , -1 denotes the end
Constraints
nil
Output Format
Print the list

Sample Input 0
22 15 6 5 27 1 8 18 42 16 2  -1
28 25 41 12 22 4 38 26 30 24 11 31 -1
Sample Output 0
1 2 4 5 6 8 11 12 15 16 18 22 22 24 25 26 27 28 30 31 38 41 42  
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int a[1000],b[1000],z,d;
    for(int i=0;i<1000;i++)
    {
        scanf("%d",&z);
            if(z!=-1)
            {
                a[i]=z;
                d=i;
            }
        else
            i=1000;
    }
    for(int i=d+1;i<1000;i++)
    {
        scanf("%d",&z);
            if(z!=-1)
            {
                a[i]=z;
                d=i;
            }
          else
            i=1000;
    }
    for(int i=0;i<=d;i++)
    {
        for(int j=i;j<=d;j++)
        {
            if(a[i]>a[j])
            {
                int t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
            
        }
    }
    for(int i=0;i<=d;i++)
        printf("%d ",a[i]);
    return 0;
}                                                          Run Code
Merge Sort Using Pointers
Write a program to merge sort using pointers
Apply merge sort to sort the values
Sample Input 1 5 78 64 23 6 93 Sample Output 1 6 23 64 78 93
Input Format
Input contains the size of the array and the values
Constraints
Array size may vary
Output Format
Print the sorted values

Sample Input 0
5
78 64 23 6
Sample Output 0
6 23 64 78 93 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    scanf("%d",&arr[i]);
    
        for(int i=0;i<n;i++)
        {
          for(int j=i+1;j<n;j++)
          {
            if(arr[i]>arr[j])
            {
               int t=arr[i];
               arr[i]=arr[j];
               arr[j]=t;
            }
          }
        }
        for(int i=0;i<n;i++)
        printf("%d ",arr[i]);
        return 0;
}                                                         Run Code
PATTERN
PROGRAM TO PRINT NUMBER PATTERN 2
1 123 12345 1234567 123456789 1234567 12345 
123 1
Sample Input 1 
5 
Sample Output 1 
1 
123 
12345 
1234567 
123456789 
1234567 
12345 
123 
1
Input Format
Given N
Constraints
NIL
Output Format
Print the pattern

Sample Input 0
5
Sample Output 0
1
123
12345
1234567
123456789
1234567
12345
123
1
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
 int i, j, N;

    scanf("%d", &N);
    
    for(i=1; i<=N; i++)
    {
        for(j=1; j<=(i * 2 - 1); j++)
            printf("%d", j);
       printf("\n");
    }
    for(i=N-1; i>=1; i--)
    {
        for(j=1; j<=(i * 2 - 1); j++)
           printf("%d", j);
        printf("\n");
    }
    return 0;
}                                                          Run Code   
PROGRAM TO PRINT THE GIVEN NUMBER PATTERN
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n,i,j,k=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            k=i*j;
            if(k<100)
                printf("%d ",k);
            else
                printf("%d",k);
            if(k<10)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n,i,j,k=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            k=i*j;
            if(k<100)
                printf("%d ",k);
            else
                printf("%d",k);
            if(k<10)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}                                                                                         Run Code
PROGRAM TO PRINT THE GIVEN NUMBER PATTERN
1 
2 4 
3 6 9 4 8 12 16 5 10 15 20 25
Sample Input 1 
5 
Sample Output 1 
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25
Input Format
Given n
Constraints
NIL
Output Format
Print the pattern
Sample Input 0
5
Sample Output 0
1  
2  4  
3  6  9  
4  8  12 16 
5  10 15 20 25 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n,i,j,k=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            k=i*j;
            if(k<100)
                printf("%d ",k);
            else
                printf("%d",k);
            if(k<10)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}                                                      Run Code        
13. Get Integer Input from User
print("Enter '0' for exit.");
val = int(input("Enter any number: "));
if val == 0:
    exit();
else:
    print("You have just entered:",val);
Output :
Enter '0' for exit.
Enter any number: You have just entered: 4
14. Add Two Numbers in Python
print("Enter '0' for exit.");
print("Enter two numbers: ");
val1 = int(input());
val2 = int(input());
if val1 == 0:
    exit();
else:
    sum = val1 + val2;
    print("Sum of the given two number:",sum);
Output :
Enter '0' for exit.
Enter two numbers: 
Sum of the given two number: 13

7. Strings in Python
a = "S CraCkcoder"
print(a)
b = input()
print(b)
c = input("Enter Your Name:")
print(c)
d = str(input("Enter Your Name:"))
print(d)
Output :
S CraCkcoder
CraCkcoder
Enter Your Name:sujeet
Enter Your Name:singh