Pointer Advanced Giridhar
Pointer Advanced Giridhar
Exercise No. 6
CODE
#include<stdio.h>//1
#include<stdlib.h>
int asc(void *a,void *b){
return (*(int*)a-*(int*)b);
}
1
SELF PRACTICE
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
Code
#include <stdio.h>//4
#include <stdlib.h>
#include <stdbool.h>
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