PSP SEC-19 BATCH-16
PSP SEC-19 BATCH-16
Problem -A
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