50+ array questions with solutions (easy, medium, hard)

Array interview questions

To ace your coding interview for a software engineering job, you’ll need to understand arrays. They come up frequently in coding interviews and are fundamental to many other data structures too.

Let’s take a look at some array questions that come up in interviews. 

6 typical array interview questions

  • Given a sorted array, return the index of a given value, or -1 if the element cannot be found.
  • What is the time complexity required to find if an array of sorted integers contains a given integer?
  • Given an array with all integers between 1 and 100 except for one, find the missing number.
  • Given a 2D array of integers, rotate clockwise without using additional memory.
  • If you have two sorted arrays, how can you merge them and keep the resulting array sorted?
  • Given unlimited coins in denominations of 1c, 2c, and 5c, how many different ways can you make a total of 20c? Can you solve the general version of this problem for an arbitrary target amount and a given list of denominations?

Below, we take a look at some more questions and provide you with links to high quality solutions to them. We explain how arrays work, their variations, and the most important things you need to know about them, including a useful 'cheat sheet' to remind you of the key points at a glance.

This is an overview of what we’ll cover:

  • Easy array interview questions
  • Medium array interview questions
  • Hard array interview questions
  • Array basics
  • Array cheat sheet
  • Mock interviews for software engineers

Click here to practice coding interviews with ex-FAANG interviewers

1. easy array interview questions.

You might be tempted to try to read all of the possible questions and memorize the solutions, but this is not feasible. Interviewers will always try to find new questions, or ones that are not available online. Instead, you should use these questions to practice the fundamental concepts of arrays.

As you consider each question, try to replicate the conditions you’ll encounter in your interview. Begin by writing your own solution without external resources in a fixed amount of time.

If you get stuck, go ahead and look at the solutions, but then try the next one alone again. Don’t get stuck in a loop of reading as many solutions as possible! We’ve analysed dozens of questions and selected ones that are commonly asked and have clear and high-quality answers.

Here are some of the easiest questions you might get asked in a coding interview. These questions are often asked during the ‘phone screen’ stage, so you should be comfortable answering them without being able to write code or use a whiteboard.

1.1 Merge two sorted arrays 

  • Text guide (GeeksforGeeks)
  • Video guide (TECH DOSE)

1.2 Remove duplicates from an array

  • Video guide (Kevin Naughton Jr.)
  • Text guide (W3Schools)
  • Text guide (Javarevisted)
  • Code example (LeetCode)

1.3 Count the frequency of an element in an array

  • Video guide (SDET)

1.4 Two sum

  • Text guide (Codeburst)

1.5 Find the minimum (or maximum) element of an array

  • Text guide (Enjoy Algorithms)
  • Text guide (After Academy)
  • Video guide (GeeksforGeeks)

1.6 Remove duplicates from sorted array

  • Text guide (Redquark)
  • Video guide (Take u Forward)

1.7 Remove element in-place

  • Video guide (Nick White)
  • Code example (LeetCode) 

1.8 Search Insert Position

  • Text guide (Codesdope)
  • Video guide (NeetCode)

1.9 Maximum Subarray

  • Text guide (Wikipedia)
  • Text guide (Techie Delight)
  • Video guide (CS Dojo)

1.10 Plus One

  • Text guide (Medium/Punitkmryh)
  • Video guide (Back to Back SWE)

1.11 Convert Sorted Array to Binary Search Tree (Arrays/Binary Trees)

  • Text guide (GeeksForGeeks)
  • Video guide (Kevin Naughton Jr)

1.12 Single Number

  • Text guide (Akhilpokle)

1.13 Count Primes

  • Video guide (Terrible Whiteboard)

1.14 Contains Duplicate

1.15 third largest number, 1.16 count odd even.

  • Text guide (W3resource)
  • Video guide (Technotip)

2. Medium array interview questions

Here are some moderate-level questions that are often asked in a video call or onsite interview. You should be prepared to write code or sketch out the solutions on a whiteboard if asked.

2.1 Move all zeros to the beginning/end of an array 

  • Text guide (Educative) 
  • Video guide (Programming tutorials)

2.2 Find if a given element is in a sorted array (binary search)

  • Text guide (Khan academy)
  • Video guide (HackerRank)

2.3 Rotate an array 

2.4 largest sum of non-adjacent numbers (dynamic programming).

  • Text guide (Medium/Arun Kumar)
  • Video guide (Coding Simplified)

2.5 A Product Array Puzzle 

  • Text guide (TutorialCup)

2.6 Maximum Product Subarray (Dynamic programming)

2.7 shortest unsorted continuous subarray.

  • Text guide (Seanpgallivan)

2.8 Maximum sum of hour glass in matrix

  • Video guide (Over The Shoulder Coding)

2.9 Paint House (Dynamic programming)

  • Text guide (ProgrammerSought) 

2.10 Minimum number of jumps to reach end

  • Text guide (Medium/Himanshu)

2.11 Find duplicates in O(n) time and O(1) extra space

2.12 find three numbers with the maximum product.

  • Video guide (Programmer Mitch)

2.13 Maximum Sum Circular Subarray

  • Text Guide (Techie Delight)
  • Video Guide (TECH DOSE)

2.14 Minimum number of swaps to sort an array

  • Video guide (Brian Dyck)

3. Hard array interview questions

Similar to the moderate section, these more difficult questions may be asked in an onsite or video call interview. You will likely be given more time if you are expected to create a full solution.

3.1 Rotate a 2D array 

  • Text guide (Jack)
  • Text guide (GeeksforGeeks) 
  • Video guide (Nick White) 

3.2 Create change with coins (dynamic programming)

  • Video guide (Back to Back SWE) 

3.3 Sliding window maximum

  • Video guide (Jessica Lin)

3.4 Find the smallest positive number missing from an unsorted array

  • Text guide (Codes Dope)
  • Video guide (Michael Muinos)

3.5 Find the missing number in unordered Arithmetic Progression

3.6 find the maximum j – i such that arr[j] > arr[i] (distance maximising problem).

  • Video guide (Genetic Coders)

3.7 Array manipulation

  • Text guide (The Poor Coder)

3.8 Median of Two Sorted Arrays

3.9 sudoku solver.

  • Video guide (Back To Back SWE)

3.10 Largest Rectangle in Histogram

3.11 maximal rectangle in binary matrix, 3.12 find minimum in rotated sorted array .

  • Text guide (Algorithmsandme)

3.13 Count of Smaller Numbers After Self

  • Text guide (CodeStudio)
  • Video guide (Happygirlzt)

3.14 Palindrome Pairs

3.15 sort an array containing 0’s, 1’s and 2’s.

  • Text guide   (Techie Delight)

3.16 Longest increasing subsequence

3.17 trapping rain water , 4. array basics.

In order to crack the questions above and others like them, you’ll need to have a strong understanding of arrays, how they work, and when to use them. Let’s get into it.

4.1 What is an array?

An array is a list-like data structure that contains a collection of values , each associated with a specific index , usually with a fixed overall size. For example, the image below shows an array that has space for up to nine elements, but contains only four. This array has the integers 1, 2, 3, and 4 as its values and these are at the “zeroth”, first, second, and third indices respectively.

problem solving with arrays

Arrays are one of the most fundamental data structures in programming and computer science, and many more complex data structures are built using arrays. The array itself is not always as simple as it might seem, and it forms the basis for many tricky interview questions.

4.1.1 Types of arrays (Java, Python, C++)

Interviewers often ask questions about “arrays”, as if it cleanly refers to a single concept. In reality, there are different types of arrays, and different languages implement arrays in different ways, leading to some confusion and complexity. Mainstream programming languages offer a default built-in array implementation (e.g. `list` in Python, or `int []` in Java and C++), and usually offer alternative implementations that the user can import from a standard library.

In many languages, including Java, default arrays are static and homogenous. Static means that the size of the array (the number of elements that it can hold) has to be declared upfront, when the array is created. Homogenous means that all of the elements in the array must be of the same type - e.g. an array of integers cannot contain string or float elements.

In other languages, including Python, the default array (`list`) is dynamic and heterogeneous. This means that they can be resized dynamically at run time, and can contain a mix of different types.

You will also often encounter nested or multidimensional arrays (often called a matrix). For 2D arrays, you can usually think of these as tables with rows and columns.

Because array terminology and implementation differs across languages, it’s always a good idea to check your assumptions about a specific array question with your interviewer.

4.1.2 How arrays store data

As with strings, data stored in arrays is traditionally kept in the heap of computer memory. If you store a basic integer in a variable with a statement like `int x = 1;`, that value is stored on the stack. To answer many array-related interview questions, you should understand the fundamentals of stack vs heap .

