find pairs with given sum in an array

Q26. Write a code to find pairs with given sum in an array.

Solution : You are given an array and a number and task is to find all pairs of elements in an integer array whose sum is equal to a given number.

Array :- An array is a collection of data items, all of the same type, accessed using a common name.
For example: int arr[7]={5,8,7,5,6,5,4};

Algorithm  for find pairs of elements in an integer array

//Algorithm To Find All Pairs Of Elements In An Integer Array.
START
Step 1 -> Input ARRAY arr[] and NUMBER num.
Step 2 -> for i=0 to length of the arr[]-1:
for j=i+1 to length of the arr[]:
if arr[i]+arr[j] equals to num:
print the output
Loop End
Loop End
STOP

Code for find pairs of elements in an integer array

Output

4+5=9
7+2=9
6+3=9

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find smallest element in an array

Q25. Write a code to find smallest element in an array

For Example :-
input : arr[]={ 5,5,8,3,5}
output : 3

So as we can see that the smallest element in arr[] = { 5,5,8,3,5} is 3 so the output is 3.

Algorithm  for Smallest element in an array 

START
Step 1 → Take an array Arr and define its values
Step 2 → Declare one variable (VAR) and assign 1st element of array to it
Step 3 → Loop for each value of Arr Repeat Step 4
Step 4 → If Arr[I] < VAR, Assign Arr[I] to VAR
Step 5 → After loop finishes, Display VAR which holds the smallest element of array
STOP

Code for Smallest element in an array 

Output

Smallest element in an Array is =3

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Delete duplicate elements from array

Q24. Write a code to delete duplicate elements from array

Delete Duplicate Elements :- In this, we will perform the delete operation in the array and after performing the delete operation, the array must contain only unique integer values.
For Example :-
Input = 5,7,8,2,4,5,6,5,4,4
Output = 5,7,8,2,4,6
So as we can see that the input consists of 10 elements ( 5,7,8,2,4,5,6,5,4,4) which contain some duplicate elements. So we have to remove duplicate elements from the array, and after performing the delete operation the output will be 5,7,8,2,4,6

Algorithm For Delete Duplicate Elements

START
Step 1 -> Input the number of elements of the array.
Step 2 -> Input the array elements.
Step 3 -> Repeat from i = 1 to n
– if (arr[i] != arr[i+1])
– temp[j++] = arr[i]
– temp[j++] = arr[n-1]
Step 4 -> Repeat from i = 1 to j
– arr[i] = temp[i]
Step 5 -> return j.
STOP

Code For Delete Duplicate Elements

Output

Array elements after deleting duplicates :5,7,8,2,4,6,

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find LCM of two numbers

Q23. Write a code to find LCM of two numbers.

LCM :- LCM (Least Common Multiple) of numbers is defined as the smallest possible multiple of two or more numbers.

For Example :-
Input = 6 and 5
Output = 30 is the LCM.
As we can see 30 is the smallest possible multiple of 6 and 5 therefore, 30 is the least common multiple (LCM) of two numbers 6 and 5

Algorithm For LCM

START
Step 1 → Initialize num1 and num2 with positive integers
Step 2 → Store maximum of num1 & num2 to max
Step 3 → Check if max is divisible by num1 and num2
Step 4 → If divisible, Display max as LCM
Step 5 → If not divisible then increase max by 1 and then goto step 3
STOP

Code For LCM

Output

LCM of 5 and 6 is 30

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

string is substring of given string or not.

Q22.Write a code to check whether the string is substring of given string or not.

String :- The string is defined as an array of characters. The difference between a character array and a string is the string ending with a special character ‘\0’.Given two strings S1 and S2, The task is to find if S1 is a substring of S2. If yes, return the index of the first occurrence, else return -1.

For Example :- Input : s1 = “Alfa”, s2 = “Alfatechlab”
Output : 0

check whether substring in a string Algorithm 

START

STOP

check whether substring in a string code

Output

Substring Found Successfully!

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number