Professional Documents
Culture Documents
reverse words in a string:write a program to reverse the words in a given string without
using any library function example hello world output world hello
#include <stdio.h>
int main() {
char str[100];
reverseWords(str);
return 0;
}
```
Input:hello world
Output:world hello
2.longest increasing subarray with sum k:given an array of integers and a target sum k,find
the length of longest subarray with a sum equal to k the elements of the array can be both
positive and negative
```c
#include <stdio.h>
if (sum == k) {
maxLength = i + 1;
startIndex = 0;
endIndex = i;
}
return maxLength;
}
int main() {
int arr[] = { 3, 1, 2, -2, 4, 2, -1, 0, -3, 1 };
int size = sizeof(arr) / sizeof(arr[0]);
int k = 4;
return 0;
}
3.matrix spiral transversal :given an N×M matrix ,print its elements in spiral order for
example given the following 3×3 matrix
```c
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
return 0;
}
Input: {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16 }}
Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Explanation: The output is matrix in spiral format.
4.counting inversions:given a array of integers,find the Number of inversions in the array an
inversions occurs when two elements in the array are out of their natural order
for example in the array [2,4,1,3,5],there are three inversions:(2,1),(4,1),(4,3)
```c
#include <stdio.h>
int merge(int arr[], int temp[], int left, int mid, int right) {
int i = left;
int j = mid + 1;
int k = left;
int invCount = 0;
return invCount;
}
return invCount;
}
int main() {
int arr[] = {2, 4, 1, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
```
Input: arr[] = {8, 4, 2, 1}
Output: 6
Explanation: Given array has six inversions: (8, 4), (4, 2), (8, 2), (8, 1), (4, 1), (2, 1).
```c
#include <stdio.h>
totalSum += arr[i];
}
int main() {
int arr[] = {5, -3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
```
Input: arr[] = {8, -8, 9, -9, 10, -11, 12}
Output: 22
Explanation: Subarray 12, 8, -8, 9, -9, 10 gives the maximum sum, that is 22.
6.Longest Common Subsequence: Given two sequences, find the length of the longest
common subsequence (LCS). A subsequence is a sequence that appears in the same
relative order but not necessarily contiguous. For example, the LCS of "ABCD" and "ACDF"
is "ACD" with a length of 3.
```c
#include <stdio.h>
#include <string.h>
return LCS[m][n];
}
int main() {
char sequence1[] = "ABCD";
char sequence2[] = "ACDF";
int m = strlen(sequence1);
int n = strlen(sequence2);
return 0;
}
```
Input:ABCD and ACDF
output:3
7.Find Peak Element: Given an array of integers, find a peak element, which is an element
that is greater than or For example, in the array [1, 3, 5, 2, 6], both 5 and 6 are peak
elements
```c
#include <stdio.h>
return left;
}
int main() {
int arr[] = {1, 3, 5, 2, 6};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
```
Input: array[]= {5, 10, 20, 15}
Output: 20
Explanation: The element 20 has neighbors 10 and 15, both of them are less than 20.
8.Word Search: Given a 2D board of letters and a word, determine if the word exists in the
grid. The word can be constructed from letters of adjacent cells (horizontally or vertically).
For example, given the board:
The word "ABCCED" exists in the board, but "SEE" does not
```c
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define ROWS 3
#define COLS 4
visited[row][col] = 0;
}
return false;
}
return false;
}
int main() {
char board[ROWS][COLS] = {
{ 'A', 'B', 'C', 'E' },
{ 's', 'F', 'C', 'S' },
{ 'A', 'D', 'E', 'E' }
};
if (wordSearch(board, word1)) {
printf("%s exists in the board.\n", word1);
} else {
printf("%s does not exist in the board.\n", word1);
}
if (wordSearch(board, word2)) {
printf("%s exists in the board.\n", word2);
} else {
printf("%s does not exist in the board.\n", word2);
}
return 0;
}
``
Input:
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true
9.Rat in a Maze: Given a 2D matrix representing a maze, find a path for a rat to reach its
destination (bottom-right corner) from the top-left corner.The rat can move in four directions:
up, down, left, and right. 0's in the matrix represent a valid path,while 1's represent
obstacles. the output should be taken by the rat
```c
#include <stdbool.h>
#include <stdio.h>
#define SIZE 4
sol[row][col] = 0;
return false;
}
return false;
}
if (solveMazeUtil(maze, 0, 0, sol)) {
printf("Path found:\n");
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
printf("%d ", sol[row][col]);
}
printf("\n");
}
} else {
printf("No path found.\n");
}
}
int main() {
int maze[SIZE][SIZE] = {
{ 0, 1, 0, 0 },
{ 0, 0, 0, 1 },
{ 1, 0, 0, 0 },
{ 0, 1, 1, 0 }
};
solveMaze(maze);
return 0;
}
```
Input:
{1, 0, 0, 0}
{1, 1, 0, 0}
{0, 1, 0, 0}
{0, 1, 1, 1}
10.Count Islands: Given a 2D matrix representing a map, where 0's represent water and 1's
represent land,count the Number of islands inthe map
An island is formed by connecting adjacent lands (horizontally or vertically). Assume that the
borders of the map are all water.for example:
Input:
[1, 1, 0, 0, 0],
[1, 1, 0, 1, 1],
[1, 1, 0, 0, 0]
```c
#include <stdbool.h>
#include <stdio.h>
#define ROWS 5
#define COLS 5
visited[row][col] = true;
return count;
}
int main() {
int map[ROWS][COLS] = {
{ 1, 1, 0, 0, 0 },
{ 1, 1, 0, 1, 1 },
{ 0, 0, 0, 1, 1 },
{ 0, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 0 }
};
return 0;
}
```
Input: mat[][] = {{1, 1, 0, 0, 0},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 0}}
Output: 4