Data in the heap has to be cleared manually in languages like C, or by the garbage collector in languages such as Java. You should be prepared to answer questions about the implications of this (for example, how it could lead to a memory leak ).

Because arrays need to store data in contiguous blocks of memory, the programmer often needs to be aware of tradeoffs around space and time when it comes to using arrays. 

  • If you don’t reserve enough space in your array, you waste time as you have to allocate a new array.
  • If you reserve too much space, this is a waste of resources and could impact the requirements of your program, or other running programs.

Adding even a single element to a ‘full’ array is an expensive operation. A new (bigger) array has to be allocated, and every single element has to be copied across. Only then can the new element be added. 

A common approach that languages use for dynamic arrays is to double their allocated size every time they become full. So if you need to add an 11th item to an array of size 10, the library will create a new array of size 20 and copy across the existing data.

This means that as you are adding elements to an array, most inserts will be fast, but your code will slow down significantly every time it triggers a resize.

4.1.3 How arrays compare to other data structures

Because strings are usually implemented as arrays of characters, many interview questions for arrays can be phrased as string interview questions, and vice-versa. 

Arrays are also closely related to linked lists, and many questions will expect you to be able to explain the differences between them, and when one has an advantage over the other.

Finally, arrays are often contrasted with sets. When you want to get data at a specific index (e.g. “I need the fifth element in this list”), arrays perform better than sets, as you can access any given element by its index in O(1) time. 

If you need to check if a specific value is contained in the array (“Does my array contain the value 5 at any position?”), arrays are not efficient. You need to loop through every single value to see if it matches what you are looking for, while sets can provide this in O(1) time.

Need a boost in your career as a software engineer?

If you want to improve your skills as an engineer or leader, tackle an issue at work, get promoted, or understand the next steps to take in your career, book a session with one of our software engineering coaches.

5. Array cheat sheet

You can download the cheat sheet  here.

5.1 Related algorithms and techniques

  • Binary search  
  • Dynamic Programming  
  • Converting to a Set  
  • Sliding window  
  • Two pointers  
  • Prefix sum  
  • Recursion  
  • Searching  
  • Looping  
  • Sorting  

5.2 Related concepts

  • Homogeneous (elements have same type)
  • Dynamic (size can change)

5.3 Cheat sheet explained

The cheat sheet above is a summary of information you might need to know for an interview, but it’s usually not enough to simply memorize it. Instead, aim to understand each result so that you can give the answer in context.

The cheat sheet is broken into time complexity (the processing time for the various array operations) and space complexity (the amount of memory required). While you might be asked about these directly in relation to the array data structure, it’s more likely that you will need to know these in relation to specific array-related algorithms, such as searching and sorting, which is what the third section details.

For more information about time and space requirements of different algorithms, read our  complete guide to big-O notation and complexity analysis .

5.3.1 Time complexity

For time complexity, some of the results are fairly intuitive. For example, accessing any element of an array is always O(1) as arrays are stored in contiguous memory, so accessing the 100th element is no harder than accessing the first one, and this is true for updating any specific element too.

Deleting or inserting an element can require us to touch every single other element in some cases, so this is O(n) in the worst case. For example, if we have an array of size 10 and we want to add an 11th element, we need to copy each element to a new array first, and then add the new one. However, this is rare, as we would usually double the size of the array every time we run out of space, making future inserts faster. Thus the amortized complexity is still constant as we can ‘pay off’ the expensive operation over time.

The time complexity is similar when searching for an element by value, where in the worst case we need to look at every single element before finding our target, but if we ‘get lucky’ we might find it in the first place we look (probably at the start of the array), so our best case is O(1).

5.3.2 Space complexity

In most cases, the space complexity of an array is simply the number of elements, so this is O(n). In some contexts, the array might be some (small) constant size, which means the space complexity is simplified to O(1). Space complexity is almost always only relevant in the context of a specific algorithm, which we cover in the next section.

5.3.3 Array algorithms complexity

We’ve listed the algorithms that interviewers will most frequently discuss while asking about arrays, but there are dozens of other search algorithms and sorting algorithms. One of the most important aspects to understand is the tradeoff between mergesort and quicksort. Quicksort works in place, so does not require additional memory, while Mergesort uses an auxiliary array, and therefore uses more space. On the flip side, the worst time complexity of mergesort is better than that of quicksort which can in some cases (e.g. when the array is already sorted) perform as badly as a naive bubble sort.

For the search algorithms, a key insight to understand is that binary search is log(n) as we can eliminate half of the array with each operation. Therefore doubling the size of the array requires only one more operation. By contrast, a linear search looks at every element until it finds the target, so doubling the size of the array also requires, on average, twice as many operations.

For example, searching an element using binary search in an array of one million elements needs a maximum of 20 comparisons. Doubling the array (two million elements) would only add one extra comparison (a total of 21 comparisons). By contrast, a linear search would need one million comparisons and doubling the array would also double the number of comparisons (to two million).

6. Mock interviews for software engineers

Before you start practicing interviews, you’ll want to make sure you have a strong understanding of not only linked lists but also the rest of the relevant data structures. Check out our guides for questions, explanations and helpful cheat sheets.

  • Linked lists
  • Stacks and Queues
  • Coding interview examples (with solutions)

Once you’re confident on all the topics, you’ll want to start practicing answering coding questions in an interview situation.

One way of doing this is by practicing out loud, which is a very underrated way of preparing. However, sooner or later you’re probably going to want some expert interventions and feedback to really improve your interview skills.

That’s why we recommend practicing with ex-interviewers from top tech companies. If you know a software engineer who has experience running interviews at a big tech company, then that's fantastic. But for most of us, it's tough to find the right connections to make this happen. And it might also be difficult to practice multiple hours with that person unless you know them really well.

Here's the good news. We've already made the connections for you. We’ve created a coaching service where you can practice system design interviews 1-on-1 with ex-interviewers from leading tech companies. Learn more and start scheduling sessions today.

Related articles:

problem solving with arrays

CodingDrills logo

Techniques for Solving Array Problems

Array challenges and problems.

In the world of programming, arrays are versatile and essential data structures, but they often come with their own set of challenges. From searching for elements to optimizing performance, solving array problems efficiently is a valuable skill for any developer. In this guide, we will explore common array challenges and provide techniques to overcome them.

Duplicate Elements in an Array

One common challenge is identifying and handling duplicate elements within an array. Whether you're working with user data or need to remove redundant values, you can use techniques like hash sets or sorting to detect and eliminate duplicates.

Example: Removing Duplicates in Python

Finding an element in an unsorted array.

Searching for an element in an unsorted array can be inefficient. Utilize techniques like linear search to locate an element by iterating through the array one element at a time.

Example: Linear Search in C++

Optimizing Array Traversal

Efficiently traversing arrays is crucial for improving program performance. Utilize techniques like caching or parallelization to speed up array processing, especially when dealing with large datasets.

Handling Array Edge Cases

Array challenges often involve managing edge cases, such as empty arrays or arrays with a single element. Always consider these scenarios when designing your array-related algorithms.

Sorting Arrays

Sorting is a fundamental operation when working with arrays. Techniques like quicksort, mergesort, or the built-in sorting functions in your programming language can help you organize array elements efficiently.

Example: Quicksort in Python

Handling Multi-dimensional Arrays

When working with multi-dimensional arrays, techniques like nested loops and matrix operations can simplify complex tasks like matrix multiplication or image processing.

Solving array challenges and problems is a critical aspect of programming. This guide has explored various techniques, provided code examples, and offered strategies for tackling common array-related issues. By mastering these techniques, you'll become a more proficient and resourceful developer, equipped to handle a wide range of programming tasks.

As you continue your programming journey, remember that array challenges are a stepping stone to more complex data structures and problem-solving. Practice and experience will make you even more adept at handling arrays and other programming challenges.

CodingDrills logo

Hi, I'm Ada, your personal AI tutor. I can help you with any coding tutorial. Go ahead and ask me anything.

I have a question about this topic

Give more examples

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

Top 50 Array Problems with Solutions A handpicked selection of array-based coding problems for interview preparation, with documented solutions in popular languages. Boost your problem-solving skills and contribute to this open-source project. Happy coding!

pranaydas1/Top-50-Array-Problems

Folders and files.

NameName
12 Commits

Repository files navigation

Geeks for geeks - top 50 array problems.

Welcome to the "Geeks for Geeks - Top 50 Array Problems" repository! This collection includes problem statements and solutions for the "Top 50 Array Problems" from the Geeks for Geeks platform. These problems are a valuable resource for improving your data structures and algorithm skills and preparing for coding interviews.

Visit the Website

