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

4.Arrays_1

The document outlines various problem-solving practices related to arrays, categorized into easy and medium levels. It includes tasks such as creating and manipulating arrays, calculating sums and averages, counting elements based on conditions, and identifying distinct values. Sample test cases are provided for each task to illustrate input and output formats.

Uploaded by

717823p127
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)
5 views

4.Arrays_1

The document outlines various problem-solving practices related to arrays, categorized into easy and medium levels. It includes tasks such as creating and manipulating arrays, calculating sums and averages, counting elements based on conditions, and identifying distinct values. Sample test cases are provided for each task to illustrate input and output formats.

Uploaded by

717823p127
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/ 18

Problem Solving Practices

Practices# :4

Topic : Arrays

Level 1: Easy
1. Create an integer array with size 5, then fetch the values from the keyboard, store them
in the array, and display the elements.

Input format

The first line of each test case consists of elements of the array

Output format

Print the array elements.

Constraints

Use a one-dimensional array to store array elements and print the same as the output.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 13578 12 14 15 8 99 24 88 67 10 56

Output 13578 12 14 15 8 99 24 88 67 10 56

2. Richard has to read n number of values in an array and he needs to print them in reverse
order

Input format

The first line of each test case consists of the size of the array

The second line of each test case has elements of the array to perform the reverse task
Output format

Print the array elements in reverse order.

Constraints

Get first-line and second-line input in the integer format.

Use a one-dimensional array to store array elements and reverse them by accessing the array

index position.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 5 3

Input 2 2 3 5 18 2 8 97 75 99 76 37 2

Output 18 5 3 2 99 75 97 8 2 2 37 76

3. Create an array of integers, then find and print the size (number of elements) of the array.

Input Format:

A series of space-separated integers representing the elements of the array.

Output Format:

A single integer represents the size of the array.

Constraints:

The array can contain between 1 and 1000 integers.

Each integer should be between -1000 and 1000.


Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 10 20 30 40 50 -3 15 22 8 -54 9 -23 879 43 323

Output 5 4 6

4. John has been given an integer array of numbers, his task is to read the integer numbers
from the console and store them into an array then print the sum and average of array
elements as output.

Input format

The first line of each test case consists of the size of the input array elements

The second line of the input consists of elements to be stored in the array

Output format

Print the sum and average of the array elements

Constraints

Get input in the integer format. Use a one-dimensional array to store and compute the sum.

Sample test Cases:


Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 3 4 5

Input 2 134 23 44 77 66 5 7 99 123 67

Output 82 210 52 301 60


5. Given input salaries of three employees in integer data type, your task is to count the
number of employees getting salaries between 5000 to 10000.

Input format

The first line of each test case consists of salaries of employees in integer.

Output format

Print the number of employees getting salaries in the specified range.

Constraints

Get input in the integer format.

Use one dimensional array to store employee salaries and do selection using if- else

statement.

Sample test Cases:

Test Test Case 1: Test Case 2: Test Case 3:

Cases

Input 1 Salary of Emp 1 :12000 Salary of Emp1 :1000 Salary of Emp 1 :1000

Salary of Emp 2 :3000 Salary of Emp 2 :7500 Salary of Emp 2 :800

Salary of Emp 3 :8000 Salary of Emp 3 :9800 Salary of Emp 3 :2500

Output 2 3 NIL

6. Given are the marks obtained by ‘n’ students in Mathematics subject as integer input and
your work is to count the number of students who obtained mark greater than 70. Also count
the number of students who are failed. (<35).

Input format

The first line of each test case consists of number of students


The second line of the input consists of marks obtained by students

Output format

Print the number of students as per condition specified where marks >70 and marks <35

Constraints

Get input in the integer format.

Use one dimensional array to store student marks salaries and do selection using if- else

statement.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 3 4 5

Input 2 78 85 90 25 89 97 75 100 98 76 37 23

Output 3 3 3

NIL 1 2

7. Rita has to initialize an array with int, float or double data type elements and she has to
calculate the number of elements in the array.

Output format

Print the array elements.

Constraints

Use one dimensional array to store array elements and sizeof function to display the length.
Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 11 33 45 57 38 11.4 12.7 33.9 44.8 4.5672, 2.45354,

