0% found this document useful (0 votes)
26 views

Array

The third document contains code to insert a particular element into

Uploaded by

Sumit Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Array

The third document contains code to insert a particular element into

Uploaded by

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

Competitive Coding Sharda-Informatics

Problem Statement: The problem is to insert an element in a specified position in a given


array.

Algorithm:

1. Accept the number of elements n and the elements of the array.


2. Input the new element to be inserted ( x) and the position where the element is to be
inserted (pos).
Commented [S1]: /*
3. Shift all elements one position forward from the place where the new element needs to * C program to insert an element in a specified position
be inserted. in a given array
*/
4. Increment the size of the array ( n).
#include <stdio.h>
5. Insert the new element ( x) at the specified position ( pos). void main()
{
6. Print the new array. int array[100];
int i, n, x, pos;

Code Explanation: printf("Enter the number of elements in the array


\n");
scanf("%d", &n);
 The program takes the number of elements and the elements of the array as input. printf("Enter the elements \n");

 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 }

to be inserted. printf("Input array elements are: \n");


for (i = 0; i < n; i++)
 It increments the size of the array ( n). {
printf("%d ", array[i]);
 It inserts the new element ( x) at the specified position (pos). }
 It prints the new array. printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be
inserted: ");
Complexities: scanf("%d", &pos);

//shift all elements 1 position forward from the place


 Time Complexity: O(N) //where element needs to be inserted
n=n+1;
 The program shifts elements and inserts the new element, which takes linear time. for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional array[pos-1]=x; //Insert the element x on the specified
position
to the input size.
//print the new array
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
}
Competitive Coding Sharda-Informatics

 /*
 * 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

Problem Statement: The problem is to delete an element from an array by index.

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:

 Time Complexity: O(N)


 The program shifts elements after the specified index, which takes linear time.
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional
to the input size.
Competitive Coding Sharda-Informatics


 /*
 * 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: }

// Sorting the elements of the array


for (i = 0; i < n; i++)
 The program takes the number of elements and the elements of the array as input. {
 It sorts the array in ascending order using a simple bubble sort. for (j = i + 1; j < n; j++)
{
 It inputs the element to be inserted ( key). if (array[i] > array[j])
{
 It finds the correct position to insert the element in the sorted array ( pos). temp = array[i];
array[i] = array[j];
 It shifts the elements to make space for the new element. array[j] = temp;
 It inserts the key at the correct position and prints the final array. }
}
}
Complexities: ...
Commented [S2]: if (pos != n)
{
 Time Complexity: O(N^2) m = n - pos + 1 ;
for (i = 0; i <= m; i++)
 The program uses a simple bubble sort to sort the array, resulting in a time {
array[n - i + 2] = array[n - i + 1] ;
complexity of O(N^2). }
}
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional array[pos] = key;

to the input size. printf("Final list is \n");


for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}

}
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;

printf("Enter how many elements \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\n", array[i]);
}

// Sorting the elements of the array


for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

printf("Sorted list is \n");


for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}

printf("Enter the element to be inserted \n");


scanf("%d", &key);

for (i = 0; i < n; i++)


{
if (key < array[i])
{
pos = i;
break;
Competitive Coding Sharda-Informatics

}
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;

printf("Final list is \n");


for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}

}
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:

 Time Complexity: O(N)


 The program iterates through the given array once, resulting in linear time complexity.
 Space Complexity: O(N)
 The space complexity is determined by the size of the count array, which is proportional
to the input size.
Competitive Coding Sharda-Informatics

/*
* C Program to Print all the Repeated Numbers with Frequency in an Array
*/
#include <stdio.h>
#include <malloc.h>

void duplicate(int array[], int num)


{
int *count = (int *)calloc(sizeof(int), (num - 2));
int i;

printf("duplicate elements present in the given array are ");


for (i = 0; i < num; i++)
{
if (count[array[i]] == 1)
printf(" %d ", array[i]);
else
count[array[i]]++;
}
}

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;

// traverse original array


for(int i=0; i<n; i++) {

int element = arr[i];


int flag = 0;

// check current element is already


// checked or not
for(int j=0; j<count; j++) {
if(temp[j] == element) {
flag = 1;
break;
}
}

// if already exist then don't check


if(flag) {
continue;
}

// check occurrence of element


for(int j=i+1; j<n; j++) {
if(arr[j] == element) {
temp[count++] = element;
// found, therefore break
break;
}
}
}

// count repeated elements


printf("Total Repeated elements = %d\n", count);
// display repeated elements
printf("Repeated elements are: ");
for (int i = 0; i < count; i++) {
printf("%d ",temp[i]);
}
}
Competitive Coding Sharda-Informatics