https://practice.geeksforgeeks.org/explore?page=1&sprint=50746f92a895c22a50504ac0c1fb9c84&sortBy=submissions&sprint_name=Top%2050%20Array%20Problems

Repository Overview

This repository is organized in a question-by-question format, with each problem statement and its corresponding solution available. Please note that these solutions are specifically designed for the Geeks for Geeks platform, as they rely on predefined input and test cases provided by Geeks for Geeks. As a result, they may not work in other code editors or environments.

Choose a Problem: Browse the repository to find a specific problem you'd like to practice.

Read the Problem Statement: Open the README file associated with the problem to read its description, constraints, and requirements.

Review the Solution: Explore the solution code provided in the accompanying code file. These solutions are tailored to work with Geeks for Geeks' predefined input and test cases.

Visit Geeks for Geeks: To practice and submit your solutions, visit the Geeks for Geeks platform ( https://www.geeksforgeeks.org/ ) and navigate to the respective problem. Use the provided solution in this repository as a reference to create your solution on the Geeks for Geeks platform.

Submit Your Solution: After solving the problem on the Geeks for Geeks platform, you can test it using their predefined test cases and submit your solution for evaluation.

Contributions

This repository is open for contributions. If you have alternative solutions, improvements, or suggestions, feel free to create a pull request. Contributions are a valuable way to enhance the quality and variety of solutions available.

Please note that the solutions provided here are intended for learning and reference purposes, specifically for use on the Geeks for Geeks platform. They may not work in other coding environments due to the platform-specific input and test cases.

Happy coding and best of luck with your Geeks for Geeks practice!

Array cheatsheet for coding interviews

Introduction ​.

Arrays hold values of the same type at contiguous memory locations. In an array, we're usually concerned about two things - the position/index of an element and the element itself. Different programming languages implement arrays under the hood differently and can affect the time complexity of operations you make to the array. In some languages like Python, JavaScript, Ruby, PHP, the array (or list in Python) size is dynamic and you do not need to have a size defined beforehand when creating the array. As a result, people usually have an easier time using these languages for interviews.

Arrays are among the most common data structures encountered during interviews. Questions which ask about other topics would likely involve arrays/sequences as well. Mastery of array is essential for interviews!

  • Store multiple elements of the same type with one single variable name
  • Accessing elements is fast as long as you have the index, as opposed to linked lists where you have to traverse from the head.

Disadvantages

  • Addition and removal of elements into/from the middle of an array is slow because the remaining elements need to be shifted to accommodate the new/missing element. An exception to this is if the position to be inserted/removed is at the end of the array.
  • For certain languages where the array size is fixed, it cannot alter its size after initialization. If an insertion causes the total number of elements to exceed the size, a new array has to be allocated and the existing elements have to be copied over. The act of creating a new array and transferring elements over takes O(n) time.

Learning resources ​

  • Array in Data Structure: What is, Arrays Operations , Guru99
  • Arrays , University of California San Diego

Common terms ​

Common terms you see when doing problems involving arrays:

  • Example: given an array [2, 3, 6, 1, 5, 4] , [3, 6, 1] is a subarray while [3, 1, 5] is not a subarray.
  • Example: given an array [2, 3, 6, 1, 5, 4] , [3, 1, 5] is a subsequence but [3, 5, 1] is not a subsequence.

Time complexity ​

OperationBig-ONote
AccessO(1)
SearchO(n)
Search (sorted array)O(log(n))
InsertO(n)Insertion would require shifting all the subsequent elements to the right by one and that takes O(n)
Insert (at the end)O(1)Special case of insertion where no other element needs to be shifted
RemoveO(n)Removal would require shifting all the subsequent elements to the left by one and that takes O(n)
Remove (at the end)O(1)Special case of removal where no other element needs to be shifted

Things to look out for during interviews ​

  • Clarify if there are duplicate values in the array. Would the presence of duplicate values affect the answer? Does it make the question simpler or harder?
  • When using an index to iterate through array elements, be careful not to go out of bounds.
  • Be mindful about slicing or concatenating arrays in your code. Typically, slicing and concatenating arrays would take O(n) time. Use start and end indices to demarcate a subarray/range where possible.

Corner cases ​

  • Empty sequence
  • Sequence with 1 or 2 elements
  • Sequence with repeated elements
  • Duplicated values in the sequence

Techniques ​

Note that because both arrays and strings are sequences (a string is an array of characters), most of the techniques here will apply to string problems.

Sliding window ​

Master the sliding window technique that applies to many subarray/substring problems. In a sliding window, the two pointers usually move in the same direction will never overtake each other. This ensures that each value is only visited at most twice and the time complexity is still O(n). Examples: Longest Substring Without Repeating Characters , Minimum Size Subarray Sum , Minimum Window Substring

Two pointers ​

Two pointers is a more general version of sliding window where the pointers can cross each other and can be on different arrays. Examples: Sort Colors , Palindromic Substrings

When you are given two arrays to process, it is common to have one index per array (pointer) to traverse/compare the both of them, incrementing one of the pointers when relevant. For example, we use this approach to merge two sorted arrays. Examples: Merge Sorted Array

Traversing from the right ​

Sometimes you can traverse the array starting from the right instead of the conventional approach of from the left. Examples: Daily Temperatures , Number of Visible People in a Queue

Sorting the array ​

Is the array sorted or partially sorted? If it is, some form of binary search should be possible. This also usually means that the interviewer is looking for a solution that is faster than O(n).

Can you sort the array? Sometimes sorting the array first may significantly simplify the problem. Obviously this would not work if the order of array elements need to be preserved. Examples: Merge Intervals , Non-overlapping Intervals

Precomputation ​

For questions where summation or multiplication of a subarray is involved, pre-computation using hashing or a prefix/suffix sum/product might be useful. Examples: Product of Array Except Self , Minimum Size Subarray Sum , LeetCode questions tagged "prefix-sum"

Index as a hash key ​

If you are given a sequence and the interviewer asks for O(1) space, it might be possible to use the array itself as a hash table. For example, if the array only has values from 1 to N, where N is the length of the array, negate the value at that index (minus one) to indicate presence of that number. Examples: First Missing Positive , Daily Temperatures

Traversing the array more than once ​

This might be obvious, but traversing the array twice/thrice (as long as fewer than n times) is still O(n). Sometimes traversing the array more than once can help you solve the problem while keeping the time complexity to O(n).

Essential questions ​

These are essential questions to practice if you're studying for this topic.

  • Best Time to Buy and Sell Stock
  • Product of Array Except Self
  • Maximum Subarray

Recommended practice questions ​

These are recommended questions to practice after you have studied for the topic and have practiced the essential questions.

  • Contains Duplicate
  • Maximum Product Subarray
  • Search in Rotated Sorted Array
  • Container With Most Water
  • Sliding Window Maximum

Recommended courses ​

Algomonster ​.

AlgoMonster aims to help you ace the technical interview in the shortest time possible . By Google engineers, AlgoMonster uses a data-driven approach to teach you the most useful key question patterns and has contents to help you quickly revise basic data structures and algorithms. Best of all, AlgoMonster is not subscription-based - pay a one-time fee and get lifetime access . Join today for a 70% discount →

Grokking the Coding Interview: Patterns for Coding Questions ​

This course on by Design Gurus expands upon the questions on the recommended practice questions but approaches the practicing from a questions pattern perspective, which is an approach I also agree with for learning and have personally used to get better at coding interviews. The course allows you to practice selected questions in Java, Python, C++, JavaScript and also provides sample solutions in those languages along with step-by-step visualizations. Learn and understand patterns, not memorize answers! Get lifetime access now →

Master the Coding Interview: Data Structures + Algorithms ​

This Udemy bestseller is one of the highest-rated interview preparation course (4.6 stars, 21.5k ratings, 135k students) and packs 19 hours worth of contents into it. Like Tech Interview Handbook, it goes beyond coding interviews and covers resume, non-technical interviews, negotiations. It's an all-in-one package! Note that JavaScript is being used for the coding demos. Check it out →

Table of Contents

  • Introduction
  • Learning resources
  • Common terms
  • Time complexity
  • Things to look out for during interviews
  • Corner cases
  • Essential questions
  • Recommended practice questions
  • Recommended courses
  • Interview Preparation Kit

2D Array - DS Easy Problem Solving (Basic) Max Score: 15 Success Rate: 93.16%

Arrays: left rotation easy problem solving (basic) max score: 20 success rate: 91.24%, new year chaos medium problem solving (basic) max score: 40 success rate: 67.60%, minimum swaps 2 medium problem solving (intermediate) max score: 40 success rate: 77.98%, array manipulation hard problem solving (intermediate) max score: 60 success rate: 61.64%, cookie support is required to access hackerrank.

Seems like cookies are disabled on this browser, please enable them to open this website

  • C Programming Home
  • ▼C Programming Exercises
  • Basic Declarations and Expressions
  • Basic Part-II
  • Basic Algorithm
  • Variable Type
  • Input - Output
  • Conditional Statements
  • Do-While Loop
  • Linked List
  • Callback function
  • Variadic function
  • Inline Function
  • File Handling
  • Searching and Sorting

C Programming Exercises, Practice, Solution : Array

C array [107 exercises with solution].

Concept of an array

[ An editor is available at the bottom of the page to write and execute the scripts.   Go to the editor ]

1. Write a program in C to store elements in an array and print them. Test Data : Input 10 elements in the array : element - 0 : 1 element - 1 : 1 element - 2 : 2 ....... Expected Output : Elements in array are: 1 1 2 3 4 5 6 7 8 9 Click me to see the solution

2. Write a program in C to read n number of values in an array and display them in reverse order. Test Data : Input the number of elements to store in the array :3 Input 3 number of elements in the array : element - 0 : 2 element - 1 : 5 element - 2 : 7 Expected Output : The values store into the array are : 2 5 7 The values store into the array in reverse are : 7 5 2 Click me to see the solution

3. Write a program in C to find the sum of all elements of the array. Test Data : Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 2 element - 1 : 5 element - 2 : 8 Expected Output : Sum of all elements stored in the array is : 15 Click me to see the solution

4. Write a program in C to copy the elements of one array into another array. Test Data : Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 15 element - 1 : 10 element - 2 : 12 Expected Output : The elements stored in the first array are : 15 10 12 The elements copied into the second array are : 15 10 12 Click me to see the solution

5. Write a program in C to count the total number of duplicate elements in an array. Test Data : Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 5 element - 1 : 1 element - 2 : 1 Expected Output : Total number of duplicate elements found in the array is : 1 Click me to see the solution

6. Write a program in C to print all unique elements in an array. Test Data : Print all unique elements of an array: ------------------------------------------ Input the number of elements to be stored in the array: 4 Input 4 elements in the array : element - 0 : 3 element - 1 : 2 element - 2 : 2 element - 3 : 5 Expected Output : The unique elements found in the array are: 3 5 Click me to see the solution

7. Write a program in C to merge two arrays of the same size sorted in descending order. Test Data : Input the number of elements to be stored in the first array :3 Input 3 elements in the array : element - 0 : 1 element - 1 : 2 element - 2 : 3 Input the number of elements to be stored in the second array :3 Input 3 elements in the array : element - 0 : 1 element - 1 : 2 element - 2 : 3 Expected Output : The merged array in decending order is : 3 3 2 2 1 1 Click me to see the solution

8. Write a program in C to count the frequency of each element of an array. Test Data : Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 25 element - 1 : 12 element - 2 : 43 Expected Output : The frequency of all elements of an array : 25 occurs 1 times 12 occurs 1 times 43 occurs 1 times Click me to see the solution

9. Write a program in C to find the maximum and minimum elements in an array. Test Data : Input the number of elements to be stored in the array :3 Input 3 elements in the array : element - 0 : 45 element - 1 : 25 element - 2 : 21 Expected Output : Maximum element is : 45 Minimum element is : 21 Click me to see the solution

10. Write a program in C to separate odd and even integers into separate arrays. Test Data : Input the number of elements to be stored in the array :5 Input 5 elements in the array : element - 0 : 25 element - 1 : 47 element - 2 : 42 element - 3 : 56 element - 4 : 32 Expected Output : The Even elements are : 42 56 32 The Odd elements are : 25 47 Click me to see the solution

11. Write a program in C to sort elements of an array in ascending order. Test Data : Input the size of array : 5 Input 5 elements in the array : element - 0 : 2 element - 1 : 7 element - 2 : 4 element - 3 : 5 element - 4 : 9 Expected Output : Elements of array in sorted ascending order: 2 4 5 7 9 Click me to see the solution

12. Write a program in C to sort the elements of the array in descending order. Test Data : Input the size of array : 3 Input 3 elements in the array : element - 0 : 5 element - 1 : 9 element - 2 : 1 Expected Output : Elements of the array in sorted descending order: 9 5 1 Click me to see the solution

13. Write a program in C to insert the values in the array (sorted list). Test Data : Input number of elements you want to insert (max 100): 5 Input 5 elements in the array in ascending order: element - 0 : 2 element - 1 : 3 element - 2 : 4 element - 3 : 7 element - 4 : 8 Input the value to be inserted : 5 The existing array list is : 2 3 4 7 8 After Insert the list is : 2 3 4 5 7 8 Click me to see the solution

14. Write a program in C to insert values in the array (unsorted list). Test Data : Input the size of array : 4 Input 4 elements in the array in ascending order: element - 0 : 1 element - 1 : 8 element - 2 : 7 element - 3 : 10 Input the value to be inserted : 5 Input the Position, where the value to be inserted :2 Expected Output : The current list of the array : 1 8 7 10 After Insert the element the new list is : 1 5 8 7 10 Click me to see the solution

15. Write a program in C to delete an element at a desired position from an array. Test Data : Input the size of array : 5 Input 5 elements in the array in ascending order: element - 0 : 1 element - 1 : 2 element - 2 : 3 element - 3 : 4 element - 4 : 5 Input the position where to delete: 3 Expected Output : The new list is : 1 2 4 5 Click me to see the solution

16. Write a program in C to find the second largest element in an array. Test Data : Input the size of array : 5 Input 5 elements in the array : element - 0 : 2 element - 1 : 9 element - 2 : 1 element - 3 : 4 element - 4 : 6 Expected Output : The Second largest element in the array is : 6 Click me to see the solution

17. Write a program in C to find the second smallest element in an array. Test Data : Input the size of array : 5 Input 5 elements in the array (value must be <9999) : element - 0 : 0 element - 1 : 9 element - 2 : 4 element - 3 : 6 element - 4 : 5 Expected Output : The Second smallest element in the array is : 4 Click me to see the solution

18. Write a program in C for a 2D array of size 3x3 and print the matrix. Test Data : Input elements in the matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [0],[2] : 3 element - [1],[0] : 4 element - [1],[1] : 5 element - [1],[2] : 6 element - [2],[0] : 7 element - [2],[1] : 8 element - [2],[2] : 9 Expected Output : The matrix is : 1 2 3 4 5 6 7 8 9 Click me to see the solution

19. Write a program in C for adding two matrices of the same size. Test Data : Input the size of the square matrix (less than 5): 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Input elements in the second matrix : element - [0],[0] : 5 element - [0],[1] : 6 element - [1],[0] : 7 element - [1],[1] : 8 Expected Output : The First matrix is : 1 2 3 4 The Second matrix is : 5 6 7 8 The Addition of two matrix is : 6 8 10 12 Click me to see the solution

20. Write a program in C for the subtraction of two matrices. Test Data : Input the size of the square matrix (less than 5): 2 Input elements in the first matrix : element - [0],[0] : 5 element - [0],[1] : 6 element - [1],[0] : 7 element - [1],[1] : 8 Input elements in the second matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Expected Output : The First matrix is : 5 6 7 8 The Second matrix is : 1 2 3 4 The Subtraction of two matrix is : 4 4 4 4 Click me to see the solution

21. Write a program in C for the multiplication of two square matrices. Test Data : Input the rows and columns of first matrix : 2 2 Input the rows and columns of second matrix : 2 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Input elements in the second matrix : element - [0],[0] : 5 element - [0],[1] : 6 element - [1],[0] : 7 element - [1],[1] : 8 Expected Output : The First matrix is : 1 2 3 4 The Second matrix is : 5 6 7 8 The multiplication of two matrix is : 19 22 43 50 Click me to see the solution

22. Write a program in C to find the transpose of a given matrix. Test Data : Input the rows and columns of the matrix : 2 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Expected Output : The matrix is : 1 2 3 4 The transpose of a matrix is : 1 3 2 4 Click me to see the solution

23. Write a program in C to find the sum of the right diagonals of a matrix. Test Data : Input the size of the square matrix : 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Expected Output : The matrix is : 1 2 3 4 Addition of the right Diagonal elements is :5 Elements in array are: Click me to see the solution

24. Write a program in C to find the sum of the left diagonals of a matrix. Test Data : Input the size of the square matrix : 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Expected Output : The matrix is : 1 2 3 4 Addition of the left Diagonal elements is :5 Click me to see the solution

25. Write a program in C to find the sum of rows and columns of a matrix. Test Data : Input the size of the square matrix : 2 Input elements in the first matrix : element - [0],[0] : 5 element - [0],[1] : 6 element - [1],[0] : 7 element - [1],[1] : 8 Expected Output : The First matrix is : The matrix is : 5 6 7 8 The sum or rows and columns of the matrix is : 5 6 11 7 8 15 12 14 Click me to see the solution

26. Write a program in C to print or display the lower triangular of a given matrix. Test Data : Input the size of the square matrix : 3 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [0],[2] : 3 element - [1],[0] : 4 element - [1],[1] : 5 element - [1],[2] : 6 element - [2],[0] : 7 element - [2],[1] : 8 element - [2],[2] : 9 Expected Output : The matrix is : 1 2 3 4 5 6 7 8 9 Setting zero in lower triangular matrix 1 2 3 0 5 6 0 0 9 Click me to see the solution

27. Write a program in C to print or display an upper triangular matrix. Test Data : Input the size of the square matrix : 3 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [0],[2] : 3 element - [1],[0] : 4 element - [1],[1] : 5 element - [1],[2] : 6 element - [2],[0] : 7 element - [2],[1] : 8 element - [2],[2] : 9 Expected Output : The matrix is : 1 2 3 4 5 6 7 8 9 Setting zero in upper triangular matrix 1 0 0 4 5 0 7 8 9 Click me to see the solution

28. Write a program in C to calculate the determinant of a 3 x 3 matrix. Test Data : Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 0 element - [0],[2] : -1 element - [1],[0] : 0 element - [1],[1] : 0 element - [1],[2] : 1 element - [2],[0] : -1 element - [2],[1] : -1 element - [2],[2] : 0 Expected Output : The matrix is : 1 0 -1 0 0 1 -1 -1 0 The Determinant of the matrix is: 1 Click me to see the solution

29. Write a program in C to accept a matrix and determine whether it is a sparse matrix. Test Data : Input the number of rows of the matrix : 2 Input the number of columns of the matrix : 2 Input elements in the first matrix : element - [0],[0] : 0 element - [0],[1] : 0 element - [1],[0] : 1 element - [1],[1] : 0 Expected Output : The given matrix is sparse matrix. There are 3 number of zeros in the matrix Click me to see the solution

30. Write a program in C to accept two matrices and check whether they are equal. Test Data : Input Rows and Columns of the 1st matrix :2 2 Input Rows and Columns of the 2nd matrix :2 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Input elements in the second matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Expected Output : The first matrix is : 1 2 3 4 The second matrix is : 1 2 3 4 The Matrices can be compared : Two matrices are equal. Click me to see the solution

31. Write a program in C to check whether a given matrix is an identity matrix. Test Data : Input number of Rows for the matrix :3 Input number of Columns for the matrix :3 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 0 element - [0],[2] : 0 element - [1],[0] : 0 element - [1],[1] : 1 element - [1],[2] : 0 element - [2],[0] : 0 element - [2],[1] : 0 element - [2],[2] : 1 Expected Output : The matrix is : 1 0 0 0 1 0 0 0 1 The matrix is an identity matrix. Click me to see the solution

32. Write a program in C to find a pair with given sum in the array. Expected Output : The given array : 6 8 4 -5 7 9 The given sum : 15 Pair of elements can make the given sum by the value of index 0 and 5 Click me to see the solution

33. Write a program in C to find the majority element of an array. A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element). Expected Output : The given array is : 4 8 4 6 7 4 4 8 There are no Majority Elements in the given array. Click me to see the solution

