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

Coding Set 3

Uploaded by

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

Coding Set 3

Uploaded by

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

Question Paper Set 3

1. Merge Two Sorted Arrays

 Problem: Write a function that takes two sorted arrays and merges them into a single sorted
array without using any in-built sorting functions.

 Sample Input:
Input: [1, 3, 5] and [2, 4, 6]

 Sample Output:
Output: [1, 2, 3, 4, 5, 6]

2. Count Vowels in a String

 Problem: Create a program that counts the number of vowels in a given string.

 Sample Input:
Input: "OpenAI"

 Sample Output:
Output: 3
(Explanation: The vowels are 'O', 'e', and 'A')

3. Binary Search Implementation

 Problem: Write a function that performs a binary search on a sorted array to find the
position of a given target value.

 Sample Input:
Input: [1, 2, 3, 4, 5], Target: 3

 Sample Output:
Output: 2
(Explanation: The target value 3 is at index 2.)

4. Find the GCD of Two Numbers

 Problem: Write a program to find the greatest common divisor (GCD) of two given integers
using the Euclidean algorithm.

 Sample Input:
Input: 8, 12

 Sample Output:
Output: 4
5. Remove Duplicates from a Linked List

 Problem: Given a singly linked list, write a program to remove all duplicate values from the
list.

 Sample Input:
Input: 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5

 Sample Output:
Output: 1 -> 2 -> 3 -> 4 -> 5
Answers :

1. Merge Two Sorted Arrays

Python:

def merge_sorted_arrays(arr1, arr2):

merged = []

i=j=0

while i < len(arr1) and j < len(arr2):

if arr1[i] < arr2[j]:

merged.append(arr1[i])

i += 1

else:

merged.append(arr2[j])

j += 1

merged.extend(arr1[i:])

merged.extend(arr2[j:])

return merged

print(merge_sorted_arrays([1, 3, 5], [2, 4, 6]))

# Output: [1, 2, 3, 4, 5, 6]

JAVA :

import java.util.Arrays;

public class MergeSortedArrays {


public static int[] merge(int[] arr1, int[] arr2) {

int[] merged = new int[arr1.length + arr2.length];

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

while (i < arr1.length && j < arr2.length) {

if (arr1[i] < arr2[j]) {

merged[k++] = arr1[i++];

} else {

merged[k++] = arr2[j++];

while (i < arr1.length) {

merged[k++] = arr1[i++];

while (j < arr2.length) {

merged[k++] = arr2[j++];

return merged;

public static void main(String[] args) {

int[] arr1 = {1, 3, 5};

int[] arr2 = {2, 4, 6};

System.out.println(Arrays.toString(merge(arr1, arr2)));

// Output: [1, 2, 3, 4, 5, 6]

}
2. Count Vowels in a String

Python:

def count_vowels(s):

vowels = "aeiouAEIOU"

return sum(1 for char in s if char in vowels)

print(count_vowels("OpenAI"))

# Output: 3

JAVA :

public class CountVowels {

public static int countVowels(String s) {

int count = 0;

String vowels = "aeiouAEIOU";

for (char c : s.toCharArray()) {

if (vowels.indexOf(c) != -1) {

count++;

return count;

public static void main(String[] args) {

System.out.println(countVowels("OpenAI"));

// Output: 3

}
}

3. Binary Search Implementation

Python:

def binary_search(arr, target):

left, right = 0, len(arr) - 1

while left <= right:

mid = (left + right) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

left = mid + 1

else:

right = mid - 1

return -1

print(binary_search([1, 2, 3, 4, 5], 3))

# Output: 2

JAVA :

public class BinarySearch {

public static int binarySearch(int[] arr, int target) {

int left = 0, right = arr.length - 1;


while (left <= right) {

int mid = (left + right) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] < target) {

left = mid + 1;

} else {

right = mid - 1;

return -1; // Target not found

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

System.out.println(binarySearch(arr, 3));

// Output: 2

4. Find the GCD of Two Numbers

Python:

def gcd(a, b):

while b:

a, b = b, a % b

return a

print(gcd(8, 12))
# Output: 4

JAVA :

public class GCD {

// Method to calculate GCD using Euclidean algorithm

public static int findGCD(int a, int b) {

while (b != 0) {

int temp = b;

b = a % b;

a = temp;

return a;

public static void main(String[] args) {

// Sample Input

int num1 = 8;

int num2 = 12;

// Finding GCD

int gcd = findGCD(num1, num2);

// Sample Output

System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);

// Output: GCD of 8 and 12 is: 4

}
5. Remove Duplicates from a Linked List

Python:

class ListNode:

def __init__(self, val=0, next=None):

self.val = val

self.next = next

def remove_duplicates(head):

current = head

while current:

runner = current

while runner.next:

if runner.next.val == current.val:

runner.next = runner.next.next

else:

runner = runner.next

current = current.next

return head

def print_list(node):

while node:

print(node.val, end=" -> ")

node = node.next

print("None")

head = ListNode(1, ListNode(2, ListNode(2, ListNode(3, ListNode(4, ListNode(4,


ListNode(5)))))))

remove_duplicates(head)

print_list(head)

# Output: 1 -> 2 -> 3 -> 4 -> 5 -> None


JAVA :

import java.util.HashSet;

import java.util.LinkedList;

public class RemoveDuplicatesLinkedList {

public static void main(String[] args) {

LinkedList<Integer> list = new LinkedList<>();

// Adding elements to the linked list

list.add(1);

list.add(2);

list.add(2);

list.add(3);

list.add(4);

list.add(4);

list.add(5);

System.out.println("Original list:");

System.out.println(list);

// Remove duplicates

removeDuplicates(list);

System.out.println("List after removing duplicates:");

System.out.println(list);

// Function to remove duplicates from a LinkedList


public static void removeDuplicates(LinkedList<Integer> list) {

HashSet<Integer> set = new HashSet<>();

list.removeIf(e -> !set.add(e));

You might also like