0% found this document useful (0 votes)
2 views

PSP SEC-19 BATCH-16

The document presents a programming assignment with three problems related to matrix manipulation, counting special integers, and finding maximum gaps in an array. Each problem includes example inputs and outputs, along with code snippets in C to solve them. The document is structured with sections for team members, problem statements, and corresponding code implementations.

Uploaded by

k61294685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PSP SEC-19 BATCH-16

The document presents a programming assignment with three problems related to matrix manipulation, counting special integers, and finding maximum gaps in an array. Each problem includes example inputs and outputs, along with code snippets in C to solve them. The document is structured with sections for team members, problem statements, and corresponding code implementations.

Uploaded by

k61294685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Our Team

RAVI TEJA SANGEETHA PAVAN KUMAR YESWANTH

231FA04E 231FA04F 231FA04F 231FA04F61


94 26 20

Branch : Btech Course : CSE Section :19


QUESTION -1

Problem -A

1.a)Given an m x n matrix, return all elements of the matrix in spiral order.

Example 1:
Input: matrix = [[1,2,3],[4,5,6], [7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:
Input: matrix = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
CODE

#include <stdio.h>
m--;
void spiralPrint(int m, int n, int a[m][n]) { }
int i, k = 0, l = 0;
if (l < n) {
while (k < m && l < n) { for (i = m-1; i >= k; --i) {
for (i = l; i < n; ++i) { printf("%d ", a[i][l]);
printf("%d ", a[k][i]); }
} l++;
k++; }
}
for (i = k; i < m; ++i) { }
printf("%d ", a[i][n-1]); int main() {
} int m = 3, n = 3;
n--; int a[3][3] = {{1, 2, 3}, { 4,5,6}, { 7,8,9}};
spiralPrint(m, n, a); OUT PUT
if (k < m) { return 0;
for (i = n-1; i >= l; --i) { }
printf("%d ", a[m-1][i]);
}
Problem -B

We call a positive integer special if all of its digits are distinct. Given a positive integer n, return the
number of special integers that belong to the interval [1, n].
Example 1:
Input: n = 20
Output: 19
Explanation: All the integers from 1 to 20, except 11, are special. Thus, there are 19 special integers.
Example 2:
Input: n = 5
Output: 5
Explanation: All the integers from 1 to 5 are special.
Example 3:
Input: n = 135
Output: 110
Explanation: There are 110 integers from 1 to 135 that are special.Some of the integers that are not
special are: 22. 114, and 131.
code

int count = 0;
#include <stdio.h> for (int i = 1; i <= n; i++) {
#include <stdbool.h> if (isSpecial(i)) {
count++;
bool isSpecial(int n) { }
int digits[10] = {0}; }
return count;
while (n > 0) { }
if (digits[n % 10]) {
return false; int main() {
} int n = 135;
digits[n % 10]++; printf("Number of special integers from 1 to %d is: %d\n", n,
n /= 10; countSpecials(n));
} return 0; OUT PUT
return true; }
}

int countSpecials(int n) {
Problem -C

Given an integer array nums, return the maximum difference between two successive
elements in its sorted form. If the array contains less than two elements, return 0.

Example 1:
Input: nums = [3,6,9,1]
Output: 3

Explanation: The sorted form of the array is [1.3,6,9), either (3,6) or (6,9) has the
maximum difference 3.

Example 2:
Input: nums [10]
Output:0
Explanation: The array contains less than 2 clements, therefore return 0.
code

#include <stdio.h> maxGap = gap;


#include <stdlib.h> }
}
int compare(const void * a, const void * b) {
return ((int)a - (int)b); return maxGap;
} }

int maximumGap(int* nums, int numsSize){ int main() {


if(numsSize < 2) { int nums[] = {3, 6, 9, 1};
return 0; int numsSize = sizeof(nums) / sizeof(nums[0]);
} printf("Maximum gap: %d\n", maximumGap(nums,
numsSize));
qsort(nums, numsSize, sizeof(int), compare); return 0;
int maxGap = 0; }
OUT PUT
for(int i = 0; i < numsSize - 1; i++) {
int gap = nums[i+1] - nums[i];
if(gap > maxGap) {
Thank You

You might also like