34. Write a program in C to find the number occurring odd number of times in an array. All numbers occur even number of times except one number which occurs odd number of times. Expected Output : The given array is : 8 3 8 5 4 3 4 3 5 The element odd number of times is : 3 Click me to see the solution

35. Write a program in C to find the largest sum of contiguous subarrays in an array. Expected Output : The given array is : 8 3 8 -5 4 3 -4 3 5 The largest sum of contiguous subarray is : 21 Click me to see the solution

36. Write a program in C to find the missing number in a given array. There are no duplicates in the list. Expected Output : The given array is : 1 3 4 2 5 6 9 8 The missing number is : 7 Click me to see the solution

37. Write a program in C to find the pivot element of a sorted and rotated array using binary search. Pivot element is the only element in input array which is smaller than it's previous element. A pivot element divided a sorted rotated array into two monotonically increasing array. Expected Output : The given array is : 14 23 7 9 3 6 18 22 16 36 The Pivot Element in the array is : 3 Click me to see the solution

38. Write a program in C to merge one sorted array into another sorted array. Pivot element is the only element in input array which is smaller than it's previous element. A pivot element divided a sorted rotated array into two monotonically increasing array. Expected Output : The given Large Array is : 10 12 14 16 18 20 22 The given Small Array is : 11 13 15 17 19 21 After merged the new Array is : 10 11 12 13 14 15 16 17 18 19 20 21 22 Click me to see the solution

