Professional Documents
Culture Documents
program to find the smallest number which greater than a given number and has same set of digits
as given number
#include <stdio.h>
#include <string.h>
// Function to swap two digits in a string
void swap(char *str, int i, int j) {
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
// Function to find the smallest number greater than num with the same set of digits
void findNextNumber(char *num) {
int n = strlen(num);
int i;
// Find the rightmost digit that is smaller than the digit to its right
for (i = n - 2; i >= 0; i--) {
if (num[i] < num[i + 1]) {
break;
}
}
// Find the smallest digit on the right side of num[i] that is greater than num[i]
int smallestGreater = i + 1;
for (int j = i + 2; j < n; j++) {
if (num[j] > num[i] && num[j] < num[smallestGreater]) {
smallestGreater = j;
}
}
printf("The next greater number with the same set of digits: %s\n", num);
}
int main() {
char num[20];
printf("Enter a number: ");
scanf("%s", num);
findNextNumber(num);
return 0;
}
Output:
Enter a number: 123
The next greater number with the same set of digits: 132
P2: Given a set of time intervals in any order, merge all overlapping intervals into one and output the
result which should have only mutually exclusive intervals.
#include<stdlib.h>
#include<stdlib.h>
struct interval
{
int start;
int end;
};
int CompareIntervals(const void *a,const void *b)
{
return ((struct interval *)a)->start-((struct interval *)b)->start;
}
void mergeIntervals(struct interval intervals[],int n)
{
if(n<=0)
return;
qsort(intervals,n,sizeof(struct interval),CompareIntervals);
int resultIndex =0;
for(int i=1;i<n;i++)
{
if(intervals[resultIndex].end>=intervals[i].start)
{
intervals[resultIndex].end=intervals[i].end>intervals[resultIndex].end?intervals[i].end:intervals[resultI
ndex].end;
}
else
{
resultIndex++;
intervals[resultIndex] = intervals[i];
}
}
printf("merged intervals :\n");
for(int i=0;i<=resultIndex;i++)
{
printf("[ %d,%d]",intervals[i].start,intervals[i].end);
printf("\n");
}
int main()
{
struct interval intervals[]= {{1,3},{2,16},{8,10},{15,18}};
int n =sizeof(intervals)/sizeof(intervals[0]);
mergeIntervals(intervals,n);
return 0;
}
output:
merged intervals :
[ 1,18]
P3 : Given an expression string exp, write a program to examine whether the pairs and the orders of
“{“, “}”, “(“, “)”, “[“, “]” are correct in the given expression.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
if (!isMatchingPair(stack->data, expression[i])) {
return false;
}
stack = pop(stack);
}
}
return isEmpty(stack);
}
int main() {
char expression[100];
printf("Enter an expression: ");
scanf("%s", expression);
if (isBalancedExpression(expression)) {
printf("The expression is balanced.\n");
} else {
printf("The expression is not balanced.\n");
}
return 0;
}
Output:
Enter an expression: [[]]
The expression is balanced.
4.Given an array, print the Next Greater Element (NGE) for every element.
#include <stdio.h>
// Function to find and print the Next Greater Element for each element
void printNGE(int arr[], int size) {
int nextGreater[size]; // Array to store NGE for each element
int stack[size]; // Stack to keep track of indices
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of the array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
Output:
Enter the size of the array: 3
Enter the elements of the array:
1
2
3
Next Greater Elements:
NGE for 1 = 2
NGE for 2 = 3
NGE for 3 = -1
5.Given an array of integers heights representing the histogram's bar height where the width of each
bar is 1, return the area of the largest rectangle in the histogram.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int height;
int index;
} StackElement;
stackTop++;
stack[stackTop].height = currentHeight;
stack[stackTop].index = i;
}
free(stack);
return maxArea;
}
int main() {
int heights[] = {2, 1, 5, 6, 2, 3};
int heightsSize = sizeof(heights) / sizeof(heights[0]);
return 0;
}
Output:
The largest rectangle area is: 10
P6: Given a Linked List and a number N, write a function that returns the value at the Nth node from
the end of the Linked List
#include <stdio.h>
#include <stdlib.h>
// Definition for singly-linked list.
struct ListNode {
int val;
struct ListNode *next;
};
return slow->val;
}
int main() {
struct ListNode* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
int n = 2; // Get the value of the 2nd node from the end
return 0;
}
Output:
The value at the 2th node from the end is: 4
7.Merge Two sorted Linked Lists
#include <stdio.h>
#include <stdlib.h>
if (head == NULL) {
head = newNode;
} else {
struct Node* current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
return head;
}
if (list1 != NULL) {
tail->next = list1;
} else if (list2 != NULL) {
tail->next = list2;
}
return mergedList;
}
// Function to display a linked list
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
printf("List 1: ");
displayList(list1);
printf("List 2: ");
displayList(list2);
return 0;
}
Output:
List 1: 1 -> 3 -> 5 -> NULL
List 2: 2 -> 4 -> 6 -> NULL
Merged List: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
P8 Find the Height of the Binary Tree
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Output:
Height of the binary tree is: 2
P9: Deletion of a node in the BST
#include <stdio.h>
#include <stdlib.h>
// Node with two children: Get the inorder successor (smallest in the right subtree)
struct Node* temp = findMin(root->right);
root->data = temp->data;
int main() {
struct Node* root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);
root->right->left = createNode(60);
root->right->right = createNode(80);
return 0;
}
Output:
Inorder traversal before deletion: 20 30 40 50 60 70 80
Inorder traversal after deleting 50: 20 30 40 60 70 80
P10: Given Two Binary Trees Check whether the two binary trees are mirrored or not
#include <stdio.h>
#include <stdlib.h>
int main() {
// Construct two binary trees
struct Node* root1 = createNode(1);
root1->left = createNode(2);
root1->right = createNode(2);
root1->left->left = createNode(3);
root1->left->right = createNode(4);
root1->right->left = createNode(4);
root1->right->right = createNode(3);
if (areMirrored(root1, root2)) {
printf("The two binary trees are mirrored.\n");
} else {
printf("The two binary trees are not mirrored.\n");
}
return 0;
}
Output:
The two binary trees are mirrored.
#include <stdio.h>
P11: Given Tree is BST or not?
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
return(node);
}
int main()
{
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
return min_index;
}
mstSet[u] = true;
int main() {
int graph[V][V] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
return 0;
}
Output:
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
P13: For a Given Graph find the Minimum Spanning Tree using Kruskal’s Algorithm
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
if (srcRoot != destRoot) {
printf("(%d, %d) weight: %d\n", edges[i].src, edges[i].dest, edges[i].weight);
mstWeight += edges[i].weight;
unionSets(subsets, srcRoot, destRoot);
count++;
}
}
int main() {
struct Edge edges[E] = {
{0, 1, 2}, {0, 3, 6}, {1, 2, 3},
{1, 3, 8}, {1, 4, 5}, {2, 4, 7},
{3, 4, 9}
};
kruskalMST(edges);
return 0;
}
Output:
Edges in the MST:
(0, 1) weight: 2
(1, 2) weight: 3
(1, 4) weight: 5
(0, 3) weight: 6
Total weight of MST: 1