0% found this document useful (0 votes)
8 views9 pages

Pointer Advanced Giridhar

The document contains a series of programming exercises focused on various topics, including pointers, sorting algorithms, Roman numeral conversion, string manipulation, and finding the longest common prefix. Each exercise includes a problem statement, sample inputs and outputs, and corresponding C code solutions. The exercises vary in difficulty from medium to hard, catering to different levels of programming skills.

Uploaded by

k.s.yogeswar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Pointer Advanced Giridhar

The document contains a series of programming exercises focused on various topics, including pointers, sorting algorithms, Roman numeral conversion, string manipulation, and finding the longest common prefix. Each exercise includes a problem statement, sample inputs and outputs, and corresponding C code solutions. The exercises vary in difficulty from medium to hard, catering to different levels of programming skills.

Uploaded by

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

SELF PRACTICE

Exercise No. 6

Topics Covered : Pointer

Advanced Date : 05.11.24

Solve the following problems

Q. No. Question Detail Level

Reorganize the products so that the first half of the shelf is


arranged in increasing order of product IDs, and the second
half is arranged in decreasing order. Write a program to sort
the product IDs in the first half of the array in ascending order
and the second half in descending order. Constraints:
 The array will have at least one element.
 The array can contain both positive and negative
integers.
 If the array has an odd number of elements, the middle
1 element should be included in the first half. Medium
Sample Input
7
5381794
Sample Output
1358974
Sample Input
6
12 7 14 5 9 2
Sample Output
7 12 14 9 5 2

CODE
#include<stdio.h>//1
#include<stdlib.h>
int asc(void *a,void *b){
return (*(int*)a-*(int*)b);
}

1
SELF PRACTICE

int dec(void *a,void *b){


return (*(int*)b-*(int*)a);
}

int main(){
int n,count1=0,t;
scanf("%d",&n);
int a[n],b[n],c[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
if(n%2==0)
t=(n/2);
else
t=(n/2)+1;
for(int i=0;i<t;i++){
b[i]=a[i];
count1++;
}
int k=0;
for(int i=t;i<n;i++){
c[k]=a[i];
k++;
}
qsort(b,count1,sizeof(int),asc);
qsort(c,k,sizeof(int),dec);
for(int i=0;i<count1;i++)
printf("%d ",b[i]);
for(int i=0;i<k;i++)
printf("%d ",c[i]);
}

OUTPUT
5
63451
34651
2
SELF PRACTICE

Integer To Roman
Problem Statement: Given an integer A, convert it to a roman
numeral, and return a string corresponding to its roman
numeral version
2 Medium
Input 1:
A=5
Output 1:
"V"
Input 2:
A = 14
Output 2:
"XIV"
Constraints
1 <= A <= 3999

Code
#include<stdio.h>//2
int main(){
int n;
scanf("%d",&n);
char *o[11]={"","I","II","III","IV","V","VI","VII","VIII","IX","X"};
char *t[11]={"","X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX",
"XC"};
char *h[11]={"", "C", "CC", "CCC", "CD", "D", "DC", "DCC",
"DCCC", "CM"};
char *th[4]={"", "M", "MM", "MMM"};
printf("%s%s%s%s",th[n/1000],h[(n%1000)/100],t[(n%100)/
10],o[n%10]);
}
Output
523
DXXIII
Remaining String
Problem statement: Given a string S without spaces, a
character ch, and an integer count, the task is to find the
string after the specified character that has occurred given
count number of times.
Example 1:

3
SELF PRACTICE

Input: S = "Thisisdemostring", ch =
3 'i', count = 3 Medium
Output: ng
Explanation: Substring of S after the 3rd occurrence of 'i' is "ng"

Example 2:
Input: S = "Thisisdemostri", ch = 'i',
count = 3
Output: Empty string
Explanation: Substring of S after the 3rd occurrence of 'i' is
empty

CODE:
#include<stdio.h>//3
#include <string.h>

int main() {
char str[255], *str2;
char *p;
int n, c = 0;
scanf("%s", str);
getchar();
p = (char *)malloc(sizeof(char));
scanf("%c", p);
getchar();
scanf("%d", &n);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == *p) {
c++;
}
if (c == n) {
int len = strlen(str) - i;
str2 = (char *)malloc(len * sizeof(char));
strncpy(str2,str+(i+1),len);
str2[strlen(str) - i] = '\0';
printf("%s",str2);
free(str2);

4
SELF PRACTICE

break;
}
}