39. Write a program in C to rotate an array by N positions. Expected Output : The given array is : 0 3 6 9 12 14 18 20 22 25 27 From 4th position the values of the array are : 12 14 18 20 22 25 27 Before 4th position the values of the array are : 0 3 6 9 After rotating from 4th position the array is: 12 14 18 20 22 25 27 0 3 6 9 Click me to see the solution

40. Write a program in C to find the ceiling in a sorted array. N.B.: Given a sorted array in ascending order and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x. Expected Output : The given array is : 1 3 4 7 8 9 9 10 The ceiling of 5 is: 7 Click me to see the solution

41. Write a program in C to find the Floor and Ceiling of the number 0 to 10 from a sroted array. Expected Output : The given array is : 1 3 5 7 8 9 Number: 0 ceiling is: 1 floor is: -1 Number: 1 ceiling is: 1 floor is: 1 Number: 2 ceiling is: 3 floor is: 1 Number: 3 ceiling is: 3 floor is: 3 Number: 4 ceiling is: 5 floor is: 3 Number: 5 ceiling is: 5 floor is: 5 Number: 6 ceiling is: 7 floor is: 5 Number: 7 ceiling is: 7 floor is: 7 Number: 8 ceiling is: 8 floor is: 8 Number: 9 ceiling is: 9 floor is: 9 Number: 10 ceiling is: -1 floor is: 9 Click me to see the solution

42. Write a program in C to find the smallest missing element in a sorted array. Expected Output : The given array is : 0 1 3 4 5 6 7 9 The missing smallest element is: 2 Click me to see the solution

43. Write a program in C to print the next greatest elements in a given unsorted array. Elements for which no superior element exists, consider the next greatest element as -1. Expected Output : The given array is : 5 3 10 9 6 13 Next Bigger Elements are: Next bigger element of 5 in the array is: 10 Next bigger element of 3 in the array is: 10 Next bigger element of 10 in the array is: 13 Next bigger element of 9 in the array is: 13 Next bigger element of 6 in the array is: 13 Next bigger element of 13 in the array is: -1 Next Bigger Elements Array: 10 10 13 13 13 -1 Click me to see the solution

44. Write a program in C to find the two repeating elements in a given array. Expected Output : The given array is : 2 7 4 7 8 3 4 The repeating elements are: 7 4 Click me to see the solution

45. Write a program in C to find two elements whose sum is closest to zero. Expected Output : The given array is : 38 44 63 -51 -35 19 84 -69 4 -46 The Pair of elements whose sum is minimum are: [44, -46] Click me to see the solution

46. Write a program in C to find the smallest positive number missing from an unsorted array. Expected Output : The given array is : 3 1 4 10 -5 15 2 -10 -20 The smallest positive number missed is: 5 Click me to see the solution

47. Write a program in C to find a subarray with a given sum from the given array. Expected Output : The given array is : 3 4 -7 1 3 3 1 -4 [0..1] -- { 3 4 } [0..5] -- { 3 4 -7 1 3 3 } [3..5] -- { 1 3 3 } [4..6] -- { 3 3 1 } Click me to see the solution

48. Write a program in C to find out if a given integer x appears more than n/2 times in a sorted array of n integers. Expected Output : The given array is : 1 3 3 5 4 3 2 3 3 The given value is : 3 3 appears more than 4 times in the given array[] Click me to see the solution

49. Write a program in C to find the majority element of an array. Expected Output : The given array is : 1 3 3 7 4 3 2 3 3 The majority of the Element : 3 Click me to see the solution

50. Write a program in C to print a matrix in spiral form. Expected Output : The given array in matrix form is : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 The spiral form of above matrix is: 1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12 Click me to see the solution

51. Write a program in C to find the maximum circular subarray sum of a given array. Expected Output : The given array is : 10 8 -20 5 -3 -5 10 -13 11 The maximum circular sum in the above array is: 29 Click me to see the solution

