List Practice
Python lists — ordered, mutable collections perfect for sequential data.
1
Sum of Elements in a List
Easy
Write a program to find the sum of all elements in a given list of integers.
Input Format
First line contains integer N (1 ≤ N ≤ 10⁵). Second line contains N space-separated integers.
Output Format
Print a single integer — the sum of all elements.
Sample Test Cases
|
Sample Input 1
5 1 2 3 4 5 |
Sample Output 1
15 |
|
Sample Input 2
3 -5 0 3 |
Sample Output 2
-2 |
|
Sample Input 3
4 10 -10 20 -20 |
Sample Output 3
0 |
|
Sample Input 4
1 100 |
Sample Output 4
100 |
Constraints
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
2
Access List Elements by Index
Easy
Write a program that takes a list and an index, and prints the element at that index. If the index is out of range, print "Index out of range". Indices are 0-based.
Input Format
First line contains integer N. Second line contains N space-separated integers. Third line contains integer K (index to access).
Output Format
Print the element at index K, or "Index out of range".
Sample Test Cases
|
Sample Input 1
5 10 20 30 40 50 2 |
Sample Output 1
30 |
|
Sample Input 2
3 100 200 300 5 |
Sample Output 2
Index out of range |
|
Sample Input 3
1 99 0 |
Sample Output 3
99 |
|
Sample Input 4
4 1 2 3 4 -1 |
Sample Output 4
Index out of range |
Constraints
0 ≤ N ≤ 10⁵, 0 ≤ K < 10⁵
3
Count Occurrences in a List
Easy
Write a program to count how many times a given element X appears in a list.
Input Format
First line contains integer N. Second line contains N space-separated integers. Third line contains integer X.
Output Format
Print a single integer — the count of X in the list.
Sample Test Cases
|
Sample Input 1
8 3 1 4 1 5 1 9 2 1 |
Sample Output 1
3 |
|
Sample Input 2
5 7 8 9 10 11 5 |
Sample Output 2
0 |
|
Sample Input 3
6 5 5 5 5 5 5 5 |
Sample Output 3
6 |
|
Sample Input 4
4 -1 -2 -3 -1 -1 |
Sample Output 4
2 |
Constraints
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i], X ≤ 10⁶
4
Append Elements to a List
Easy
Write a program that reads N integers and builds a list by appending each element. Print the final list.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the list as comma-separated values inside square brackets, e.g. [1, 2, 3].
Sample Test Cases
|
Sample Input 1
4 7 8 9 10 |
Sample Output 1
[7, 8, 9, 10] |
|
Sample Input 2
1 42 |
Sample Output 2
[42] |
|
Sample Input 3
5 -5 0 3 -1 2 |
Sample Output 3
[-5, 0, 3, -1, 2] |
|
Sample Input 4
3 100 200 300 |
Sample Output 4
[100, 200, 300] |
Constraints
1 ≤ N ≤ 100
5
Find Minimum and Maximum in a List
Easy
Write a program to find the smallest and largest element in a given list of integers.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print two integers separated by a space: min and max.
Sample Test Cases
|
Sample Input 1
6 45 12 78 3 99 23 |
Sample Output 1
3 99 |
|
Sample Input 2
3 -7 15 0 |
Sample Output 2
-7 15 |
|
Sample Input 3
1 5 |
Sample Output 3
5 5 |
|
Sample Input 4
4 -50 -20 -100 -10 |
Sample Output 4
-100 -10 |
Constraints
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
6
Reverse a List
Easy
Write a program to reverse a given list and print the reversed list.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the reversed list as space-separated integers.
Sample Test Cases
|
Sample Input 1
5 1 2 3 4 5 |
Sample Output 1
5 4 3 2 1 |
|
Sample Input 2
1 42 |
Sample Output 2
42 |
|
Sample Input 3
4 0 -1 -2 -3 |
Sample Output 3
-3 -2 -1 0 |
|
Sample Input 4
6 5 6 7 8 9 10 |
Sample Output 4
10 9 8 7 6 5 |
Constraints
1 ≤ N ≤ 10⁴
7
Check if Element Exists in List
Easy
Write a program to check if a given element X exists in the list. Print "Found" or "Not Found".
Input Format
First line contains integer N. Second line contains N space-separated integers. Third line contains X.
Output Format
Print "Found" if X exists, otherwise "Not Found".
Sample Test Cases
|
Sample Input 1
6 10 20 30 40 50 60 35 |
Sample Output 1
Not Found |
|
Sample Input 2
6 10 20 30 40 50 60 30 |
Sample Output 2
Found |
|
Sample Input 3
1 7 7 |
Sample Output 3
Found |
|
Sample Input 4
4 1 2 3 4 100 |
Sample Output 4
Not Found |
Constraints
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i], X ≤ 10⁶
8
Remove Duplicates from a List
Easy
Write a program to remove duplicate elements from a list while preserving the original order of first occurrences.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the list with duplicates removed, space-separated.
Sample Test Cases
|
Sample Input 1
8 4 2 4 3 2 1 3 5 |
Sample Output 1
4 2 3 1 5 |
|
Sample Input 2
5 1 2 3 4 5 |
Sample Output 2
1 2 3 4 5 |
|
Sample Input 3
5 10 10 10 10 10 |
Sample Output 3
10 |
|
Sample Input 4
6 -1 0 -1 1 2 0 |
Sample Output 4
-1 0 1 2 |
Constraints
1 ≤ N ≤ 10⁴, -10⁶ ≤ arr[i] ≤ 10⁶
9
Sort a List in Ascending Order
Easy
Write a program to sort a given list of integers in ascending order.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the sorted list as space-separated integers.
Sample Test Cases
|
Sample Input 1
7 9 3 7 1 5 2 8 |
Sample Output 1
1 2 3 5 7 8 9 |
|
Sample Input 2
4 10 20 30 40 |
Sample Output 2
10 20 30 40 |
|
Sample Input 3
5 3 -5 8 -10 0 |
Sample Output 3
-10 -5 0 3 8 |
|
Sample Input 4
3 2 1 1 |
Sample Output 4
1 1 2 |
Constraints
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
10
Find the Length of a List
Easy
Write a program to find and print the number of elements in a given list.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print a single integer — the length of the list.
Sample Test Cases
|
Sample Input 1
5 100 200 300 400 500 |
Sample Output 1
5 |
|
Sample Input 2
1 7 |
Sample Output 2
1 |
|
Sample Input 3
8 1 2 3 4 5 6 7 8 |
Sample Output 3
8 |
|
Sample Input 4
3 -10 0 15 |
Sample Output 4
3 |
Constraints
1 ≤ N ≤ 10⁵
1
Second Largest Element
Medium
Write a program to find the second largest element in a list of distinct integers.
Input Format
First line contains integer N (N ≥ 2). Second line contains N space-separated distinct integers.
Output Format
Print the second largest element.
Sample Test Cases
|
Sample Input 1
6 10 5 8 20 3 15 |
Sample Output 1
15 |
|
Sample Input 2
3 100 -5 50 |
Sample Output 2
50 |
|
Sample Input 3
2 5 10 |
Sample Output 3
5 |
|
Sample Input 4
7 99 12 45 23 78 42 33 |
Sample Output 4
78 |
Constraints
2 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
2
Rotate a List by K Positions
Medium
Write a program to rotate a list to the right by K positions. Elements at the end wrap around to the beginning.
Input Format
First line contains integer N. Second line contains N space-separated integers. Third line contains integer K (0 ≤ K ≤ 10⁹).
Output Format
Print the rotated list as space-separated integers.
Sample Test Cases
|
Sample Input 1
7 1 2 3 4 5 6 7 3 |
Sample Output 1
5 6 7 1 2 3 4 |
|
Sample Input 2
5 1 2 3 4 5 0 |
Sample Output 2
1 2 3 4 5 |
|
Sample Input 3
5 1 2 3 4 5 5 |
Sample Output 3
1 2 3 4 5 |
|
Sample Input 4
4 1 2 3 4 2 |
Sample Output 4
3 4 1 2 |
Constraints
1 ≤ N ≤ 10⁵, 0 ≤ K ≤ 10⁹
3
Merge Two Sorted Lists
Medium
Write a program to merge two sorted lists into a single sorted list.
Input Format
First line contains integer N. Second line contains N sorted integers. Third line contains M. Fourth line contains M sorted integers.
Output Format
Print the merged sorted list as space-separated integers.
Sample Test Cases
|
Sample Input 1
4 1 3 5 7 4 2 4 6 8 |
Sample Output 1
1 2 3 4 5 6 7 8 |
|
Sample Input 2
3 1 2 3 3 4 5 6 |
Sample Output 2
1 2 3 4 5 6 |
|
Sample Input 3
3 1 2 3 3 1 4 5 |
Sample Output 3
1 1 2 3 4 5 |
|
Sample Input 4
2 -5 3 2 0 10 |
Sample Output 4
-5 0 3 10 |
Constraints
1 ≤ N, M ≤ 10⁵
4
Most Frequent Element
Medium
Write a program to find the element that appears most frequently in a list. If tied, print the smallest such element.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the most frequent element (smallest in case of tie).
Sample Test Cases
|
Sample Input 1
9 3 1 4 1 5 3 1 9 3 |
Sample Output 1
1 |
|
Sample Input 2
7 5 5 5 2 2 2 2 |
Sample Output 2
2 |
|
Sample Input 3
5 3 3 3 3 3 |
Sample Output 3
3 |
|
Sample Input 4
8 -1 -1 0 0 1 1 2 2 |
Sample Output 4
-1 |
Constraints
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
5
Remove Elements at Even Indices
Medium
Write a program to remove all elements at even indices (0-based) from a list and print the remaining elements.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the elements at odd indices as space-separated values. If none, print nothing.
Sample Test Cases
|
Sample Input 1
6 10 20 30 40 50 60 |
Sample Output 1
20 40 60 |
|
Sample Input 2
1 42 |
Sample Output 2
|
|
Sample Input 3
5 1 2 3 4 5 |
Sample Output 3
2 4 |
|
Sample Input 4
4 a b c d |
Sample Output 4
b d |
Constraints
1 ≤ N ≤ 10⁴
6
Intersection of Two Lists
Medium
Write a program to find the intersection of two lists (common elements without duplicates). Return in order of first appearance in the first list.
Input Format
First line contains integer N. Second line contains N space-separated integers. Third line contains M. Fourth line contains M space-separated integers.
Output Format
Print common elements space-separated. If none, print nothing.
Sample Test Cases
|
Sample Input 1
6 1 2 3 4 5 6 5 4 5 6 7 8 |
Sample Output 1
4 5 6 |
|
Sample Input 2
4 1 2 3 4 4 5 6 7 8 |
Sample Output 2
|
|
Sample Input 3
4 1 2 1 2 3 2 1 3 |
Sample Output 3
1 2 |
|
Sample Input 4
5 0 5 10 15 20 4 0 10 100 200 |
Sample Output 4
0 10 |
Constraints
1 ≤ N, M ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
7
Check if List is Palindrome
Medium
Write a program to check if a list reads the same forwards and backwards.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print "Palindrome" or "Not Palindrome".
Sample Test Cases
|
Sample Input 1
5 1 2 3 2 1 |
Sample Output 1
Palindrome |
|
Sample Input 2
4 1 2 3 4 |
Sample Output 2
Not Palindrome |
|
Sample Input 3
1 7 |
Sample Output 3
Palindrome |
|
Sample Input 4
6 1 2 2 3 3 1 |
Sample Output 4
Not Palindrome |
Constraints
1 ≤ N ≤ 10⁵
8
Flatten a Nested List
Medium
Write a program to flatten a list of lists (2D) into a single 1D list in row-major order.
Input Format
First line contains integer R (rows). Next R lines: C followed by C integers.
Output Format
Print the flattened list as space-separated integers.
Sample Test Cases
|
Sample Input 1
3 3 1 2 3 2 4 5 4 6 7 8 9 |
Sample Output 1
1 2 3 4 5 6 7 8 9 |
|
Sample Input 2
2 3 10 20 30 1 40 |
Sample Output 2
10 20 30 40 |
|
Sample Input 3
1 1 5 |
Sample Output 3
5 |
|
Sample Input 4
4 1 1 1 2 0 1 3 |
Sample Output 4
1 2 3 |
Constraints
1 ≤ R, C ≤ 100
9
Move Zeros to End
Medium
Write a program to move all zeros in a list to the end while preserving the order of non-zero elements.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the modified list as space-separated integers.
Sample Test Cases
|
Sample Input 1
8 0 1 0 3 12 0 5 0 |
Sample Output 1
1 3 12 5 0 0 0 0 |
|
Sample Input 2
4 1 2 3 4 |
Sample Output 2
1 2 3 4 |
|
Sample Input 3
5 0 0 0 0 0 |
Sample Output 3
0 0 0 0 0 |
|
Sample Input 4
6 5 0 5 0 5 0 |
Sample Output 4
5 5 5 0 0 0 |
Constraints
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
10
Split List into Chunks
Medium
Write a program to split a list into chunks of size K. The last chunk may be smaller.
Input Format
First line contains integer N. Second line contains N space-separated integers. Third line contains K (chunk size).
Output Format
Print each chunk on a new line as space-separated integers.
Sample Test Cases
|
Sample Input 1
8 1 2 3 4 5 6 7 8 3 |
Sample Output 1
1 2 3 4 5 6 7 8 |
|
Sample Input 2
4 1 2 3 4 1 |
Sample Output 2
1 2 3 4 |
|
Sample Input 3
6 1 2 3 4 5 6 6 |
Sample Output 3
1 2 3 4 5 6 |
|
Sample Input 4
5 10 20 30 40 50 2 |
Sample Output 4
10 20 30 40 50 |
Constraints
1 ≤ N ≤ 10⁴, 1 ≤ K ≤ N
1
Longest Consecutive Subsequence
Hard
Write a program to find the length of the longest consecutive elements sequence in an unsorted list. Must run in O(N) time.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print a single integer — the length of the longest consecutive sequence.
Sample Test Cases
|
Sample Input 1
9 100 4 200 1 3 2 5 6 7 |
Sample Output 1
5 |
|
Sample Input 2
5 10 20 30 40 50 |
Sample Output 2
1 |
|
Sample Input 3
7 1 1 2 2 3 3 5 |
Sample Output 3
3 |
|
Sample Input 4
1 42 |
Sample Output 4
1 |
Constraints
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
2
Find All Triplets That Sum to Zero
Hard
Write a program to find all unique triplets in a list that sum to zero. Triplets should be sorted internally, no duplicates.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print each triplet on a new line (ascending). If none, print "No triplets found".
Sample Test Cases
|
Sample Input 1
6 -1 0 1 2 -1 -4 |
Sample Output 1
-1 -1 2 -1 0 1 |
|
Sample Input 2
3 1 2 3 |
Sample Output 2
No triplets found |
|
Sample Input 3
4 0 0 0 0 |
Sample Output 3
0 0 0 |
|
Sample Input 4
5 -2 1 1 0 2 |
Sample Output 4
-2 0 2 -2 1 1 |
Constraints
3 ≤ N ≤ 3000, -10⁵ ≤ arr[i] ≤ 10⁵
3
Maximum Subarray Sum (Kadane's Algorithm)
Hard
Write a program to find the maximum sum of any contiguous subarray using Kadane's algorithm.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print a single integer — the maximum subarray sum.
Sample Test Cases
|
Sample Input 1
9 -2 1 -3 4 -1 2 1 -5 4 |
Sample Output 1
6 |
|
Sample Input 2
5 1 2 3 4 5 |
Sample Output 2
15 |
|
Sample Input 3
5 -1 -2 -3 -4 -5 |
Sample Output 3
-1 |
|
Sample Input 4
1 10 |
Sample Output 4
10 |
Constraints
1 ≤ N ≤ 10⁵, -10⁴ ≤ arr[i] ≤ 10⁴
4
Product of Array Except Self
Hard
Write a program to compute an array where each element is the product of all elements except itself — without division, O(N) time.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print N space-separated integers.
Sample Test Cases
|
Sample Input 1
5 1 2 3 4 5 |
Sample Output 1
120 60 40 30 24 |
|
Sample Input 2
3 0 1 2 |
Sample Output 2
0 0 0 |
|
Sample Input 3
4 -1 2 -3 1 |
Sample Output 3
-6 3 -2 6 |
|
Sample Input 4
2 1 2 |
Sample Output 4
2 1 |
Constraints
2 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
5
Find the Missing Number (1 to N)
Hard
Write a program to find the missing number from N-1 distinct integers from 1 to N.
Input Format
First line contains N. Second line contains N-1 distinct integers (one missing).
Output Format
Print the missing number.
Sample Test Cases
|
Sample Input 1
6 1 2 4 5 6 |
Sample Output 1
3 |
|
Sample Input 2
5 1 2 3 4 |
Sample Output 2
5 |
|
Sample Input 3
5 2 3 4 5 |
Sample Output 3
1 |
|
Sample Input 4
2 1 |
Sample Output 4
2 |
Constraints
2 ≤ N ≤ 10⁵
6
Majority Element II
Hard
Write a program to find all elements that appear more than ⌊N/3⌋ times.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print majority elements ascending. If none, print "None".
Sample Test Cases
|
Sample Input 1
8 3 2 3 1 2 3 2 3 |
Sample Output 1
2 3 |
|
Sample Input 2
5 1 1 1 2 3 |
Sample Output 2
1 |
|
Sample Input 3
5 1 2 3 4 5 |
Sample Output 3
None |
|
Sample Input 4
5 1 1 2 2 3 |
Sample Output 4
None |
Constraints
1 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹
7
Subarray with Given Sum
Hard
Write a program to find a contiguous subarray that sums to target S. Return start and end indices (0-based).
Input Format
First line contains N and S. Second line contains N non-negative integers.
Output Format
Print start and end (inclusive). If not found, print "-1 -1".
Sample Test Cases
|
Sample Input 1
5 12 1 2 3 7 5 |
Sample Output 1
1 3 |
|
Sample Input 2
4 15 5 3 2 5 |
Sample Output 2
0 3 |
|
Sample Input 3
3 100 1 2 3 |
Sample Output 3
-1 -1 |
|
Sample Input 4
6 0 1 2 0 3 4 5 |
Sample Output 4
2 2 |
Constraints
1 ≤ N ≤ 10⁵, 0 ≤ arr[i] ≤ 10⁹, 0 ≤ S ≤ 10⁹
8
Rearrange Array Alternately
Hard
Write a program to rearrange a sorted list alternately: max, min, second max, second min, etc.
Input Format
First line contains integer N. Second line contains N sorted integers.
Output Format
Print the rearranged list as space-separated integers.
Sample Test Cases
|
Sample Input 1
6 1 2 3 4 5 6 |
Sample Output 1
6 1 5 2 4 3 |
|
Sample Input 2
5 1 2 3 4 5 |
Sample Output 2
5 1 4 2 3 |
|
Sample Input 3
4 1 2 3 4 |
Sample Output 3
4 1 3 2 |
|
Sample Input 4
3 -5 0 10 |
Sample Output 4
10 -5 0 |
Constraints
1 ≤ N ≤ 10⁵, -10⁶ ≤ arr[i] ≤ 10⁶
9
Sort 0s, 1s, and 2s (Dutch National Flag)
Hard
Write a program to sort a list of 0s, 1s, 2s in O(N) time and O(1) space.
Input Format
First line contains N. Second line contains N integers ∈ {0, 1, 2}.
Output Format
Print the sorted list.
Sample Test Cases
|
Sample Input 1
10 2 0 2 1 1 0 1 2 0 1 |
Sample Output 1
0 0 0 1 1 1 1 2 2 2 |
|
Sample Input 2
5 0 0 0 0 0 |
Sample Output 2
0 0 0 0 0 |
|
Sample Input 3
4 2 1 2 1 |
Sample Output 3
1 1 2 2 |
|
Sample Input 4
3 2 1 0 |
Sample Output 4
0 1 2 |
Constraints
1 ≤ N ≤ 10⁵
10
Find Pair with Maximum Product
Hard
Write a program to find a pair of elements (distinct indices) whose product is maximum.
Input Format
First line contains integer N. Second line contains N space-separated integers.
Output Format
Print the pair sorted ascending.
Sample Test Cases
|
Sample Input 1
6 -10 -3 5 6 -20 2 |
Sample Output 1
-20 -10 |
|
Sample Input 2
5 1 2 3 4 5 |
Sample Output 2
4 5 |
|
Sample Input 3
3 -5 -2 -1 |
Sample Output 3
-5 -2 |
|
Sample Input 4
2 7 3 |
Sample Output 4
3 7 |
Constraints
2 ≤ N ≤ 10⁵, -10⁹ ≤ arr[i] ≤ 10⁹