Question: Write a program to rotate an array arr[], having a size ‘n’ by ‘d’ elements? Input: arr[] = { 4, 8, 15, 16, 23, 42, 99 }, d = 3 Output: 16, 23, 42, 99, 4, 8, 15 Let us suppose our array is in this way: On rotating the array by 3 elements, it should look like: We can do this by rotating the array one element at a time. We discussed it in this post. We can also use the REVERSAL Algorithm to rotate the array. This…
nikoo28
nikoo28
a tech-savvy guy and a design buff... I was born with the love for exploring and want to do my best to give back to the community. I also love taking photos with my phone to capture moments in my life. It's my pleasure to have you here.
-
-
Question: Write a program to rotate an array arr[], having a size ‘n’ by ‘d’ elements? Input: arr[] = { 4, 8, 15, 16, 23, 42, 99 }, d = 3 Output: 16, 23, 42, 99, 4, 8, 15 Let us suppose our array is in this way: On rotating the array by 3 elements, it should look like: To solve the above problem, we can adopt this following technique- Create a function that rotates the entire array by one element at a time. To rotate by one, store arr[0]…
-
Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 We solved this problem using array summation here. We can also utilize the property of XOR to solve this problem. The major property of XOR that we will be using is A XOR A = 0. Thus, the steps involved…
-
Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 We solved the same problem via sorting here. Another solution to the problem is possible that has a even smaller time complexity and requires just a single scan of the array. The technique involved is:- Using the summation formula, get…
-
Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 After the brute force method we can also use another method to find the missing number in an array. This technique involves sorting the array. Sort the array and give it a linear scan, if there is a difference between…
-
Question:- We are given a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers is missing. Give an algorithm to find the missing integer. Input:- 1, 2, 4, 6, 3, 7, 8 Output:- 5 Brute Force Solution:- One simple solution to this is, for each number in 1 to n check whether that number is in the given array or not. Return the number that is not present. Here is the code module for…
-
Arrays
Find the element which appears maximum number of times in an array? (Method 3)
by nikoo281 minutes readQuestion: Given an array of n numbers. Give an algorithm for finding the element which appears maximum number of times in the array? Input: 3, 2, 1, 2, 2, 3 Output: 2 The previous method required us to sort the array. But what if we do not want to sort the array. We can also solve this problem using 2 scans of the array. We cannot use the negation method used to check duplicates because of the number of repetitions. In the first scan, instead of negating -> add the…
-
Arrays
Find the element which appears maximum number of times in an array? (Method 2)
by nikoo281 minutes readQuestion: Given an array of n numbers. Give an algorithm for finding the element which appears maximum number of times in the array? Input: 3, 2, 1, 2, 2, 3 Output: 2 This problem can be done by brute force method. Now let us try to optimize this problem further. We need to reduce the time complexity. We used 2 for loops in the brute force approach. This is not necessary. Another approach can be:- Sort the entire array. Start scanning the array from the beginning and keep counting the…
-
Arrays
Find the element which appears maximum number of times in an array?
by nikoo281 minutes readQuestion: Given an array of n numbers. Give an algorithm for finding the element which appears maximum number of times in the array? Input: 3, 2, 1, 2, 2, 3 Output: 2 One simple and a brute force solution to this is, for each input element check whether there is any element with same value and for each occurrence, increment the counter. Each time, check the current counter with the max counter and update it if this value is greater than max counter. This we can solve just by using…
-
Arrays
Write a program to check if there are any duplicated elements in an array? (Method 3)
by nikoo281 minutes readQuestion: Given an array of n numbers, give an algorithm for checking whether there are any duplicated elements in the array or not? Input: 4, 8, 15, 16, 23, 42 Output: NO Input: 4, 8, 4, 15, 23, 42 Output: YES Another possible and better way to solve this problem based on a certain condition. Let us assume that the array elements are positive numbers and also all the elements are in the range 0 to n-1. For each element A[i], go to the array element whose index is A[i].…