52. Write a program in C to count the number of triangles that can be formed from a given array. Expected Output : The given array is : 6 18 9 7 10 Number of possible triangles can be formed from the array is: 5 Click me to see the solution

53. Write a program in C to find the number of times a given number appears in an array. Expected Output : The given array is : 2 3 4 4 4 4 5 5 5 6 7 7 The number of times the number 4 occurs in the given array is: 4 Click me to see the solution

54. Write a program in C to sort an array of 0s, 1s and 2s. Expected Output : The given array is : 0 1 2 2 1 0 0 2 0 1 1 0 After sortig the elements in the array are: 0 0 0 0 0 1 1 1 1 2 2 2 Click me to see the solution

55. Write a program in C to check whether an array is a subset of another array. Expected Output : The given first array is : 4 8 7 11 6 9 5 0 2 The given second array is : 5 4 2 0 6 The second array is the subset of first array. Click me to see the solution

56. Write a program in C to return the minimum number of jumps to reach the end of the array. Expected Output : The given array is : 1 3 5 8 9 2 6 7 6 8 9 1 1 1 The minimum of number of jumps is required to reach the end is: 3 Click me to see the solution

57. Write a program in C to find the minimum element in a sorted and rotated array. Expected Output : The given array is : 3 4 5 6 7 9 2 The minimum element in the above array is: 2 Click me to see the solution

58. Write a program in C to move all zeroes to the end of a given array. Expected Output : The given array is : 2 5 7 0 4 0 7 -5 8 0 The new array is: 2 5 7 8 4 -5 7 0 0 0 Click me to see the solution

59. Write a program in C to return the counting sort of an array. Expected Output : The given array is : 4 14 8 0 2 5 2 1 0 17 9 0 5 After sorting the elements in the array are: 0 0 0 1 2 2 4 5 5 8 9 14 17 Click me to see the solution

60. Write a program in C to find the row with the maximum number of 1s. Expected Output : The given 2D array is : 0 1 0 1 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 The index of row with maximum 1s is: 1 Click me to see the solution

61. Write a program in C to find the maximum product subarray in a given array. Expected Output : The given array is : -4 9 -7 0 -15 6 2 -3 The maximum product of a sub-array in the given array is: 540 Click me to see the solution

62. Write a program in C to find the largest subarray with an equal number of 0s and 1s. Expected Output : The given array is : 0 1 0 0 1 1 0 1 1 1 Subarray found from the index 0 to 7 Click me to see the solution

63. Write a program in C to replace every element with the greatest element on its right side. Expected Output : The given array is : 7 5 8 9 6 8 5 7 4 6 After replace the modified array is: 9 9 9 8 8 7 7 6 6 0 Click me to see the solution

64. Write a program in C to find the median of two sorted arrays of the same size. Expected Output : The given array - 1 is : 1 5 13 24 35 The given array - 2 is : 3 8 15 17 32 The Median of the 2 sorted arrays is: 14 Click me to see the solution

65. Write a program in C to find the product of an array such that product is equal to the product of all the elements of arr[] except arr[i]. Expected Output : The given array is : 1 2 3 4 5 6 The product array is: 720 360 240 180 144 120 Click me to see the solution

66. Write a program in C to count the number of inversions in a given array. Expected Output : The given array is : 1 9 6 4 5 The inversions are: (9, 6) (9, 4) (9, 5) (6, 4) (6, 5) The number of inversion can be formed from the array is: 5 Click me to see the solution

67. Write a program in C to search for an element in a row wise and column wise sorted matrix. Expected Output : The given array in matrix form is : 15 23 31 39 18 26 36 43 25 28 37 48 30 34 39 50 The given value for searching is: 37 The element Found at the position in the matrix is: 2, 2 Click me to see the solution

68. Write a program in C to return the maximum sum such that no two elements are adjacent. Expected Output : The given array is : 1 3 5 9 7 10 1 10 100 The maximum sum from the array such that no two elements are adjacent is: 122 Click me to see the solution

69. Write a program in C to find the maximum difference between any two elements such that the larger element appears after the smaller number. Expected Output : The given array is : 7 9 5 6 13 2 The elements which provide maximum difference is: 5, 13 The Maximum difference between two elements in the array is: 8 Click me to see the solution

70. Write a program in C to find two numbers that occur an odd number of times in an array. Expected Output : The given array is: 6 7 3 6 8 7 6 8 3 3 The two numbers occuring odd number of times are: 3 & 6 Click me to see the solution

71. Write a program in C to find the median of two sorted arrays of different sizes. Expected Output : The given first array is : 90 240 300 The given second array is : 10 13 14 20 25 The median of two different size arrays are : 22.500000 Click me to see the solution

72. Write a program in C to return only the unique rows from a given binary matrix. Expected Output : The given array is : 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1 1 0 1 0 0 The unique rows of the given array are : 0 1 0 0 1 1 0 1 1 0 1 0 1 0 0 Click me to see the solution

73. Write a program in C to print all unique elements of an unsorted array. Expected Output : The given array is : 1 5 8 5 7 3 2 4 1 6 2 Unique Elements in the given array are: 1 5 8 7 3 2 4 6 Click me to see the solution

74. Write a program in C to find the sum of the upper triangular elements of a matrix. Expected Output : The given array is : 1 2 3 4 5 6 7 8 9 The elements being summed of the upper triangular matrix are: 2 3 6 The Sum of the upper triangular Matrix Elements are: 11 Click me to see the solution

75. Write a program in C to find the sum of the lower triangular elements of a matrix. Expected Output : The given array is : 1 2 3 4 5 6 7 8 9 The elements being summed of the lower triangular matrix are: 4 7 8 The Sum of the lower triangular Matrix Elements are: 19 Click me to see the solution

76. Write a program in C to find the largest number possible from the set of given numbers. Expected Output : The given numbers are : 15 628 971 9 2143 12 The largest possible number by the given numbers are: 997162821431512 Click me to see the solution

77. Write a program in C to generate random permutations of array elements. Expected Output : The given array is: 1 2 3 4 5 6 7 8 The shuffled elements in the array are: 2 8 7 3 4 5 1 6 Click me to see the solution

78. Write a program in C to find four array elements whose sum is equal to a given number. Expected Output : The given array is: 3 7 1 9 15 14 6 2 5 7 The elements are: 3, 15, 14, 5 Click me to see the solution

79. Write a program in C to sort n numbers in the range from 0 to n^2. Expected Output : The given array is: 37 62 52 7 48 3 15 61 Sorted array is: 3 7 15 37 48 52 61 62 Click me to see the solution

80. Write a program in C to count all distinct pairs for a specific difference. Expected Output : The given array is: 5 2 3 7 6 4 9 8 The distinct pairs for difference 5 are: [7, 2] [8, 3] [9, 4] Number of distinct pairs for difference 5 are: 3 Click me to see the solution

81. Write a program in C to find the maximum repeating number in a given array. The array range is [0..n-1] and the elements are in the range [0..k-1] and k Expected Output : The given array is: 2 3 3 5 3 4 1 7 7 7 7 The maximum repeating number is: 7 Click me to see the solution

82. Write a program in C to print all possible combinations of r elements in a given array. Expected Output : The given array is: 1 5 4 6 8 The combination from by the number of elements are: 4 The combinations are: 1 5 4 6 1 5 4 8 1 5 6 8 1 4 6 8 5 4 6 8 Click me to see the solution

83. Write a program in C to find a pair with the given difference. Expected Output : The given array is: 1 15 39 75 92 The given difference is: 53 The pair are: (39, 92) Click me to see the solution

84. Write a program in C to find the minimum distance between two numbers in a given array. Expected Output : The given array is: 7 9 5 11 7 4 12 6 2 11 The minimum distance between 7 and 11 is: 1 Click me to see the solution

85. Write a program in C to count all possible paths from top left to bottom right of a m X n matrix. Expected Output : The size of matrix is : 4 x 4 The all possible paths from top left to bottom right is: 20 Click me to see the solution

86. Write a program in C to find the equilibrium index of an array. Expected Output : The given array is: 0 -4 7 -4 -2 6 -3 0 The equilibrium index found at : 7 5 0 Click me to see the solution

87. Write a program in C to find the maximum element in an array that is first increasing and then decreasing. Expected Output : The given array is: 2 7 12 25 4 57 27 44 The maximum element which is increasing then decreasing is: 57 Click me to see the solution

