Array
Array
Algorithm:
It inputs the new element to be inserted ( x) and the position where the element is to be for (i = 0; i < n; i++)
{
inserted (pos). scanf("%d", &array[i]);
It shifts all elements one position forward from the place where the new element needs }
/*
* C program to insert an element in a specified position in a given array
*/
#include <stdio.h>
void main()
{
int array[100];
int i, n, x, pos;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are: \n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be inserted: ");
scanf("%d", &pos);
//shift all elements 1 position forward from the place
//where element needs to be inserted
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
array[pos-1]=x; //Insert the element x on the specified position
//print the new array
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
}
Competitive Coding Sharda-Informatics
Algorithm:
1. Accept the size of the array (n) and the elements of the array.
2. Input the index of the element to be deleted (index).
3. Check if the index is valid:
If the index is greater than or equal to n+1, print an error message and exit.
If the index is valid, proceed to the next step.
4. Shift all elements after the specified index one position backward to fill the gap left by the
deleted element.
5. Print the modified array.
Code Explanation:
The program takes the size of the array and the elements of the array as input.
It inputs the index of the element to be deleted.
It checks if the index is valid and, if so, shifts elements and prints the modified array.
Complexities:
/*
* C program to delete an element from an array by index
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, n, index, arr[10];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: \n");
for (i = 0; i < n; i++)
{
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("Enter the index of the element to be deleted: ");
scanf("%d", &index);
if (index >= n+1)
{
printf (" \n Deletion is not possible in the array.");
}
else
{
for (i = index; i < n - 1; i++)
arr[i] = arr[i + 1];
printf("The array after deleting the element is: ");
for (i = 0; i < n - 1; i++)
printf("%d ", arr[i]);
return 0;
}
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to insert a particular element in a specified position Commented [S1]: /*
in a given array. * C program to insert a particular element in a specified
position
* in a given array
*/
Algorithm:
#include <stdio.h>
void main()
1. Accept the number of elements n and the elements of the array. {
int array[10];
2. Sort the elements of the array in ascending order. int i, j, n, m, temp, key, pos;
3. Input the element to be inserted (key).
printf("Enter how many elements \n");
4. Find the correct position ( pos) to insert the element in the sorted array. scanf("%d", &n);
printf("Enter the elements \n");
If key is less than the current element, set pos to the current position.
for (i = 0; i < n; i++)
If key is greater than the last element, set pos to n. {
scanf("%d", &array[i]);
5. Shift the elements to make space for the new element at position pos. }
6. Insert the key at the correct position.
printf("Input array elements are \n");
7. Print the final array. for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
Code Explanation: }
}
Competitive Coding Sharda-Informatics
/*
* C program to insert a particular element in a specified position
* in a given array
*/
#include <stdio.h>
void main()
{
int array[10];
int i, j, n, m, temp, key, pos;
}
if (key > array[n-1])
{
pos = n;
break;
}
}
if (pos != n)
{
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
}
array[pos] = key;
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to print all the repeated numbers along with their
frequencies in an array.
Algorithm:
1. Create an array count to store the frequency of each element. Initialize it with zeros.
2. Iterate through the given array, and for each element:
If the corresponding count in the count array is 1, print the element as it is a repeated
number.
Otherwise, increment the count for that element in the count array.
3. Print the repeated elements along with their frequencies.
Code Explanation:
The program initializes an array count to store the frequency of each element.
It iterates through the given array, updating the count array and printing repeated elements
along with their frequencies.
The output shows the repeated elements present in the array.
Complexities:
/*
* C Program to Print all the Repeated Numbers with Frequency in an Array
*/
#include <stdio.h>
#include <malloc.h>
int main()
{
int array[] = {5, 10, 10, 2, 1, 4, 2};
int array_freq = sizeof(array) / sizeof(array[0]);
duplicate(array, array_freq);
getchar();
return 0;
}
Competitive Coding Sharda-Informatics
#include <stdio.h>
void findDuplicates(int arr[], int n)
{
// create another array of similar size
int temp[n];
int count = 0;
// main function
int main()
{
// original array
int arr[] = {50, 20, 10, 40, 20, 10, 10, 60, 30, 70};
// find duplicates
findDuplicates(arr, size);
return 0;
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to accept a list of data items, find the second-largest Commented [S1]: /*
* C program to accept a list of data items and find the
and smallest elements, compute the average of both, and search for the average value in second largest
* and smallest elements in it. Compute the average of
the array. Display appropriate messages based on the search result. both and search
* for the average value if it is present in the array.
* Display appropriate message on successful search.
Algorithm: */
#include <stdio.h>
1. Accept the value of N. void main ()
{
2. Input the numbers into the array.
int number[30];
3. Sort the array in descending order. int i, j, a, n, counter, average;
4. Print the numbers in descending order.
printf("Enter the value of N\n");
5. Print the second-largest and second-smallest numbers. scanf("%d", &n);
6. Compute the average of the second-largest and second-smallest numbers. printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
7. Initialize a counter to 0. scanf("%d", &number[i]);
8. Search for the average value in the array, counting occurrences.
for (i = 0; i < n; ++i)
9. Display appropriate messages based on the search result. {
for (j = i + 1; j < n; ++j)
{
Code Explanation: if (number[i] < number[j])
{
a = number[i];
number[i] = number[j];
The program takes the value of N and the numbers as input from the user.
number[j] = a;
It sorts the array in descending order and prints the numbers. }
}
It computes and prints the second-largest and second-smallest numbers.
}
It computes the average and searches for the average value in the array, counting
occurrences. printf("The numbers arranged in descending order
are given below \n");
It displays appropriate messages based on the search result.
for (i = 0; i < n; ++i)
{
Complexities: printf("%d\n", number[i]);
}
The space complexity is determined by the size of the array. for (i = 0; i < n; ++i)
{
if (average == number[i])
{
++counter;
}
}
...
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to find the sum of the contiguous subarray within a
1-D array of numbers that has the largest sum.
Algorithm:
1. Accept the length of the array (size) and the elements of the array.
2. Initialize variables m and l to store the starting and ending indices of the largest subarray.
3. Initialize largest to the first element of the array.
4. Use nested loops to iterate through all possible subarrays.
For each subarray, calculate the sum and update m, l, and largest if the sum is
greater than the current largest sum.
5. Print the elements of the largest subarray and its sum.
Code Explanation:
Complexities:
/*
* C Program to Find the Sum of Contiguous Subarray within a
* 1 - D Array of Numbers which has the Largest Sum
*/
#include<stdio.h>
int main()
{
int size,m=0,l=0;
for(int i=0;i<size;i++)
{
scanf("%d",&array[i]);
int largest=array[0];
for(int i=0;i<size;i++)
{
int sum=0;
for(int j=i;j<size;j++)
{
sum=sum+array[j];
if(sum>largest)
{
m=i;l=j;
largest=sum;
}
}
}
#include <stdio.h>
1. Create a function large that takes three parameters - an integer array list, an int large(int[], int, int);
integer position representing the current position in the array, and an integer int main()
{
largest representing the largest number found so far.
int size;
2. If the position is 0, return the largest as the base case for the recursion. int largest;
int list[20];
3. If the position is greater than 0, check if the element at the current position int i;
(list[position]) is greater than the current largest. If true, update the largest. printf("Enter size of the list:");
scanf("%d", &size);
4. Make a recursive call to large with the parameters list, position - 1, and the
printf("Printing the list:\n");
updated largest. for (i = 0; i < size ; i++)
{
5. Return the result of the recursive call. list[i] = rand() % size;
printf("%d \t", list[i]);
}
Code Explanation:
if (size == 0)
{
printf("Empty list\n");
The program takes the size of the array and generates random numbers to fill }
the array. else
It prints the array and then calls the large function to find the largest number {
largest = list[0];
using recursion. largest = large(list, size - 1, largest);
printf("\nThe largest number in the list is:
The result is printed in the main function. %d\n", largest);
}
}
Complexities:
int large(int list[], int position, int largest)
{
Time Complexity: O(N)
if (position == 0)
The recursion iterates through the array once, where N is the size of the return largest;
}
Problem Statement: The problem is to find the largest number in an array of
numbers using recursion. Commented [S1]: /*
* C Program to find the Biggest Number in an Array of
Numbers using
* Recursion
Algorithm: */
#include <stdio.h>
1. Create a function large that takes three parameters - an integer array list, an int large(int[], int, int);
integer position representing the current position in the array, and an integer int main()
{
largest representing the largest number found so far.
int size;
2. If the position is 0, return the largest as the base case for the recursion. int largest;
int list[20];
3. If the position is greater than 0, check if the element at the current position int i;
(list[position]) is greater than the current largest. If true, update the largest. printf("Enter size of the list:");
scanf("%d", &size);
4. Make a recursive call to large with the parameters list, position - 1, and the
printf("Printing the list:\n");
updated largest. for (i = 0; i < size ; i++)
{
5. Return the result of the recursive call. list[i] = rand() % size;
printf("%d \t", list[i]);
}
Code Explanation:
if (size == 0)
{
printf("Empty list\n");
The program takes the size of the array and generates random numbers to fill }
the array. else
It prints the array and then calls the large function to find the largest number {
largest = list[0];
using recursion. largest = large(list, size - 1, largest);
printf("\nThe largest number in the list is:
The result is printed in the main function. %d\n", largest);
}
}
Complexities:
int large(int list[], int position, int largest)
{
Time Complexity: O(N)
if (position == 0)
The recursion iterates through the array once, where N is the size of the return largest;
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to find the largest number in an array using loops.
Algorithm:
#include <stdio.h>
The program takes the size of the array and its elements as input from the user.
int main()
It initializes the variable largest with the first element of the array. {
int size, i, largest;
It then iterates through the array, updating the largest variable if a larger element is
printf("\n Enter the size of the array: ");
found. scanf("%d", &size);
Finally, it prints the largest element in the array. int array[size]; //Declaring array
return 0;
}
Competitive Coding Sharda-Informatics
Problem Statement: Th`e problem is to find two elements in the array such that the
difference between them is the largest.
Algorithm:
1. Create a function maximum_difference that takes an integer array array and its size
arr_size.
2. Initialize a variable max_diff with the difference between the second and first elements
of the array.
3. Use two nested loops to iterate through all pairs of elements in the array.
4. For each pair of elements, calculate the difference ( array[j] - array[i]) and update
max_diff if the calculated difference is greater than the current max_diff .
5. Return the final value of max_diff.
Problem Statement: The problem is to find the missing element in an array of distinct integers
from 1 to N. The array is supposed to have integers from 1 to N, but one element is missing.
Algorithm:
Code Explanation:
Complexities:
#include <stdio.h>
void findMissing(int arr[], int N)
{
int temp[N + 1];
for (int i = 0; i <= N; i++) {
temp[i] = 0;
}
for (int i = 0; i < N; i++) {
temp[arr[i] - 1] = 1;
}
int ans;
for (int i = 0; i <= N; i++) {
if (temp[i] == 0)
ans = i + 1;
}
printf("%d", ans);
}
/* Driver code */
int main()
{
int arr[] = { 1, 3, 7, 5, 6, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
findMissing(arr, n);
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to find and print the non-repeating elements in an array.
Algorithm:
Code Explanation:
The program takes the number of elements and the elements of the array as input.
It uses nested loops to check for duplicates and prints the non-repeating elements.
Complexities:
/*
C program to reverse an array using loops
*/
#include <stdio.h>
#include <stdlib.h>
int non_repeating_elements(int arr[], int n)
{
int i,j;
int count = 1;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(arr[i] == arr[j] && i != j)
break;
}
if(j == n )
{
printf("\nNon-repeating element [%d] : %d \n",count,arr[i]);
++count;
}
}
return -1;
}
int main()
{
int n,i;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
int arr[n];
printf("\nInput the array elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
non_repeating_elements(arr, n);
return 0;
}
Competitive Coding Sharda-Informatics
#include <stdio.h>
int visited[n];
if(visited[i]==0){
int count = 1;
for(int j=i+1; j<n; j++){
if(arr[i]==arr[j]){
count++;
visited[j]=1;
}
}
if(count==1)
printf("%d ",arr[i]);
}
}
return 0;
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to accept N integer numbers, store them in an array AR, and
then separate the odd elements into array OAR and even elements into array EAR. Finally, display
the contents of OAR and EAR.
Algorithm:
Code Explanation:
The program takes the size of array AR and its elements as input.
It uses a loop to separate odd and even elements into OAR and EAR.
It prints the contents of OAR and EAR.
Complexities:
/*
* C Program to accept N integer number and store them in an array AR.
* The odd elements in the AR are copied into OAR and other elements
* are copied into EAR. Display the contents of OAR and EAR.
*/
#include <stdio.h>
void main()
{
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to remove duplicates from an array using nested
for loops.
Algorithm:
Accept the number of elements in the array (n) and the elements of the array.
Create an array temp to store unique elements after removing duplicates.
Initialize a variable count to 0.
Iterate through the array, and for each element, check if it is already present in the temp
array.
If not present, add the element to the temp array and increment the count.
If already present, skip the element.
Print the original array and the modified array without duplicates.
Code Explanation:
The program takes the number of elements and the elements of the array as input.
It uses nested loops to check for duplicate elements and store unique elements in the
temp array.
It prints the original array and the modified array without duplicates.
Complexities:
/*
* C Program to remove duplicates from array using nested for loop
*/
#include <stdio.h>
int main()
{
int n, count = 0;
printf("Enter number of elements in the array: ");
scanf("%d", &n);
int arr[n], temp[n];
if(n==0)
{
printf("No element inside the array.");
exit(0);
}
printf("Enter elements in the array: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nArray Before Removing Duplicates: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
// To store unique elements in temp after removing the duplicate elements
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < count; j++)
{
if (arr[i] == temp[j])
break;
}
if (j == count)
{
temp[count] = arr[i];
count++;
}
}
printf("\nArray After Removing Duplicates: ");
for (int i = 0; i < count; i++)
printf("%d ", temp[i]);
return 0;
}
Competitive Coding Sharda-Informatics
Algorithm:
1. Accept the size of the array (size) and the elements of the array.
2. Initialize two pointers, start pointing at the first element and end pointing at the last
element.
3. Swap the elements at the start and end positions.
4. Increment start and decrement end.
5. Repeat steps 3-4 until start becomes greater than or equal to end.
6. Print the reversed array.
Code Explanation:
The program takes the size of the array and the elements of the array as input.
It uses a while loop to swap elements from the beginning and end of the array until the
pointers meet.
It prints the original array and the reversed array.
Complexities:
/*
C program to reverse an array using loops
*/
#include <stdio.h>
int main()
{
int size;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter Array Elements: ");
int arr[size];
//Start points at the first element and end points at the last element
int start=0,end=size-1;
while(start<end)
{
//Swapping elements
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
Problem Statement:
The problem is to segregate 0s on the left side and 1s on the right side of the array. The goal is
to perform this segregation in a single traversal of the array.
Algorithm:
1. Initialize two pointers, left at the beginning and right at the end of the array.
2. While left is smaller than right, perform the following steps:
Increment left index while encountering 0 at the left.
Decrement right index while encountering 1 at the right.
If left is still smaller than right, swap the elements at left and right.
3. Continue this process until left becomes greater than or equal to right.
Code Explanation:
The program initializes two pointers, left and right, and uses them to traverse the array in a
single pass.
It swaps elements at the left and right positions whenever necessary to segregate 0s and 1s.
Finally, it prints the segregated array.
Complexities:
/*
* C Program to Segregate 0s on Left Side & 1s on right side of the Array (Traverse Array only once)
*/
#include <stdio.h>
int main()
{
int arr[] = {0, 1, 0, 1, 1, 0};
int array_size = 6, i = 0;
segregate0and1(arr, array_size);
printf("segregated array is ");
for (i = 0; i < 6; i++)
printf("%d ", arr[i]);
getchar();
return 0;
}
Competitive Coding Sharda-Informatics
Problem Explanation: The problem involves finding and displaying two elements in an array
such that their sum is closest to zero.
Algorithm Explanation:
1. Initialize Variables: Initialize variables min_l and min_r to store the indices of the two elements
with the closest sum, and min_sum to store the minimum sum found so far.
2. Iterate Through Pairs:
Use two nested loops to iterate through all pairs of elements in the array.
For each pair, calculate the sum.
If the absolute value of the sum is less than the absolute value of min_sum, update
min_sum, min_l, and min_r with the current pair.
3. Print Result:
Print the two elements whose sum is closest to zero.
Explanation Without Code: The program starts by initializing variables to store the indices and
sum of the two elements with the closest sum. It then iterates through all pairs of elements,
updating the variables whenever a pair with a closer sum is found. Finally, it prints the two
elements with the closest sum to zero.
Complexities:
/*
* C Program to Find the two Elements such that their Sum is Closest to Zero
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
/* Initialization of values */
min_l = 0;
min_r = 1;
min_sum = array[0] + array[1];
for (l = 0; l < array_size - 1; l++)
{
for (r = l + 1; r < array_size; r++)
{
sum = array[l] + array[r];
if (abs(min_sum) > abs(sum))
{
min_sum = sum;
min_l = l;
min_r = r;
}
}
}
printf(" The two elements whose sum is minimum are %d and %d", array[min_l], array[min_r]);
}
int main()
{
int array[] = {42, 15, -25, 30, -10, 35};
minabsvaluepair(array, 6);
getchar();
return 0;
}
Competitive Coding Sharda-Informatics
Problem Statement: The problem is to read elements into an array and find the largest
two elements in a given array.
return 0;
}
Competitive Coding Sharda-Informatics
Problem Explanation: This C program finds and prints the union and intersection of two arrays.
Algorithm Explanation:
1. Input Arrays: Receive input for two arrays (array1 and array2) of size SIZE.
2. Print Arrays: Print the elements of the input arrays.
3. Sort Arrays: Sort both arrays using the function_sort function.
4. Find Intersection:
Use the find_intersection function to find the common elements between the two
arrays and store them in the intersection_array.
Print the intersection array.
5. Find Union:
Use the find_union function to find the union of the two arrays and store the result in
the union_array.
Print the union array.
Note:
The function_sort function is used for sorting arrays using the Bubble Sort algorithm.
The find_intersection and find_union functions use a merging approach to find common
elements and form the union, respectively.
Complexities:
Time Complexity:
Sorting arrays: O(SIZE^2) (Bubble Sort)
Finding Intersection: O(SIZE)
Finding Union: O(SIZE)
Overall Time Complexity: O(SIZE^2)
Space Complexity: O(SIZE) (for storing intersection and union arrays)
Competitive Coding Sharda-Informatics
/*
* C Program to Find Union & Intersection of 2 Arrays
*/
#include <stdio.h>
#define SIZE 5
void main()
{
int array1[SIZE], array2[SIZE], intersection_array[SIZE], union_array[SIZE*2];
int num_elements;
//Sort array 1
function_sort(array1);
printf("nnSorted elements of Array 1: ");
print_value(array1, SIZE);
//Sort array 2
function_sort(array2);
printf("\n\nSorted elements of Array 2: ");
Competitive Coding Sharda-Informatics
print_value(array2, SIZE);
//Find Intersection
num_elements = find_intersection(array1, array2, intersection_array);
printf("\n\n Intersection is: ");
print_value(intersection_array, num_elements);
//Find Union
num_elements = find_union(array1, array2, union_array);
printf("\n\n Union is: ");
print_value(union_array, num_elements);
}
intersection_array[k] = array1[i];
i++;
j++;
k++;
}
}
return(k);
}
{
union_array[k] = array2[j];
j++;
k++;
}
}
else
{
while (i < SIZE)
{
union_array[k] = array1[i];
i++;
k++;
}
}
return(k);
}