free(p);
return 0;
}
OUTPUT:
happydayeveryone
y
3
one
Smallest Positive Integer that cannot be represented as Sum
Problem Statement : Given an array of size N, find the smallest
positive integer value that is either not presented in the array
or cannot be represented as a sum(here sum means you are
adding two or more elements) of some elements from the
array.

4 Hard
Example 1:
Input:
N=6
array[] = {1, 10, 3, 11, 6, 15}
Output:
2
Explanation:
2 is the smallest integer value that cannot be represented as
sum of elements from the array.

Example 2:
Input:
N=3
array[] = {1, 1, 1}
Output:
4
Explanation:
1 is present in the array.
2 can be created by combining two 1s.
5
SELF PRACTICE

3 can be created by combining three 1s.


4 is the smallest integer value that
cannot be represented as sum of
elements from the array. Constraints:
1 ≤ N ≤ 10^6
1 ≤ array[i] ≤ 10^9
The array may contain duplicates.

Code
#include <stdio.h>//4
#include <stdlib.h>
#include <stdbool.h>

int smallestPositiveInteger(int* arr, int n) {


int s = 0;
for (int i = 0; i < n; i++) {
s += arr[i];
}
bool* dp = (bool*)calloc(s + 1, sizeof(bool));
dp[0] = true;
for (int i = 0; i < n; i++) {
for (int j = s; j >= arr[i]; j--) {
if (dp[j - arr[i]]) {
dp[j] = true;
}
}
}
for (int i = 1; i <= s; i++) {
if (!dp[i]) {
free(dp);
return i;
}
}
free(dp);
return s + 1;
}

6
SELF PRACTICE

int main() {
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d", smallestPositiveInteger(a, n));
return 0;
}
Output
5
12368
21
Longest Common Prefix in an Array
Given an array of N strings, find the longest common prefix
among all strings present in the array.
Example 1:
Input:
N=4
arr[] = {breadandjam, bread, breadth, breed}
5 Hard
Output: bre
Explanation: "bre" is the longest common prefix in all the
given strings.
Example 2:
Input:
N=2
arr[] = {hello, world}
Output: -1

Code
#include<stdio.h>//5
#include<string.h>
int main(){
char *str[255],temp[255];
int n,c=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",str[i]);
7
SELF PRACTICE

}
strcpy(temp,*(str+0));
for(int i=1;i<n;i++){
int j = 0;
while (j < strlen(temp) && j < strlen(*(str+i)) && temp[j] ==
*(*(str+i)+j)) {
temp[j]=*(*(str+i)+j);
c=1;
j++;
}
temp[j] = '\0';
}
if(c)
printf("%s",temp);
else
printf("-1");
}

output
3
Hello Heap Hell
He
You are given a string str of length N to a function, the string may
contain multiple words separated by spaces. The string may also
6 Hard
have leading and trailing spaces, as well as multiple spaces
between words. Your task is to reverse the order of the words in

the string, ensuring that there is exactly one space between each
word in the reversed string, with no leading or trailing spaces.
Sample Input
str []= " Hello World from OpenAI "
Sample Output
"OpenAI from World Hello"

Code
#include<string.h>//6
#include<stdio.h>
int main(){

8
SELF PRACTICE

char str[255],*rev[255];
char *token;
int i=0;
gets(str);
token=strtok(str," ");
while(token){
rev[i] = (char *)malloc(strlen(token) + 1);
strcpy(*(rev+i),token);
token=strtok(NULL," ");
i++;
}
for(int j=i-1;j>=0;j--){
printf("%s",*(rev+j));
if (i > 0)
printf(" ");
}
}

Output
Good morning Everyone
Everyone morning Good

You might also like