88. Write a program in C to find the maximum difference n – m such that array[n] > array[m] from a given array[]. Given an array arr[], find the maximum difference n – m such that arr[n] > arr[m] Expected Output : The given array is: 7 5 8 2 3 2 4 2 1 0 m = 0, n = 2, arr1[m] = 7 arr1[n] = 8 difference = 2 m = 3, n = 6, arr1[m] = 2 arr1[n] = 4 difference = 3 The maximum differcences between two position of array index is: 3 Click me to see the solution

89. Write a program in C to find the largest square sub-matrix with all 1s. Expected Output : The given array in matrix form is : 0 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 0 The maximum size sub-matrix is: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Click me to see the solution

90. Given an array of size n such that every element is in the range from 0 to n-1. Write a program in C to rearrange the given array so that arr[i] becomes arr[arr[i]]. Expected Output : The Original array is 2 1 4 3 0 The modified array is: 4 1 0 3 2 Click me to see the solution

91. An unsorted array of a specific size is given. Write a program in C to find the minimum length of a subarray such that sorting this subarray makes the whole array sorted. Expected Output : The given array is: 10 12 15 17 28 32 42 18 56 59 67 The minimum length of unsorted subarray which makes the given array sorted lies between the indeces 4 and 7 Click me to see the solution

92. Write a program in C that checks whether the elements in an unsorted array appear consecutively or not. Expected Output : The given array is: 7 4 3 5 6 2 The appearence of elements in the array are consecutive. The given array is: 7 4 4 5 6 2 The appearence of elements in the array are not consecutive. The given array is: 7 4 9 5 6 3 The appearence of elements in the array are not consecutive. Click me to see the solution

93. Write a program in C to rearrange positive and negative numbers alternatively in a given array. N.B.: If positive numbers are more they appear at the end and for also negative numbers, they too appear in the end of the array. Expected Output : The given array is: -4 8 -5 -6 5 -9 7 1 -21 -11 19 The rearranged array is: -4 7 -5 1 -21 5 -11 8 -9 19 -6 Click me to see the solution

94. Write a program in C to find the largest element of each and every contiguous subarray of size k from a given array. Expected Output : The given array is: 1 3 6 21 4 9 12 3 16 10 The length of each subarray is: 4 The contigious subarray of length 4 and their maximum value are: 1 3 6 21 ----> 21 3 6 21 4 ----> 21 6 21 4 9 ----> 21 21 4 9 12 ----> 21 4 9 12 3 ----> 12 9 12 3 16 ----> 16 12 3 16 10 ----> 16 Click me to see the solution

95. Write a program in C to segregate 0s and 1s in an array. Expected Output : The given array is: 1 0 1 0 0 1 0 1 1 The array after segregation is: 0 0 0 0 1 1 1 1 1 Click me to see the solution

96. Write a program in C to segregate even and odd elements in an array. Expected Output : The given array is: 17 42 19 7 27 24 30 54 73 The array after segregation is: 54 42 30 24 27 7 19 17 73 Click me to see the solution

97. Write a program in C to find the index of the first peak element in a given array. Expected Output : The given array is: 5 12 13 20 16 19 11 7 25 The index of first peak element in the array is: 3 Click me to see the solution

98. Write a program in C to return the largest span found in the leftmost and rightmost appearances of the same value (values are inclusive) in a given array. Expected Output : The given array is: 17 42 19 7 27 24 17 54 73 The span between the same values in the array is: 7 Click me to see the solution

99. Write a program in C to return true if an array can be split in such a way that the left side of the splitting is equal to the sum of the right side. Expected Output : The given array is : 1 3 3 8 4 3 2 3 3 The array can be split in a position where the sum of both side are equal. Click me to see the solution

100. Write a program in C to return the number of clumps (a series of 2 or more adjacent elements of the same value) in a given array. Expected Output : The given array is: 17 42 42 7 24 24 17 54 17 The number of clumps in the array is: 2 Click me to see the solution

101. Write a program in C to rearrange an array such that arr[i]=i.

N.B.: Given array contains N elements, from 0 to N – 1. All elements within the range may not be present in the array. There will be -1 if an element within the range is not present in the array.

Expected Output : The given array is: 2 5 -1 6 -1 8 7 -1 9 1 The new array is: -1 1 2 -1 -1 5 6 7 8 9 Click me to see the solution

102. Write a program in C to rearrange an array in such an order that– small, large, second smallest, second largest, etc. Expected Output : The given array is: 5 8 1 4 2 9 3 7 6 The new array is: 1 9 2 8 3 7 4 6 5 Click me to see the solution

103. Write a program in C to update every array element with multiplication of previous and next numbers in array. Expected Output : The given array is: 1 2 3 4 5 6 The new array is: 2 3 8 15 24 30 Click me to see the solution

104. Write a program in C to rearrange an array such that even index elements are smaller and odd index elements are greater than their next. Expected Output : The array given is: 6 4 2 1 8 3 The new array after rearranging: 4 6 1 8 2 3 Click me to see the solution

105. Write a program in C to find the minimum number of swaps required to gather all elements less than or equal to k. Expected Output : The given array is: 2 7 9 5 8 7 4 The minimum swap required is: 2 Click me to see the solution

106. Write a C program to convert an array in such a way that it doubles its value. This will replace the next element with 0 if the current and next elements are the same. This program will rearrange the array so that all 0's are moved to the end. Expected Output : The given array is: 0 3 3 3 0 0 7 7 0 9 The new array is: 6 3 14 9 0 0 0 0 0 0 Click me to see the solution

107. Write a program in C to concatenate two given arrays of integers. Sample Data: ({ 10, 20, 30, 40, 50, 60 }, { 70, 80, 90, 100, 110, 120 }) -> "10 20 30 40 50 60 70 80 90 100 110 120" Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Follow us on Facebook and Twitter for latest update.

  • Weekly Trends and Language Statistics
  • DSA Tutorial
  • Data Structures
  • Linked List
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Divide & Conquer
  • Mathematical
  • Backtracking
  • Branch and Bound
  • Pattern Searching

Array Data Structure Guide

An array data structure is a fundamental concept in computer science that stores a collection of elements in a contiguous block of memory. It allows for efficient access to elements using indices and is widely used in programming for organizing and manipulating data.

problem solving with arrays

Array Data Structure

Table of Content

What is an Array?

Need of array data structures.

  • Types of Array Data Structures

Array Operations

  • Application of Array

An array is a collection of items of the same variable type that are stored at contiguous memory locations. It’s one of the most popular and simple data structures and is often used to implement other data structures. Each item in an array is indexed starting with 0 . Each element in an array is accessed through its index.

Arrays are a fundamental data structure in computer science. They are used in a wide variety of applications, including:

  • Storing data for processing
  • Implementing data structures such as stacks and queues
  • Representing data in tables and matrices
  • Creating dynamic data structures such as linked lists and trees

Types of Array

There are two main types of arrays:

  • One-dimensional arrays: These arrays store a single row of elements.
  • Multidimensional arrays: These arrays store multiple rows of elements.

Common operations performed on arrays include:

  • Traversal : Visiting each element of an array in a specific order (e.g., sequential, reverse).
  • Insertion : Adding a new element to an array at a specific index.
  • Deletion : Removing an element from an array at a specific index.
  • Searching : Finding the index of an element in an array.

Applications of Array

Arrays are used in a wide variety of applications, including:

Learn Basics of Array:

  • What is Array
  • Introduction to Arrays – Data Structure and Algorithm Tutorials
  • Applications, Advantages and Disadvantages of Array

Array in Different Language:

  • Arrays in C/C++
  • Arrays in Java
  • Arrays in Python
  • Arrays in C#
  • Arrays in Javascript

Basic Operations on Array:

  • Searching in Array
  • Write a program to reverse an array
  • Complete Guide On Array Rotations
  • Search, insert and delete in an unsorted array
  • Search, insert and delete in a sorted array
  • Sort an Array
  • Generate all subarrays

Easy Problems on Array:

  • Find the largest three elements in an array
  • Find Second largest element in an array
  • Move all zeroes to end of array
  • Rearrange array such that even positioned are greater than odd
  • Rearrange an array in maximum minimum form using Two Pointer Technique
  • Segregate even and odd numbers
  • Reversal algorithm for array rotation
  • Print left rotation of array in O(n) time and O(1) space
  • Sort an array in wave form
  • Sort an array which contain 1 to n values
  • Count the number of possible triangles
  • Print All Distinct Elements of a given integer array
  • Find the element that appears once in Array where every other element appears twice
  • Leaders in an array
  • Find sub-array with given sum

