A swap means removing any element from the array and appending it to the back of the same array. Given an array of integers find the minimum number of swaps needed to sort the array.
This one though looks like a complicated problem invoiving many cases , can actually be broken down into just 3 cases
1. All numbers are negative
2. At least one number is 0 but there is no number >0
3. There is at least one number >0
In first case, the best possible sum is the largest number. The number of possibilities is the number of times that number appears in the arrray.
In second case, the best possible sum is 0. The number of possibilities is 2^K – 1 where K is the number of zeros in the array.
In the third case, the best possible sum is the sum of all positive numbers in the array. Number of possibilities is 2^K where K is the number of zeros in the array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream>
using namespace std; #define mysizeof(data) (char*)(&data+1)-(char*)(&data)
int main(){
char arr[]={1,2,3};
int arr1[]={1,2,3};
cout<<mysizeof(arr);
cout<<endl<<mysizeof(arr1);
}
output:
3
12
suppose memory location for arr1[] starts with 2000, then arr1[]+1 will point to 2012 and not 2004.
This is because arr1[] is referring to the complete array and hence arr1[]+1 will refer to a memory location just next to the last element of the array.
So, the output with mysizeof(arr1) gives you 12
Start two pointers i & j from the end of the string.
a. Keep copying the character from ith location to the jth location and decrement both i & j
b. When i encounters a space, decrement i, but not j
c Repeat steps a & b until i falls of the beginning of the string.
d. Keep setting ‘space’ character at jth location and decrement j unitl j falls of the beginning of the string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This can be done in O(nlogn) by inserting the elements(worst case:insert all n elements to find the 2 duplicates) into a BST and to check for duplicate elements.
Method 2:(creating count array)
Traverse the array once. While traversing, keep track of count of all elements in the array using a temp array count[] of size n, when you see an element whose count is already set, print it as duplicate.
I have an array of numbers from 1 to 100 (both inclusive). The size of the array is 100. The numbers are randomly added to the array, but there is one random empty slot in the array. What is the quickest way to find that slot as well as the number that should be put in the slot?
Solution:
Add n=100 elements using n*(n+1)/2 and subtract the sum of the array from it .you will find the missing element
The empty slot can be detected during the iteration in which the sum is computed.
source:stackoverflow
// will be the sum of the numbers in the array.
int sum = 0;
int idx = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
idx = i;
} else {
sum += arr[i];
}
}
// the total sum of numbers between 1 and arr.length.
int total = (arr.length + 1) * arr.length / 2;
System.out.println("missing number is: " + (total - sum) + " at index " + idx);
find the balance index of an array where balanced index i is defined as the one whose left sum is equal to the right sum of the index . i.e summation (1 to i-1) = summation (i+1 to length of an array)
Basically using a sorted array, for each number (target) in an array, you use two pointers, one starting from the front and one starting from the back of the array, check if the sum of the elements pointed to by the pointers is >, < or == to the target, and advance the pointers accordingly or return true if the target is found.