// main function
int main()
{
// original array
int arr[] = {50, 20, 10, 40, 20, 10, 10, 60, 30, 70};

// find array size


int size = sizeof(arr)/sizeof(arr[0]);

// 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]);
}

printf("The 2nd largest number is = %d\n",


 Time Complexity: O(N^2) number[1]);
 The program uses a simple bubble sort to sort the array, resulting in a time printf("The 2nd smallest number is = %d\n",
number[n - 2]);
complexity of O(N^2).
average = (number[1] + number[n - 2]) / 2;
 Space Complexity: O(N) counter = 0;

 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:

 The program accepts the length and elements of the array.


 It uses nested loops to iterate through all possible subarrays, updating m, l, and largest
as needed.
 Finally, it prints the elements of the largest subarray and its sum.

Complexities:

 Time Complexity: O(N^2)


 The program uses nested loops to iterate through all possible subarrays, resulting
in a time complexity of O(N^2).
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional
to the input size.
Competitive Coding Sharda-Informatics

/*
* 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;

printf("Type the length of the array\n");


scanf("%d",&size);
int array[size];
printf("type the elements of the array\n");

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;
}
}
}

printf("\n The largest contigous subarray is");


for(int z=m;z<=l;z++)
{
printf(" %d ",array[z]);
}
printf("\n The sum of the largest contigous subarray is");
printf(" %d",largest);
return 0;
}
Competitive Coding Sharda-Informatics

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;

array. if (position > 0)


{
 Space Complexity: O(N) if (list[position] > largest)
{
 The space complexity is determined by the depth of the recursion stack, largest = list[position];
}
which is O(N) due to the recursive calls. return large(list, position - 1, 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;

array. if (position > 0)


{
 Space Complexity: O(N) if (list[position] > largest)
{
 The space complexity is determined by the depth of the recursion stack, largest = list[position];
}
which is O(N) due to the recursive calls. return large(list, position - 1, largest);
}

}
Competitive Coding Sharda-Informatics

Problem Statement: The problem is to find the largest number in an array using loops.

Algorithm:

1. Declare variables size, i, and largest.


2. Input the size of the array (size).
3. Declare an array of integers with a size of size.
4. Input the elements of the array.
5. Initialize largest with the first element of the array.
6. Use a loop to iterate through the array starting from the second element.
7. In each iteration, compare the current element with largest. If the current element is
greater, update largest.
8. After the loop, largest will contain the largest element in the array.
9. Print the value of largest. Commented [S1]: /*
* C Program to find the largest number in an array using
loops
Code Explanation: */

#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

//Input array elements


printf("\n Enter %d elements of the array: \n", size);
Complexities:
for (i = 0; i < size; i++)
{
 Time Complexity: O(N) scanf(" %d", &array[i]);
}
 The program iterates through the array once, where N is the size of the array.
//Declaring Largest element as the first element
 Space Complexity: O(N) largest = array[0];
 The space complexity is determined by the size of the array.
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}