Medium Problems on Array:

  • Rearrange an array such that arr[i] = i
  • Rearrange positive and negative numbers in O(n) time and O(1) extra space
  • Reorder an array according to given indexes
  • Search an element in a sorted and rotated array
  • Find the Rotation Count in Rotated Sorted array
  • K-th Largest Sum Contiguous Subarray
  • Find the smallest missing number
  • Difference Array | Range update query in O(1)
  • Maximum profit by buying and selling a share at most twice
  • Smallest subarray with sum greater than a given value
  • Inversion count in Array using Merge Sort
  • Sort an array of 0s, 1s and 2s
  • Merge two sorted arrays with O(1) extra space
  • Majority Element
  • Two Pointers Technique
  • Find a peak element
  • Find a triplet that sum to a given value
  • Minimum increment by k operations to make all elements equal
  • Equilibrium index of an array

Hard Problems on Array:

  • Find k numbers with most occurrences in the given array
  • MO’s Algorithm
  • Square Root (Sqrt) Decomposition Algorithm
  • Sparse Table
  • Range sum query using Sparse Table
  • Range Minimum Query (Square Root Decomposition and Sparse Table)
  • Range LCM Queries
  • Merge Sort Tree for Range Order Statistics
  • Minimum number of jumps to reach end
  • Space optimization using bit manipulations
  • Sort a nearly sorted (or K sorted) array
  • Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
  • Median in a stream of integers (running integers)
  • Construct an array from its pair-sum array
  • Maximum equlibrium sum in an array
  • Smallest Difference Triplet from Three arrays
  • Find all triplets with zero sum

Quick Links :

  • ‘Practice Problems’ on Arrays
  • ‘Quizzes’ on Arrays
  • ‘Video Tutorials’ on Arrays

Recommended:

  • Learn Data Structure and Algorithms | DSA Tutorial
  • Introduction to Matrix or Grid – Data Structure and Algorithms Tutorial

Please Login to comment...

Similar reads, improve your coding skills with practice.

 alt=

IMAGES

  1. Maths Investigation Problem solving with arrays

    problem solving with arrays

  2. Make Arrays: Reasoning and Problem Solving

    problem solving with arrays

  3. Arrays

    problem solving with arrays

  4. Maths Investigation Problem solving with arrays

    problem solving with arrays

  5. Use arrays: Reasoning and Problem Solving

    problem solving with arrays

  6. Maths Investigation Problem solving with arrays

    problem solving with arrays

COMMENTS

  1. Top 50 Array Coding Problems for Interviews

    Find the first repeating element in an array of integers. Solve. Find the first non-repeating element in a given array of integers. Solve. Subarrays with equal 1s and 0s. Solve. Rearrange the array in alternating positive and negative items. Solve. Find if there is any subarray with a sum equal to zero.

  2. Practice questions on Arrays

    Given two sorted arrays A and B, generate all possible arrays such that the first element is taken from A then from B then from A, and so on in increasing order till the arrays are exhausted. The generated arrays should end with an element from B. Example: A = {10, 15, 25} B = {1, 5, 20, 30} The resulting arrays are: 10 20 10 20 25 30 10 30 15 20 1

  3. 20+ Array Coding Problems and Questions from Programming ...

    The key to solving array-based questions is having a good knowledge of array data structure as well as basic programming constructors such as loop, recursion, and fundamental operators.

  4. Problems

    Our platform offers a range of essential problems for practice, as well as the latest questions being asked by top-tier companies. Explore; Problems; Contest; Discuss; Interview. Store Study Plan. See all. Array 1704. String ... Dynamic Programming 517. Math 509. Sorting 409. Greedy 373. Depth-First Search 291. Database 278. Binary Search 271 ...

  5. 50+ array questions with solutions (easy, medium, hard)

    Here are some moderate-level questions that are often asked in a video call or onsite interview. You should be prepared to write code or sketch out the solutions on a whiteboard if asked. 2.1 Move all zeros to the beginning/end of an array. 2.2 Find if a given element is in a sorted array (binary search)

  6. Practice Arrays

    Solve Arrays coding problems to start learning data structures and algorithms. This curated set of 23 standard Arrays questions will give you the confidence to solve interview questions. ... The problems are based on basic logic, and definitely, the learner who is a beginner will enjoy solving such problems and will improve day by day. The ...

  7. Techniques for Solving Array Problems

    Techniques for Solving Array Problems Array Challenges and Problems. In the world of programming, arrays are versatile and essential data structures, but they often come with their own set of challenges. From searching for elements to optimizing performance, solving array problems efficiently is a valuable skill for any developer. In this guide ...

  8. Array

    Subscribe to see which companies asked this question. You have solved 0 / 1715 problems.. Show problem tags #

  9. pranaydas1/Top-50-Array-Problems

    Top 50 Array Problems with Solutions A handpicked selection of array-based coding problems for interview preparation, with documented solutions in popular languages. Boost your problem-solving skills and contribute to this open-source project. Happy coding! - pranaydas1/Top-50-Array-Problems

  10. Java Array Exercise: A Step-by-Step Guide to Mastery

    Given two arrays A and B of the same size N. Check whether array A can be fit into array B. An array is said to fit into another array if by arranging the elements of both arrays, there exists a solution such that the ith element of the first array is less than or equal to ith element of the second array. Examples: Input : A[] = { 7, 5, 3, 2 }, B[]

  11. Array cheatsheet for coding interviews

    Common terms you see when doing problems involving arrays: Subarray - A range of contiguous values within an array. Example: given an array [2, 3, 6, 1, 5, 4], [3, 6, 1] is a subarray while [3, 1, 5] is not a subarray. Subsequence - A sequence that can be derived from the given sequence by deleting some or no elements without changing the order ...

  12. Solve Data Structures

    Sparse Arrays. Medium Problem Solving (Basic) Max Score: 25 Success Rate: 97.28%. Solve Challenge. Array Manipulation. Hard Problem Solving (Intermediate) Max Score: 60 Success Rate: 61.64%. Solve Challenge. Status. Solved. Unsolved. Skills. Problem Solving (Advanced) Problem Solving (Basic)

  13. Mastering LeetCode: 10 Common Array Questions and How to Solve ...

    9. Intersection of Two Arrays II. Problem: Given two arrays, write a function to compute their intersection. Solution: Use two dictionaries (hash tables) to store the frequency of elements in both ...

  14. How I Master Array Coding Problems for Interviews

    Solving these problems demands extensive practice and a deep understanding of data structures. Level 1 - Array Problems. When I first started, even solving easy problems was a challenge.

  15. Python: Array

    Extended array: array('i', [1, 3, 5, 7, 9, 1, 3, 5, 7, 9]) Click me to see the sample solution. 8. Write a Python program to convert an array to an array of machine values and return the bytes representation. Sample Output: Bytes to String: b'w3resource' Click me to see the sample solution. 9. Write a Python program to append items to a ...

  16. Java Array exercises: Array Exercises

    Java Array Exercises [79 exercises with solution] [ An editor is available at the bottom of the page to write and execute the scripts. Go to the editor] 1. Write a Java program to sort a numeric array and a string array. Click me to see the solution. 2. Write a Java program to sum values of an array. Click me to see the solution.

  17. C++ Array

    27. Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number. Click me to see the sample solution. 28. Write a C++ program to arrange the numbers in a given array in a way that the sum of some numbers equals the largest number in the array.

  18. Arrays Interview Questions

    EasyProblem Solving (Basic)Max Score: 15Success Rate: 93.16%. How to access and use 2d-arrays.

  19. Problems on arrays

    Solve from more than 2000 coding problems and interview questions on 100+ different topics. HackerEarth is a global hub of 5M+ developers. HackerEarth is a global hub of 5M+ developers. ... Problems on arrays. Difficulty # title. status. success rate. difficulty +1-650-461-4192; For sales enquiry. [email protected]; For support.

  20. C programming exercises: Array

    Input 5 elements in the array (value must be <9999) : element - 0 : 0 element - 1 : 9 element - 2 : 4 element - 3 : 6 element - 4 : 5 Expected Output: The Second smallest element in the array is : 4 Click me to see the solution. 18. Write a program in C for a 2D array of size 3x3 and print the matrix. Test Data : Input elements in the matrix :

  21. Array Data Structure Guide

    Arrays are a fundamental data structure in computer science. They are used in a wide variety of applications, including: Storing data for processing. Implementing data structures such as stacks and queues. Representing data in tables and matrices. Creating dynamic data structures such as linked lists and trees.

  22. Array

    The first line of input contains two integers n and k. The second line contains n integers — the elements of the array. The absolute values of elements do not exceed 104. The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem.

  23. Problem-Solving with Arrays

    Uses arrays to solve multiplication problems. Steps to Success: Visualises and builds an array based on its named dimensions, e.g. 3 fours; Connects the dimensions in an array to the factors in a multiplication problem; Visualises and draws an array to solve a multiplication problem MuS5