LG Programs
LG Programs
1_second_lar.c
//Write a program to find the second largest element in an array without sorting the array.
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int large=arr[0];
int small=arr[0];
for(int i=0;i<size;i++)
{
if(large < arr[i])
large=arr[i];
if(arr[i] < small)
small=arr[i];
}
printf("large = %d\nsmall=%d\n",large,small);
int sec_large = small;
for(int i=0;i<size;i++)
{
if(large >arr[i] && sec_large < arr[i])
sec_large = arr[i];
}
printf("sec_large = %d\n",sec_large);
return 0;
}
2_increase_arr.c
// Find Increasing Elements in an Array
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int max=arr[0];
printf("%d ",max);
for(int i=0;i<size;i++)
{
if(arr[i] > max)
{
max=arr[i];
printf("%d ",max);
}
}
return 0;
}
3_next_greater.c
//For every element in the array,find the next greater element present on its right [Link] no greate
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int flag;
for(int i=0;i<size;i++)
{
flag=0;
for(int j=i+1;j<size;j++)
{
if(arr[i] < arr[j])
{
printf("%d->%d\n",arr[i],arr[j]);
flag=1;
break;
}
}
if(!flag)
printf("%d->%d\n",arr[i],-1);
}
return 0;
}
4_sum_key.c
// Sum of Any Array Elements Equal to the Key
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
int target;
printf("enter the target : ");
scanf("%d",&target);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(arr[i]+arr[j] == target)
{
printf("%d + %d",arr[i],arr[j]);
}
}
}
return 0;
}
5_missing_sub_arr.c
/*
Description: Given two arrays of size N and N-1,
where one element is missing in the second array,
find the missing element.
Test Case 1 Input: A = [1,2,3,4,5], B = [1,2,4,5] Output: 3
Test Case 2 Input: A = [10,20,30], B = [10,30] Output: 20
*/
/*#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
int sub_arr[size-1];
for(int i=0;i<size-1;i++)
scanf("%d",&sub_arr[i]);
int flag;
for(int i=0;i<size;i++)
{
flag=0;
for(int j=0;j<size-1;j++)
{
if(arr[i] == sub_arr[j])
{
flag=1;
break;
}
}
if(!flag)
printf("%d ",arr[i]);
}
}*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
int sum1=0,sum2=0;
for(int i=0;i<size;i++)
{
scanf("%d",&arr[i]);
sum1+=arr[i];
}
int sub_arr[size-1];
for(int i=0;i<size-1;i++)
{
scanf("%d",&sub_arr[i]);
sum2+=sub_arr[i];
}
printf("%d ",sum1-sum2);
}
6_freq.c
/*
WAP to Return Element Frequency Greater Than Size/2
Description:Write a program to find the majority element in an array whose frequency is greater than
Test Case 1 Input: 2 2 1 2 3 2 2 Output: 2
Test Case 2 Input: 5 5 5 1 2 Output: 5
*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int count;
for(int i=0;i<size;i++)
{
count=1;
for(int j=i+1;j<size;j++)
{
if(arr[i] == arr[j])
{
count++;
}
}
if(count > size/2)
{
printf("%d ",arr[i]);
return 0;
}
}
printf("No majority element");
return 0;
}
/*
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[n];
if(count == 0)
{
candidate = arr[i];
count = 1;
}
}
// Verify candidate
count = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] == candidate)
count++;
}
if(count > n / 2)
printf("%d", candidate);
else
printf("No majority element");
return 0;
}
*/
7_sum_near_target.c
/*
Description:
Write a program to find two elements in the array
whose sum is closest to the given target value.
Test Case 1 Input: Array = 1 3 4 7 10, Target = 15 Output: 4 + 10 = 14
Test Case 2 Input: Array = 2 5 8 14, Target = 20 Output: 5 + 14 = 19
*/
/*#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int target;
printf("enter the target : ");
scanf("%d",&target);
for(int i=0;i<size;i++)
{
for(int j=1+i;j<size;j++)
{
if(arr[i]+arr[j] == target || arr[i]+arr[j] == target-1 || arr[i]+arr[j] == target+1)
{
printf("%d + %d = %d\n",arr[i],arr[j],arr[i]+arr[j]);
}
}
}
return 0;
}*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int target;
printf("enter the target : ");
scanf("%d",&target);
int min=abs(arr[0]+arr[1] - target);
int sum,diff,first=arr[0],second=arr[1];
for(int i=0;i<size;i++)
{
for(int j=1+i;j<size;j++)
{
sum=arr[i]+arr[j];
diff=abs(sum - target);
if(diff < min)
{
min = diff;
first = arr[i];
second = arr[j];
}
}
}
printf("%d + %d = %d\n", first, second, first + second);
return 0;
}
8_dup_arr.c
/*
Description:
Write a program to identify duplicate elements present in an array.
Test Case 1 Input: 1 2 3 4 2 Output: 2
Test Case 2 Input: 5 6 7 5 8 Output: 5
*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int count;
for(int i=0;i<size;i++)
{
count=0;
for(int j=1+i;j<size;)
{
if(arr[i] == arr[j])
{
count++;
for(int k=j;k<size-1;k++)
{
arr[k]=arr[k+1];
}
size--;
}
else
j++;
}
if(count > 0)
printf("%d ",arr[i]);
}
return 0;
}
9_remove_dup.c
/*
Description: Write a program to remove duplicate elements from an array
Test Case 1 Input: 1 2 2 3 4 4 Output: 1 2 3 4
Test Case 2 Input: 5 5 5 6 7 Output: 5 6 7
*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
for(int i=0;i<size;i++)
{
for(int j=1+i;j<size;j++)
{
if(arr[i] == arr[j])
{
for(int k=j;k<size-1;k++)
{
arr[k] =arr[k+1];
}
size--;
j--;
}
}
}
for(int i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
11_rev_skip_2.c
/*
Description: Write a program to print array elements in reverse order while skipping two positions be
Test Case 1 Input: 1 2 3 4 5 6 7 8 Output: 8 5 2
Test Case 2 Input: 10 20 30 40 50 60 Output: 60 30
*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
// for(int i=size-1;i>=0;i-=3)
// printf("%d ",arr[i]);
for(int i=0;i<size/2;i++)
{
int temp = arr[i];
arr[i] = arr[size-1-i];
arr[size-1-i] = temp;
}
for(int i=0;i<size;i+=3)
printf("%d ",arr[i])
return 0;
}
12_str_rev_without_temp.c
/*
Description: Write a program to reverse a string without using any temporary variable.
Test Case 1 Input: hello Output: olleh
Test Case 2 Input: world Output: dlrow
*/
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("enter the string : ");
scanf("%[^\n]",str);
int start=0;
int end=strlen(str)-1;
while(start<end)
{
// str[start]=str[start]+str[end];
// str[end] = str[start]-str[end];
// str[start]=str[start]-str[end];
str[start]=str[start]^str[end];
str[end] = str[start]^str[end];
str[start]=str[start]^str[end];
start++;
end--;
}
printf("%s",str);
return 0;
}
13_str_rev_word.c
/*
Description: Write a program to reverse the order of words in a sentence without reversing the charac
Test Case 1 Input: how are you Output: you are how
Test Case 2 Input: I love coding Output: coding love I
*/
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
printf("enter the string : ");
scanf("%[^\n]", str);
int len = strlen(str);
int i;
for (i = 0; i < len / 2; i++)
{
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
printf("%s\n", str);
i = 0;
int j = 0;
while (i <= len)
{
if (str[i] == ' ' || str[i] == '\0')
{
int end = i - 1;
while (j < end)
{
char temp = str[j];
str[j] = str[end];
str[end] = temp;
j++;
end--;
}
j = i + 1;
}
i++;
}
printf("%s\n", str);
return 0;
}
14_str_cmp.c
/*
Description: Write a program to compare two strings by ignoring uppercase and lowercase differences.
Test Case 1 Input: Hello, hELLo Output: Both are same
Test Case 2 Input: Apple, Apples Output: Not same
*/
#include <stdio.h>
#include <string.h>
#include<ctype.h>
int main()
{
char str1[100], str2[100];
printf("enter the 2 strings : ");
scanf("%s%s", str1, str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 != len2)
{
printf("not equal\n");
return 0;
}
int i = 0;
while (str1[i])
{
if (tolower(str1[i]) != tolower(str2[i]))
{
printf("not equal\n");
return 0;
}
i++;
}
// char ch1,ch2;
// while(str1[i])
// {
// if(str1[i] >='A' && str1[i] <='Z')
// ch1=str1[i]+32;
// else
// ch1=str1[i];
// if(str2[i] >='A' && str2[i] <='Z')
// ch2=str2[i]+32;
// else
// ch2=str2[i];
// if(ch1 != ch2)
// {
// printf("not equal\n");
// return 0;
// }
// i++;
// }
printf("equal\n");
return 0;
}
15_str_dup.c
/*
Description: Write a program to find duplicate characters in a string and print their frequency/count
Test Case 1 Input: programming Output: r -> 2 g -> 2 m -> 2
Test Case 2 Input: success Output: s -> 3 c -> 2
*/
#include<stdio.h>
int main()
{
char str[100];
printf("enter the string : ");
scanf("%s",str);
int i=0,j,count;
while(str[i])
{
j=i+1,count=1;
while(str[j])
{
if(str[i] == str[j])
{
count++;
for(int k=j;str[k];k++)
{
str[k]=str[k+1];
}
}
else
j++;
}
if(count > 1)
printf("%c -> %d\n",str[i],count);
i++;
}
return 0;
}
16_anagram_group.c
/*
Description: Write a program to group strings that are anagrams of each other.
Two strings are anagrams if they contain the same characters in different order.
Test Case 1 Input: cat tac act dog god Output: [cat tac act] [dog god]
Test Case 2 Input: listen silent rat tar Output: [listen silent] [rat tar]
*/
#include<stdio.h>
#include<string.h>
void sort(char str[])
{
for(int i=0;i<strlen(str)-1;i++)
{
for(int j=i+1;j<strlen(str);j++)
{
if(str[i] > str[j])
{
char temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
}
int isanagram(char str1[],char str2[])
{
if(strlen(str1) !=strlen(str2))
return 0;
char temp1[100], temp2[100];
strcpy(temp1, str1);
strcpy(temp2, str2);
sort(temp1);
sort(temp2);
return strcmp(temp1,temp2)==0;
}
int main()
{
int n;
scanf("%d",&n);
char str[n][100];
int visited[n];
for(int i =0;i<n;i++)
{
scanf("%s",str[i]);
visited[i]=0;
}
for(int i=0;i<n;i++)
{
if(visited[i])
continue;
printf("[%s ",str[i]);
visited[i]=1;
for(int j=i+1;j<n;j++)
{
if(!visited[j] && isanagram(str[i],str[j]))
{
printf("%s ",str[j]);
visited[j]=1;
}
}
printf("]");
printf("\n");
}
return 0;
}
17_anagram.c
/*
Write a program to check whether two strings are anagrams of each other.
Test Case 1 Input: listen, silent Output: Anagram
Test Case 2 Input: hello, world Output: Not Anagram
*/
#include <stdio.h>
#include <string.h>
void sort(char str[])
{
int len=strlen(str);
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
if (str[i] > str[j])
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
}
int main()
{
char str1[100], str2[100];
printf("enter the strings : ");
scanf("%s%s", str1, str2);
if (strlen(str1) != strlen(str2))
{
printf("not anagram\n");
return 0;
}
char temp1[100], temp2[100];
strcpy(temp1, str1);
strcpy(temp2, str2);
sort(temp1);
sort(temp2);
if (strcmp(temp1, temp2) == 0)
printf("anagram\n");
else
printf("not anagram\n");
return 0;
}
18_couple_str.c
/*
Description: Write a program to determine whether characters in one string can be mapped uniquely and
Test Case 1 Input: PAPER, TITLE Output: YES
Test Case 2 Input: foo, bar Output: NO
*/
#include<stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
printf("enter the strings : ");
scanf("%s%s",str1,str2);
if(strlen(str1) != strlen(str2))
{
printf("NO\n");
return 0;
}
char map1[256]={0};
char map2[256]={0};
int i=0;
while(str1[i])
{
char ch1=str1[i];
char ch2=str2[i];
if(map1[ch1]==0 && map2[ch2]==0)
{
map1[ch1]=ch2;
map2[ch2]=ch1;
}
else
{
if(map1[ch1] !=ch2 || map2[ch2] !=ch1)
{
printf("NO\n");
return 0;
}
}
i++;
}
printf("yes\n");
return 0;
}
19_nth_bit.c
/*
WAP to Reset the Nth Bit Using Bitwise Description: Write a program to clear/reset a specific bit po
Test Case 1 Input: Number = 15, Position = 1 Output: 13
Test Case 2 Input: Number = 7, Position = 2 Output: 3
*/
#include<stdio.h>
int main()
{
int num;
printf("enter the num : ");
scanf("%d",&num);
int pos;
printf("enter the pos : ");
scanf("%d",&pos);
printf("%d\n",num & (~(1<<pos)));
return 0;
}
20_check_set_or_not.c
/*
Description: Write a program to check whether a bit at a specified position in a number is set (1) or
Test Case 1 Input: Number = 5, Position = 2 Output: Set
Test Case 2 Input: Number = 8, Position = 1 Output: Not Set
*/
#include<stdio.h>
int main()
{
int num,pos;
printf("enter the num pos :");
scanf("%d%d",&num,&pos);
if((num & (1<<pos)) !=0)
printf("set\n");
else
printf("not set\n");
return 0;
}
21_swap_bitwise.c
/*
Description: Write a program to swap two numbers without using a temporary variable by using XOR bitw
Test Case 1 Input: A = 5, B = 10 Output: A = 10, B = 5
Test Case 2 Input: A = 7, B = 3 Output: A = 3, B = 7
*/
#include<stdio.h>
int main()
{
int a,b;
printf("enter the a and b : ");
scanf("%d%d",&a,&b);
printf("a=%d b=%d\n",a,b);
a=a^b;
b=a^b;
a=a^b;
printf("a=%d b=%d\n",a,b);
return 0;
}
22_conv_hexa.c
/*
Description: Write a program to rearrange hexadecimal digits using bitwise operations.
Test Case 1 Input: 0xABCD Output: 0xBADC
Test Case 2 Input: 0x1234 Output: 0x2143
*/
#include<stdio.h>
int main()
{
int x;
printf("enter the value : ");
scanf("%X",&x);
int a=((x & 0xF000)>>4) | ((x & 0x0F00) << 4) | ((x & 0x00F0)>>4) | ((x & 0x000F) << 4);
printf("0x%X",a);
}
23_fib_recur.c
/*
Description: Write a recursive program to print the Fibonacci series up to N terms.
Test Case 1 Input: 5 Output: 0 1 1 2 3
Test Case 2 Input: 7 Output: 0 1 1 2 3 5 8
*/
#include<stdio.h>
void fib_recu(int first,int second,int limit)
{
if(limit)
{
printf("%d ",first);
second=first+second;
first=second-first;
fib_recu(first,second,limit-1);
}
else
return;
}
int main()
{
int limit;
printf("enter the limit : ");
scanf("%d",&limit);
fib_recu(0,1,limit);
}
24_bubble_sort.c
/*
Description: Write a program to sort an array using Bubble Sort.
Test Case 1 Input: 5 2 8 1 3 Output: 1 2 3 5 8
Test Case 2 Input: 9 7 5 3 1 Output: 1 3 5 7 9
*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-1;j++)
{
if(arr[j] > arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(int i=0;i<size;i++)
printf("%d ",arr[i]);
return 0;
}
25_prime.c
/*
Description: Write a program to check whether a given number is prime or not. A prime number has exac
Test Case 1 Input: 13 Output: Prime
Test Case 2 Input: 12 Output: Not Prime
*/
#include <stdio.h>
int main()
{
int num;
printf("enter the num : ");
scanf("%d", &num);
if (num < 2)
{
printf("%d is not prime\n", num);
return 0;
}
int flag = 0;
for(int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
flag = 1;
break;
}
}
if (!flag)
printf("%d is prime\n", num);
else
printf("%d is not prime\n", num);
return 0;
}
26_dynamic_mem.c
/*
Description:
Write a program to dynamically allocate memory using malloc and calloc,
use the allocated memory, and then free it properly
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr=malloc(5*sizeof(int));
if(ptr == NULL)
return 0;
for(int i=0;i<5;i++)
{
ptr[i]=i+1;
printf("%d ",ptr[i]);
}
printf("dynamically memory allocated\n");
int *ptr1 =calloc(5,sizeof(int));
if(ptr1 == NULL)
return 0;
for(int i=0;i<5;i++)
{
ptr1[i]=i+1;
printf("%d ",ptr1[i]);
}
printf("dynamically memory allocated\n");
free(ptr);
free(ptr1);
ptr=NULL;
ptr1=NULL;
return 0;
}
27_sliding.c
/*
Description:
Given an array representing heights of vertical lines,
find two lines that together can store the maximum amount of water.
Test Case 1 Input: 1 8 6 2 5 4 8 3 7 Output: 49
Test Case 2 Input: 1 1 Output: 1
*/
#include<stdio.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d",&size);
int arr[size];
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
int maxarea=0;
for(int i=0;i<size-1;i++)
{
for(int j=i+1;j<size;j++)
{
int height;
if(arr[i]<arr[j])
height=arr[i];
else
height= arr[j];
int area=(j-i)*height;
if(area>maxarea)
maxarea=area;
}
}
printf("%d\n",maxarea);
return 0;
}
28_count_one.c
/*
1. Count and Say
Problem Statement
The Count and Say sequence is a sequence of digit strings generated by describing the previous term.
1st term = "1"
2nd term = "11" (one 1)
3rd term = "21" (two 1s)
4th term = "1211" (one 2, one 1)
5th term = "111221" (one 1, one 2, two 1s)
Given an integer N, print the Nth term of the Count and Say sequence.
Input Format
A single integer N.
Output Format
Print the Nth term of the Count and Say sequence.
Test Case 1
Input
1
Output
1
Test Case 2
Input
4
Output
1211
Test Case 3
Input
5
Output
111221
Explanation
For N = 5:
1
11
21
1211
111221
*/
#include <stdio.h>
#include <string.h>
int main()
{
int n;
scanf("%d", &n);
while(curr[i])
{
int count = 1;
i++;
}
next[k] = '\0';
strcpy(curr, next);
}
printf("%s\n", curr);
return 0;
}
29_minimum_sum.c
/*
If the rule is to find the minimum absolute sum of any two elements,
then negative sums are converted to positive using absolute value.
Given an array of N integers, find the minimum absolute sum
that can be obtained by adding any two distinct elements of the array.
The absolute sum of two numbers a and b is:
|a+b|
Return the smallest such value.
Input
5
-1 2 3 -4 2
Pair Sums
-1 + 2 = 1 → |1| = 1
-1 + 3 = 2 → |2| = 2
-1 + (-4)= -5 → |-5| = 5
-1 + 2 = 1 → |1| = 1
2 + 3 = 5 → |5| = 5
2 + (-4) = -2 → |-2| = 2
2 + 2 = 4 → |4| = 4
3 + (-4) = -1 → |-1| = 1
3 + 2 = 5 → |5| = 5
-4 + 2 = -2 → |-2| = 2
Output
1
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size;
printf("enter the size : ");
scanf("%d", &size);
if (size < 2)
{
printf("Need at least 2 elements\n");
return 0;
}
int arr[size];
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int min = abs(arr[0] + arr[1]);
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
int sum = abs(arr[i] + arr[j]);
if (min > sum)
min = sum;
}
}
printf("%d\n", min);
return 0;
}