printf("\n largest element present in the given array


is : %d", largest);

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.

Code Explanation: Commented [S1]: /*


* C Program to Find 2 Elements in the Array such that
Difference between them is Largest
*/
 The program defines a function maximum_difference to find the maximum difference #include <stdio.h>
between two elements in an array.
int maximum_difference(int array[], int arr_size)
 In the main function, an array is initialized with values, and the maximum_difference {
int max_diff = array[1] - array[0];
function is called with the array and its size. int i, j;
for (i = 0; i < arr_size; i++)
 The result is printed using the printf statement. {
for (j = i + 1; j < arr_size; j++)
{
Complexities: if (array[j] - array[i] > max_diff)
max_diff = array[j] - array[i];
}
 Time Complexity: O(N^2) }
return max_diff;
 The program uses two nested loops to iterate through all pairs of elements in the }
array, resulting in a time complexity of O(N^2). int main()
 Space Complexity: O(1) {
int array[] = {10, 15, 90, 200, 110};
 The space complexity is constant as there is no additional space usage proportional printf("Maximum difference is %d",
maximum_difference(array, 5));
to the input size. getchar();
return 0;
}
Competitive Coding Sharda-Informatics

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:

1. Create an array temp of size N+1 and initialize all elements to 0.


2. Iterate through the given array and mark the presence of each element in the temp array.
3. Iterate through the temp array and find the missing element (the element with value 0).

Code Explanation:

 The program initializes an array temp with all elements set to 0.


 It marks the presence of each element in the given array by setting the corresponding index in
temp to 1.
 Finally, it finds and prints the missing element in the temp array.

Complexities:

 Time Complexity: O(N)


 The program iterates through the given array once and the temp array once, resulting in
linear time complexity.
 Space Complexity: O(N)
 The space complexity is determined by the size of the temp array, which is proportional
to N.
Competitive Coding Sharda-Informatics

 #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:

1. Accept the number of elements n and the elements of the array.


2. Iterate through each element of the array.
 For each element, check if it has any duplicates by comparing with all other elements.
 If no duplicates are found, print the element as a non-repeating element.
3. Repeat this process for all elements in the array.

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:

 Time Complexity: O(N^2)


 The program uses nested loops to compare each element with all other elements,
resulting in a time complexity of O(N^2).
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional to
the input size.
Competitive Coding Sharda-Informatics

 /*
 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>

 // Main function to run the program


 int main()
 {
 int arr[] = {21, 30, 10, 2, 10, 20, 30, 11};
 int n = sizeof(arr)/sizeof(arr[0]);

 int visited[n];

 for(int i=0; i<n; i++){

 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:

1. Accept the size of array AR (n) and its elements.


2. Initialize two arrays OAR and EAR to store odd and even elements, respectively.
3. Iterate through each element of AR.
 If the element is even, copy it to EAR.
 If the element is odd, copy it to OAR.
4. Display the contents of OAR and EAR.

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:

 Time Complexity: O(N)


 The program iterates through the array once, and the time complexity is linear with
respect to the size of the array.
 Space Complexity: O(N)
 The space complexity is linear with respect to the size of the array, as additional arrays
OAR and EAR are used to store elements.
Competitive Coding Sharda-Informatics

/*
* 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()
{

long int ARR[10], OAR[10], EAR[10];


int i, j = 0, k = 0, n;

printf("Enter the size of array AR n");


scanf("%d", &n);

printf("Enter the elements of the array n");


for (i = 0; i < n; i++)
{
scanf("%ld", &ARR[i]);
fflush(stdin);
}

/* Copy odd and even elements into their respective arrays */

for (i = 0; i < n; i++)


{
if (ARR[i] % 2 == 0)
{
EAR[j] = ARR[i];
j++;
}
else
{
OAR[k] = ARR[i];
k++;
}
}

printf("The elements of OAR are n");


for (i = 0; i < k; i++)
{
printf("%ldn", OAR[i]);
}

printf("The elements of EAR are n");


for (i = 0; i < j; i++)
{
printf("%ldn", EAR[i]);
}

}
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:

Time Complexity: O(N^2)


The program uses nested loops to compare each element with all previous
elements, resulting in a time complexity of O(N^2).
Space Complexity: O(N)
The space complexity is determined by the size of the temp array, which can have
at most N unique elements.
Competitive Coding Sharda-Informatics

 /*
 * 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

Problem Statement: The problem is to reverse an array using loops.

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:

 Time Complexity: O(N)


 The program iterates through the array once, and the time complexity is linear with
respect to the size of the array.
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional
to the input size.
Competitive Coding Sharda-Informatics

 /*
 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];

 //Input array elements


 for(int i=0;i<size;i++)
 scanf("%d",&arr[i]);
 printf("Entered Array is: ");
 for(int i=0;i<size;i++)
 printf("%d ",arr[i]);

 //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;

 //Incrementing start and decrementing end


 start++;
 end--;
 }

 //Printing reversed array


 printf("\nReversed array is: ");
 for(int i=0;i<size;i++)
 printf("%d ",arr[i]);
 return 0;
 }
Competitive Coding Sharda-Informatics

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:

 Time Complexity: O(N)


 The program traverses the array once, and the time complexity is linear with respect to
the size of the array.
 Space Complexity: O(1)
 The space complexity is constant as there is no additional space usage proportional to
the input size.
Competitive Coding Sharda-Informatics

/*
* C Program to Segregate 0s on Left Side & 1s on right side of the Array (Traverse Array only once)
*/
#include <stdio.h>

/*Function to segregate all 0s on left and all 1s on right*/


void segregate0and1(int array[], int size)
{
int left = 0, right = size-1;

while (left < right)


{
/* Increment left index while we see 0 at left */
while (array[left] == 0 && left < right)
left++;
/* Decrement right index while we see 1 at right */
while (array[right] == 1 && left < right)
right--;
/* If left is smaller than right then there is a 1 at left and a 0 at right. Exchange it */
if (left < right)
{
array[left] = 0;
array[right] = 1;
left++;
right--;
}
}
}

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:

 Time Complexity: O(N^2)


 The program uses nested loops to iterate through all pairs of elements, resulting in
quadratic time complexity.
 Space Complexity: O(1)
 The space complexity is constant as the program uses a fixed number of variables
regardless of the input size.
Competitive Coding Sharda-Informatics

/*
* C Program to Find the two Elements such that their Sum is Closest to Zero
*/
# include <stdio.h>
# include <stdlib.h>
# include <math.h>

void minabsvaluepair(int array[], int array_size)