-5.22234

Output 5 4 3

8. Alice has one array with N number of elements and he has to copy all the elements in first
array to second array and print the second array.

Input format

The first line of each test case consists of first array elements.

Output format

Print the array elements of second array.

Constraints

Get first line input N in the integer format. Use one dimensional array to store array elements

and copy them to next array.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 10 12 8 25 99 3 13 29 7

Output 10 12 8 25 99 3 13 29 7

9. Dany has an assignment to print the alternate elements of the input array Arr which is
given to him during the run time of the program.
Input format

The first line of each test case consists of input elements of array

Output format

Print the array elements of second array.

Constraints

Input Arr ranges from 1<Arr <10

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 2 4 6 9 3 7 12 1 3 6 2 4 8 0 12 44 15 44 88 21 2 8

Output 2 6 3 12 1 6 4 0 44 44 21 8

10. The challenge for Lisa is to determine the minimum element and maximum element in
the array N given the size and number of elements as input.

Input format

The first line of each test case consists of array size

The second line of input should be array elements in integer form.

Output format

Print the minimum element and maximum element of the array

Constraints

Input N ranges from 1 to 10


Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 8 5

Input 2 24 -10 99 12 4 10 87 -3 -12 55 9 12 25 35 55 89 121

Output -10 99 -12 87 25 121

11. You are tasked with analyzing the scores submitted by participants in a coding
competition. The objective is to count how many participants achieved each distinct score.

Input Format:

A single line of space-separated integers representing the scores of the participants.

Output Format:

Print each distinct score along with the count of participants who achieved that score in the
format: score: count.

Constraints:

The number of scores can range from 1 to 1000.

Each score should be between 0 and 100 (inclusive).

Scores may contain duplicates.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 90 85 90 75 85 100 60 70 70 80 60 90 80 45 55 45 60 70 55 80
90 60 45

Output 90: 3 60: 3 45: 3

85: 2 70: 2 55: 2


75: 1 80: 2 60: 1

100: 1 90: 1 70: 1

80: 1

12. Elsa must accept 'n' numbers, store all prime numbers in an array, then print those
numbers as output.

Input format

The first line of each test case consists of array size as limit

The second line of input has the array elements as non- negative numbers

Output format

Print the prime numbers of the array

Constraints

Input the limit from 1 to 10

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 5 7

Input 2 12 23 29 18 3 7 9 12 2 19 25 57 73 98 50 35

Output 23 29 372 19 73

13. You are tasked with identifying anomalous packet types in a network traffic monitoring
system. In an array of integers representing packet types, all types appear an even number
of times and appear an odd number of times. Your goal is to find and print the anomalous
packet type.
Input Format:

A single line of space-separated integers representing the packet types.

Output Format:

Print the anomalous packet types (appear an odd number of times).

Constraints:

The number of packet types can range from 1 to 1000.

Each packet type is an integer between 1 and 10,000.

There will always be exactly one packet type that appears an odd number of times.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 123214414 5656787 9 9 10 10 11 11 12 12

12

Output 134 8 12

14. You are managing an online survey where users rate various products. After collecting
the ratings, you notice that some products received ratings an odd number of times, while
others received them an even number of times. Your task is to identify which product(s)
received ratings an even number of times.

Input Format:

The first line contains an integer n (1 ≤ n ≤ 1000) — the number of products.

The second line contains n integers where each integer represents the rating count for a
product.

Output Format:

Print a list of products (indices) that received an even number of ratings.


Constraints:

1 ≤ n ≤ 1000 (number of products)

1 ≤ rating count ≤ 1000 for each product

If no product received an even number of ratings, return an empty list.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 5 4 6

34782 1357 629145

Output 245 None 125

Level 2: Medium
1. Given a list of N elements and a key K. Find whether the given key exists in the list or not.
If it exists, print its position in the list.

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements

The third line of each test case consists of integer key K to be searched

Output format
Print the position, if integer is present in the list else print NOT FOUND

Constraints
1 < = N < = 100
Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 5 6 4

Input 2 1 2 5 6 10 33 47 19 10 28 99 10 20 30 40

Input 3 5 6 20

