Posts

Showing posts from June, 2021

Design a way to sort the list of positive integers in the descending order according to frequency of elements. The elements with integers with higher frequency come before with lower frequency elements with same frequency come in the same order as they appear the values.

Image
          Consider the following I/O samples. SAMPLE INPUT:       6                                            2 2 4 9 9 9 SAMPLE OUTPUT:     9 9 9 2 2 4   C PROGRAM:    #include <stdio.h> #include<limits.h> #include<malloc.h> int * frequencySortArray( int *arr,int size) {      int** occurence = NULL;      int maxpos, count = 0, count_flag, o_row, index,max;      int newindex = 0, num, ctr;      occurence = (int**)calloc(1,sizeof(int*));      occurence[0] = (int*)calloc(2,sizeof(int));      occurence[0][0] = arr[0];      occurence[0][1]++;      count++;      for(index = 1 ; index < size ; index++)      {   ...

HOLLOW INVERTED PYRAMID STAR

Image
Consider the following I/O samples. SAMPLE INPUT:   3 SAMPLE OUTPUT:      * * * * *                                               *     *                                                  *          C PROGRAM:    #include <stdio.h> int main() {     int row,col,spc,n;     scanf("%d",&n);     for(row=n; row>=1; --row)     {         for(spc=n; spc>row; spc--)         {             printf("  ");         }         for(col=1; col<2*row; col++)     ...

HOLLOW PYRAMID STAR PATTERN

Image
Consider the following I/O samples. SAMPLE INPUT:   3 SAMPLE OUTPUT:                      *                                           *     *                                         *         *                                       *             *                                     * * * * * * * * *           C PROGRAM:    #include <stdio.h> int main() {     int row,col,spc,n;   ...

HOLLOW SQUARE PATTERN

Image
     Consider the following I/O samples. SAMPLE INPUT:  4 SAMPLE OUTPUT:                                               * * * *                                                       *         *                                                                                 *       *                         ...

INVERTED PYRAMID STAR PATTERN

Image
Consider the following I/O samples. SAMPLE INPUT:   3 SAMPLE OUTPUT:                                                         * * *                                              *   *                                                 *                                                                                 ...

PYRAMID STAR PATTERN

Image
Consider the following I/O samples. SAMPLE INPUT:   3 SAMPLE OUTPUT:                                                 *                                                        * *                                                  * * *              C PROGRAM:    #include <stdio.h> int main() {     int row,col,spc,n;     scanf("%d",&n);     for(row=1; row<=n; ++row)     {         for(spc=1; spc<=n-row; spc+...

SQUARE STAR PATTERN

Image
Consider the following I/O samples. SAMPLE INPUT:   3 SAMPLE OUTPUT:                                                                                         * * *                                                                            * * *                                                               ...

MIRRORED RIGHT TRIANGLE STAR PATTERN

Image
Consider the following I/O samples. SAMPLE INPUT:   5 SAMPLE OUTPUT:                    C PROGRAM:    #include <stdio.h> int main() {     int row,col,n;     scanf("%d",&n);     for(row=1; row<=n; ++row)     {         // Print spaces         for(col=row; col<n; ++col)         {             printf("  ");         }          // Print star         for(col=1; col<=row; ++col)         {             printf("* ");         }        printf("\n");     }     return 0; } OUTPUT:   The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++  an...

RIGHT TRIANGLE STAR PATTERN

Image
Consider the following I/O samples. SAMPLE INPUT:   5 SAMPLE OUTPUT:                                                   *                                           * *                                             * * *                                             * * * *                                             * * * * * ...

NUMBER OF ODD AND EVEN NUMBERS IN AN ARRAY

Image
Consider the following I/O samples. SAMPLE INPUT:  1 2 3 4 5 6 7 8 9 10 SAMPLE OUTPUT:  ODD = 5                                                                                   EVEN = 5 C PROGRAM:    #include<stdio.h> int main() {    int arr[100];    int i, n,even=0,odd=0;    printf("Enter the number of elements:");    scanf("%d",&n);    printf("Enter the array elements:");    for(i=0;i<n;i++)    scanf("%d",&arr[i]);    for(i=0;i<n;i++)    {         if(arr[i]%2==0)             even++;         else             odd++;   ...

C PROGRAM FOR BALANCED EXPRESSION

Image
Consider the following I/O samples. SAMPLE INPUT 1:  [10+(6-2)/{3-1}]          SAMPLE OUTPUT 1 :   VALID         SAMPLE INPUT 2:  (([a-z]+{c-d}/a)-d)/n) SAMPLE OUTPUT 1 :   INVALID   C PROGRAM: #include <stdio.h> #include <stdbool.h> #include <string.h> #define MAX_SIZE 100 int top=-1; char arr[MAX_SIZE]; int isEmpty() {     if(top == -1)         return true;     else         return false; } int isFull() {     if(top == MAX_SIZE-1)         return true;     else         return false; } void push(char item) {     if(isFull())         printf("Stack is full");     else         top++;         arr[top] = item; } void pop() {     if(isEmpty())         printf("Stack is emp...

C PROGRAM TO CHECK BALANCED PARENTHESIS - ( { [

Image
Consider the following I/O samples. SAMPLE INPUT 1:  [ { ( )  } ]             SAMPLE OUTPUT 1 :   VALID         SAMPLE INPUT 2:  [ { ] } SAMPLE OUTPUT 1 :   INVALID   C PROGRAM: #include<stdio.h> #include<string.h> #define N 100 char stack[N]; int i=-1; //push parenthesis into stack void push(char data)                 {     i++;     if(i>=N)         printf("Stack is full");     else         stack[i]=data; } //pop parenthesis out from the stack void pop() {     if(i == -1)         printf("Cannot remove");     else         i--; } char top() {     return stack[i]; } int empty() {     if(i == -1)         return 1;  // stack empty     else     ...

C PROGRAM TO CHECK BALANCED PARENTHESIS - ()

Image
Consider the following I/O samples. SAMPLE INPUT 1:  (())()               SAMPLE OUTPUT 1 :   VALID         SAMPLE INPUT 2:  ()(()(() SAMPLE OUTPUT 1 :   INVALID   C PROGRAM: #include<stdio.h> #include<string.h> #define N 100 char stack[N]; int i=-1; //push parenthesis into stack void push(char data)                 {     i++;     if(i>=N)         printf("Stack is full");     else         stack[i]=data; } //pop parenthesis out from the stack void pop() {     if(i == -1)         printf("Cannot remove");     else         i--; } char top() {     return stack[i]; } int empty() {     if(i == -1)         return 1;  // stack empty     else      ...