{
int count = 0;
int l, r, min_sum, sum, min_l, min_r;

/* Array should have at least two elements*/


if (array_size < 2)
{
printf("Invalid Input");
return;
}

/* 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.

Algorithm: Commented [S1]: /*


* C program to read elements into an array and find the
* largest two elements in a given array.
1. Declare variables n, i, largest1, largest2, and temp. */

2. Input the size of the array ( n). #include <stdio.h>


int main ()
3. Declare an array of integers with a size of n. {
int n = 0, i = 0, largest1 = 0, largest2 = 0, temp = 0;
4. Input the elements of the array.
5. Iterate through the array to find the two largest elements ( largest1 and largest2). printf ("Enter the size of the array\n");
scanf ("%d", &n);
6. Swap the values if necessary, so that largest1 contains the greater of the two. int array[n];
printf ("Enter the elements\n");
7. Iterate through the array again, updating largest1 and largest2 based on the current for (i = 0; i < n; i++)
{
element's value. scanf ("%d", &array[i]);
8. Print the values of largest1 and largest2. }

printf ("The array elements are : \n");


for (i = 0; i < n; i++)
Code Explanation: {
printf ("%d\t", array[i]);
}
 The program takes the size of the array and its elements as input from the user.
printf ("\n");
 It initializes largest1 and largest2 with the first two elements of the array.
 It ensures that largest1 always contains the greater value. largest1 = array[0];
largest2 = array[1];
 It then iterates through the array, updating largest1 and largest2 based on the current
if (largest1 < largest2)
element's value. {
temp = largest1;
 Finally, it prints the values of the two largest elements. largest1 = largest2;
largest2 = temp;
}
Complexities:
for (int i = 2; i < n; i++)
{
 Time Complexity: O(N) if (array[i] > largest1)
{
 The program iterates through the array twice, where N is the size of the array. largest2 = largest1;
largest1 = array[i];
 Space Complexity: O(N) }
else if (array[i] > largest2 && array[i] != largest1)
 The space complexity is determined by the size of the array. {
largest2 = array[i];
}
}

printf ("The FIRST LARGEST = %d\n", largest1);


printf ("THE SECOND LARGEST = %d\n", largest2);

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 get_value(int arr[]);


void print_value(int arr[], int n);
void function_sort(int arr[]);
int find_intersection(int array1[], int array2[], int intersection_array[]);
int find_union(int array1[], int array2[], int union_array[]);

void main()
{
int array1[SIZE], array2[SIZE], intersection_array[SIZE], union_array[SIZE*2];
int num_elements;

//input elements of Array1


printf("\n Enter the elements of Array 1: n");
get_value(array1);
printf("\n\n Elements of Array 1: ");
print_value(array1, SIZE);

//Sort array 1
function_sort(array1);
printf("nnSorted elements of Array 1: ");
print_value(array1, SIZE);

//input elements of Array2


printf("nnEnter the elements of Array 2: n");
get_value(array2);
printf("\n\n Elements of Array 2: ");
print_value(array2, 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);
}

void get_value(int arr[])


{
int i, j;
for (i = 0; i < SIZE; i++)
{
j = i + 1;
printf("\n Enter element %d: ", j);
scanf("%d", &arr[i]);
}
}

void print_value(int arr[], int n)


{
int i;
printf("{ ");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("}");
}

void function_sort(int arr[])


{
Competitive Coding Sharda-Informatics

int i, j, temp, swapping;

for (i = 1; i < size; i++)


{
swapping = 0;
for (j = 0; j < size-i; j++)
{
if (arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapping = 1;
}
}
if (swapping == 0)
{
break;
}
}
}

int find_intersection(int array1[], int array2[], int intersection_array[])


{
int i = 0, j = 0, k = 0;
while ((i < size) && (j < size))
{
if (array1[i] < array2[j])
{
i++;
}
else if (array1[i] > array2[j])
{
j++;
}
else
{
Competitive Coding Sharda-Informatics

intersection_array[k] = array1[i];
i++;
j++;
k++;
}
}
return(k);
}

int find_union(int array1[], int array2[], int union_array[])


{
int i = 0, j = 0, k = 0;
while ((i < SIZE) && (j < SIZE))
{
if (array1[i] < array2[j])
{
union_array[k] = array1[i];
i++;
k++;
}
else if (array1[i] > array2[j])
{
union_array[k] = array2[j];
j++;
k++;
}
else
{
union_array[k] = array1[i];
i++;
j++;
k++;
}
}
if (i == SIZE)
{
while (j < SIZE)
Competitive Coding Sharda-Informatics

{
union_array[k] = array2[j];
j++;
k++;
}
}
else
{
while (i < SIZE)
{
union_array[k] = array1[i];
i++;
k++;
}
}
return(k);
}

You might also like