0% found this document useful (0 votes)
12 views40 pages

Array Operations and Calculations

Array in c

Uploaded by

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

Array Operations and Calculations

Array in c

Uploaded by

sridharshini1405
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Array rotation for k times(left or right)

Left:
Right:
[Link] c program to print the number of occurance of a number in an array
[Link] and Product
[Link] square of the elements in an array
[Link] between maximum and minimum element of an array
[Link] of array elements divisible by specific number
[Link] the array after removing duplicate elements
8. Print negative elements in array
[Link] the peak elements in array
[Link] the count of positive numbers
11..Delete the element in the given position in an array
[Link] of duplicates in an array

[Link] of even numbers in an array


[Link] the non prime numbers in an array
[Link] an Array print only the Positive numbers Sample Input: [1,3,-4,-5,2,3]
Sample Output: [1,3,2,3]
16..replace the peak number by adding it neighbour elements (example:- input: 1 2
4 3 output: 1 2 9 3 )
[Link] the sum of first element, second element,last element,second last element
in an array Example : [1,2,4,5,5] Output: 1+2+5+5 = 13, [1,2,3] Output : 4
[Link] the median of an array after sorting
[Link] the average of an array
[Link] Input range 2-10 if exceeds print "Invalid". print two elements which is the
sum of two element closest to 0(zero). Input: [-1,-10,8,2] output: [-1,2].
Explanation -1+2=-1 is the closest to 0 than other combinations.
[Link] the unique elements in array Example: input = [0,5,3,5,2,3] output = 0 and 2

[Link] the second largest element in the array


[Link] the count of repeated element in array
[Link] the array in ascending order and print even numbers first and odd numbers
next
[Link] even numbers from an array
[Link] first occurence of a given number from an array

[Link] Chunking - Eg1 : Chunk size = 2, array size 8, elements (1,2,3,4,5,6,7,8) -


Ans : [1,2] [3,4] [5,6] [7,8] Eg2: Chunk size = 3, array size 4,

elements (1,2,3,4) - Ans : [1,2,3]

[Link] the total number of duplicate elements in an array


You are given an array of integers. Your task is to count the total number of duplicate
elements in the array.
.TEST CASE 2:
Input: [1, 2, 2, 3, 4, 4, 4, 5, 5]
Output: 3
Explanation: The duplicate elements are 2, 4, and 5.

[Link] the frequency of each element of an array.


You are given an array of integers. Your task is to count the frequency of each element
inthe array and return a dictionary where keys are the unique elements in the array
andvalues are their frequencies.
TEST CASE 2:
Input: [1, 2, 2, 3, 4, 4, 4, 5]
Output:
1 occurs 1 times
2 occurs 2 times
3 occurs 1 times
4 occurs 3 times
5 occurs 1 times
Explanation: Each element appears only once in the array.

[Link] a pair with a given sum .


Write a program to find a pair with a given sum in the array.

\
[Link] all 0's
Given an integer array nums, move all 0's to the end of it while maintaining the
relative
order of the non-zero [Link] :that you must do this in-place without making
a
copy of the array.
TEST CASE 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
TEST CASE 2:
Input: nums = [0]
Output: [0]
TEST CASE 3:
Input: nums = [0,1]
Output: [1,0]
[Link] the Element from Array
Write a program to delete elements from the array at specified position. How to
remove
elements from an array at a given position. Logic to remove elements from any given
position in an array. The program should also print an error message if the delete
position

[Link] two array to third array


Write a program to input elements in two array and merge two array to the third array.
How to merge two array. Logic to merge two sorted array to third array.

[Link] of array except self.


Given an integer array nums, return an array answer such that answer[i] is equal to
the
product of all the elements of nums except nums[i].
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
TEST CASE 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]

[Link] Distance Between Same Elements


Given an array with duplicate elements, find the maximum distance between two
occurrences of the same element.
TEST CASE 1:
Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
Output: 9

[Link] Many Numbers Are Smaller Than the Current Number.


Given the array nums, for each nums[i] find out how many numbers in the array are
smaller than it. That is, for each nums[i] you have to count the number of valid
j's such
that j != i and nums[j] < nums[i].
Return the answer in an array.
[Link] Smaller Elements on Right Side
Given an array of integers, for each element, count the number of elements on its
right
side that are smaller than it.
[Link] the digits in an array.
Given an array of positive integers nums, return an array answer that consists of the
digits
of each integer in nums after separating them in the same order they appear in nums.
To separate the digits of an integer is to get all the digits it has in the same order.
For example, for the integer 10921, the separation of its digits is [1,0,9,2,1].

[Link] a program to read elements in a matrix and check whether the given matrix
isa symmetric matrix or not.A matrix is considered a symmetric matrix if it is equal to
its transpose, i.e., A = A^T. Inother words, a square matrix is symmetric if the
elements across the main diagonal areequal.
TEST CASE 1:
Input:
Enter the number of rows: 3
Enter the number of columns: 3
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 2
Enter element [1][1]: 4
Enter element [1][2]: 5
Enter element [2][0]: 3
Enter element [2][1]: 5
Enter element [2][2]: 6

Common questions

Powered by AI

Deleting an element at a specific position in an array is significant in managing data collections efficiently. This operation involves shifting subsequent elements leftwards to fill the gap, which can result in a time complexity of O(n) in the worst case. Such operations require careful index management to avoid errors .

To calculate the frequency of each element in an array, iterate through the array while maintaining a dictionary to store element counts. Increment the count each time an element is found. This has practical applications in counting the occurrence of features in datasets for statistical analysis or machine learning .

To merge two sorted arrays into a third sorted array, use two pointers to iterate through both arrays, adding the smaller element to the new array and advancing the respective pointer. This has a computational complexity of O(n + m), where n and m are the lengths of the two arrays. This method is efficient for merging sorted data, commonly used in mergesort algorithms .

One efficient approach is to sort the array first, which takes O(n log n), and then use a two-pointer technique to find the pair with the sum closest to zero. The idea is to initialize two pointers, one at the start and one at the end of the sorted array, and adjust them based on their sum compared to zero. This method has an overall complexity of O(n log n) due to sorting .

To compute the product of elements except for the current element without using division, use two auxiliary arrays: one traverses left to right calculating cumulative products, and the other right to left. Multiply these two for the result. The challenge is managing edge cases, especially dealing with zeros in the array .

To rearrange an array by moving all zeros to the end, use a two-pointer approach: one pointer tracks the last non-zero found, and the other iterates through the array. When a non-zero is encountered, swap it with the element at the non-zero tracker, incrementing the tracker each time. This operation is done in-place and runs in O(n) time complexity .

Array chunking can be verified by iterating over the array in increments of the specified chunk size, ensuring each chunk contains the correct number of elements. This technique is useful in data processing for batching operations, enabling parallel processing, or dividing data into smaller, more manageable subsets .

To rotate an array to the left by 'k' positions, you can reverse the first 'k' elements, reverse the remaining elements, and finally reverse the entire array. For a right rotation, adjust 'k' to be the length of the array minus 'k', and apply the same logic. Key considerations include minimizing time complexity to O(n) by avoiding extra space and only using the array swapping method described .

An efficient algorithm to find the maximum distance between two duplicates involves creating a dictionary to track the first occurrence index of each element and updating it when a longer distance is found. This method operates with O(n) time complexity, as it involves a single traversal of the array .

A matrix is symmetric if it is equal to its transpose, meaning the element at position (i, j) is equal to the element at position (j, i). To determine this, compare each element across the matrix's main diagonal. The matrix must be square for symmetry to be applicable .

You might also like