Output Exists at location 3 Not found Exists at location 2

2.Rahul has a set of integer input elements that are stored in an array, he has to find the first
and second largest element in a given array list of N numbers. Help him to derive the needed
result.

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements

Output format
Print the first and second largest array elements

Constraints
1 < = N < = 100

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 5 4 3

Input 2 91 22 54 64 100 20 87 45 67 55 10 99
Output 100 91 87 67 99 55

3. Given an integer array of numbers, your objective is to count distinct element in the given
array.

Input format

The first line of each test case consists of input array elements

Output format
Print the count of the distinct array elements

Constraints
Compare the elements using selection and repetition statements.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 30, 50, 30, 10, 20, 40, 20,88,45,57,88 65,44,12,15,64,44

10, 20

Output 5 4 5

4. Lina has stored few elements to an array and she has to traverse through the list and print
the number of positive and negative values present in the array as < 0, >0

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements


Output format
Print the count of positive and negative array elements

Constraints
Traverse through array elements using nested for loop.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 5 3

Input 2 10 -23 88 -56 12 24 -99 87 -65 35 8 -45

Output 22 32 21

5. Given an array with N elements as input at the run time, your code should insert an element
at ith location of the list, then all elements from the right of i+1 th location have to be shifted
one step towards right.

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements

The third line of each test case has the location and the value to be inserted.

Output format
Print the resultant array after insertion operation.

Constraints
Traverse through array elements and insert array value such that 1<=N<=100.
Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 5 3

Input 2 12 88 90 45 8 9 10 12 15 22 19 30

Input 3 1 10 3 35 2 35

Output 10 12 88 90 45 8 9 35 10 12 15 22 35 19 30

6. Riya has a set of integer input elements and the position at run time by the user. Your task
is to create a program to delete an element at a desired position from an array.

Input format
The first line of each test case consists of input array elements
The second line of each test case consists position(index) of the element to be deleted.

Output format
Print array elements after deletion with their current index position

Constraints
1 < = N < = 100

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 3 5

Input 2 15 19 9 10 18 99 34 66 99 12 78 23

Input 3 2 1 4
Output 15 19 10 18 34 66 99 12 78

7. Deena’s task is given for receiving input array elements and sorting those elements into
ascending order.

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements

Output format
Print the elements in ascending order

Constraints
Input elements should contain both positive and negative numbers.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 4 5 3

Input 2 11 -44 23 8 11 12 34 56 -10 9 12 -15

Output -44 8 11 23 -10 11 12 34 56 -15 9 12

8. Given an array arr with input integer elements, these elements should have combination
of non-negative numbers and negative number. Your task is to arrange the numbers in non-
increasing order.

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements


Output format
Print the elements in descending order.

Constraints
Input numbers should contain both positive and negative numbers.

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 7 5 4

Input 2 45 88 12 -10 25 35 8 11 12 34 56 -10 -12 10 45 83

Output 88 45 35 25 12 8 -10 56 34 12 11 -10 83 45 10 -12

9.Rihan has a set of natural numbers that are stored to one dimensional array and his
objective is to separate odd and even numbers in two separate arrays

Input format

The first line of each test case has number of elements to be stored in array

The second line of each test case consists of N array elements.

Output format
First line of output should have the odd numbers of the array
Second line of output should have the even elements of the array.

Constraints
Compute modulo function to sort odd and even numbers

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:


Input 1 3 5 4

Input 2 185 23 55 99 76 45 12 89 33 29

Output 15 23 83 45 10 12

8 55 99 83 45

10 12

10. Given an array arr[] of size N-1 with integers in the range of [1, N], the task is to swap
adjacent elements of a one dimensional array.

Input format

The first line of each test case consists of size of the array

The second line of each test case consists of N array elements

Output format
Print the elements in array after swapping.

Constraints
Input elements has the range [1.N]

Sample test Cases:

Test Cases Test Case 1: Test Case 2: Test Case 3:

Input 1 5 4 7

Input 2 10 13 15 17 20 123 67 15 10 55 67 13 56 98 77 153

Output 13 10 17 15 20 67 123 10 15 67 55 56 13 77 98 153

You might also like