Related
What I want: Suppose there is an array of some size. First, I want to find the maximum element in the array, and if there is more than one element with the same maximum value, then I want to store each of the indices of the maximum element without altering the ordering of the elements in the array.
Example: Let's say there are two classes: class A & class B. In an exam, each student of class A scored differently from the others, so we could easily find the maximum score and who scored it in the said exam. However, in the same exam, two students from class B scored the maximum score. Now, we have to identify those two students (with the help of their roll numbers, say) and the score they scored. I don't know how to do that!
What I know: I know how to code a C program to find the maximum value in an array with its index value. But, if there are multiple elements with the same max value in different positions in an array, then I think I would need a new array to store the indices of the max element. Once I achieve that, I could easily find the max value with the aid of any of the stored indices.
My attempt:
#include<stdio.h>
int main(void)
{
size_t arr[5] = {2,8,10,7,10};
size_t i,j,max = arr[0];
size_t indices[5]={0};
for(j = 0;j < 5; ++j)
{
for(i = 0;i < 5; ++i)
{
if(arr[i] >= max)
indices[j] = i;
}
printf("Max Value: %zu ----> Position: %zu\n",arr[indices[j]],indices[j]+1);
}
return 0;
}
The output it generates:
Max Value: 10 ----> Position: 5
Max Value: 10 ----> Position: 5
Max Value: 10 ----> Position: 5
Max Value: 10 ----> Position: 5
Max Value: 10 ----> Position: 5
(I knew that it won't work, and it didn't as well. The index 3 is getting lost in the process!)
The desired output should read something like this:
Max Value: 10 ----> Position: 3
Max Value: 10 ----> Position: 5
Please suggest how I can code a C program that can perform the desired task.
First thing, you are never overwritting max variable, so you are comparing each value on the array against arr[0], in this case 2.
Once fixed this you have multiple solutions for your problem.
Easiest one (though not the most efficient): iterate once to get the maximum value, and iterate again to get each occurence of that value.
Alternative: iterate only once. If arr[i] < max do nothing. If arr[i] == max store its index. If arr[i] > max update max, clear indices list and store current index.
Also, take care when storing indices, as 0 represents the first element of an array and should not be used as "empty" value.
Edit:
To clarify last sentence, keep in mind that in C, arrays are indexed from 0 to SIZE-1, instead of 1 to SIZE. Imagine that you make a variable to store an array index, let's call it index, and you initialize it like:
int index = 0;
Even though 0 is usually a neutral value, in this case means "the first element of the array". This doesn't necessarily mean that your code will break, but may lead to some tricky errors and poor clarity of the code.
Since indexes are always 0 or higher, using -1 as neutral value can be a good idea.
First loop to find the maximum element, then loop to find positions having value equal to that maximum.
#include<stdio.h>
int main(void)
{
size_t arr[5] = {2,8,10,7,10};
size_t max = arr[0];
size_t indices[5]={0};
for(size_t i = 0; i < 5; ++i) if(arr[i] > max) max = arr[i];
size_t top = 0; // top contains count of elements with val = max
for(size_t j = 0; j < 5; ++j)
{
if(arr[j]==max){
indices[top] = j;
printf("Max Value: %zu ----> Position: %zu\n",arr[indices[top]],indices[top]+1);
top++;
}
}
return 0;
}
The following code should produce the results you desire:
#include <stdio.h>
int main(){
int j, i, max, element, maxElement = 0;
int arr[5] = {2,8,10,7,10};
for(i = 0;i < 5; i++){ // loop to find highest index
element = arr[i];
if(element > max)max = element;} // highest index stored in 'max'
for(j = 0; j < 5; j++){ // second loop to find instances where 'max' is matched
maxElement = arr[j];
if(arr[j] == max)
printf("arr[%d] has max of %d\n",j,max);
}
return 0;
}
This creates the output:
arr[2] has max of 10
arr[4] has max of 10
The aim of my C program is to take two arrays (both comprised of unique numbers) and merge the two of them into a new array, eliminating any numbers that are the same between both of them. However, when I try to merge the two, it instead prints back both arrays combined without eliminating any duplicates.
My program creates "array_C" by first adding in the elements from "array_A". Afterwards, it checks if there are duplicates between "array_B" and "array_C" using a counter variable. For every value in "array_C" that the for loop checks, if the value of "array_B" is not equal to the value in "array_C", the counter decreases by 1. If after all the values in "array_C" are checked, the counter is <= 0, that means there are no duplicates of that value in "array_C", and it should be added to the end of "array_C". I keep track of this using a "position" variable.
//Creation of array_C
int length_C = length_A + length_B;
int array_C[length_C];
//Copying array_A to array_C
for (i = 0; i < length_A; i++) {
array_C[i] = array_A[i];
}
//Checking array_C against array_B for duplicates
counter = length_A;
int position = length_A;
for (i = 0; i < length_B; i++) {
for (j = 0; j < length_C; j++) {
if (array_B[i] != array_C[j]) {
counter--;
} else {
counter++;
}
}
//this is the position tracker to add new value in array_C
if (counter <= 0) {
array_C[position] = array_B[i];
position++;
}
}
If I entered this:
Enter the length of array 1: 6
Enter the elements of the array: 1 2 3 4 5 6
Enter the length of array 2: 6
Enter the elements of the array: 3 4 5 6 7 8
I expect the results should look like this:
Here is the merged array:
1 2 3 4 5 6 7 8
But instead, it looks like this:
1 2 3 4 5 6 3 4 5 6 7 8
So apparently something is going wrong and it is not understanding that it should only add variables that are not duplicates.
Your logic is flawed. That's why you are getting unexpected outcome. See the following revision in your code:
for (i = 0; i < length_B; i++) {
int skip = 0;
for (j = 0; j < length_C; j++) {
if (array_B[i] == array_C[j]) {
skip=1;
break;
}
}
if(skip == 1) continue;
array_C[position++] = array_B[i];
}
the problem is with the logic inside your inner for loop. according to the problem statement if any value of array_c matches with any value of array_b you should get rid of that value otherwise add the value to array_c. so you can simply try doing the following. please make sure you understand the code. if you have any question feel free to ask.
for (i = 0; i < length_B; i++) {
bool isExistInArrayC = false;
for (j = 0; j < length_C; j++) {
if (array_B[i] == array_C[j]) {
isExistInArrayC = true;
break;
}
}
//this is the position tracker to add new value in array_C
if (isExistInArrayC == false) {
array_C[position] = array_B[i];
position++;
}
}
The suggestions will certainly work, but performance (especially with large size arrays) will be very poor. I would maintain a sorted array "C" and do a binary search into it when adding integers from array B.
You'll need a double-linked list for array C of course.
i want to create a function that take the inputs of an array and its number of values. The function should look through the array and as soon as it sees a ''3 in a row''(e.g { 1 2 3 4 5 5 5 6 7 8}), in this case 5. The function should print the index of the first 5. Im new to coding so im finding it difficult how to begin. Ive made an attempt but dont know how to proceed.
int NewFunction(int array, int numValues){
int i;
int j;
for(i=0;i<numValues;i++){
for(j=i+1;j<numValues;j++){
if(
First of all, you might want to go with a more descriptive name than NewFunction. Also, the array shouldn't be of type int, you're probably looking for a pointer to an int.
Furthermore, you don't need a nested loop like that:
for(i=0;i<numValues;i++){
for(j=i+1;j<numValues;j++){
Imagine doing this by hand, getting a list of about 1000 numbers, trying to find three in a row. How often would you pass through the list? A maximum of once, right? You wouldn't go through the list a thousand times, so neither should your algorithm, therefore you don't need a nested loop here.
What you're looking for is something like this:
int threeInARow(int* array, int numValues) {
int count = 1; // how many numbers in a row were found
int current = array[0]; // the number that we're looking for
int i = 1;
for (; i < numValues; i++) {
if (array[i] == current) {
if (++count == 3) return i - 2;
}
else { // a different number is found: start over
count = 1;
current = array[i];
}
}
return -1; // return a value indicating that no result was found
}
Start with a variable: where have you first seen the last value you saw. Let's call it last, and initialise it at 0. Then iterate index from 1 to the length of the array. If the difference between index and last is 3, return last as the index of the repeating value. If not, check whether index is at length. If so, the search failed. Otherwise, if the value at the current index is different from the value at last, set last to the current index.
Another approach.
#include <stdio.h>
#define ELEMENT 14
int three_in_a_row(int arr[], int n) {
int i, index, count = 0, max = 0;
i = -1;
do {
i = i + 1;
if (arr[i] == arr[i + 1]) {
index = i-1; //because we want the first occurrence
count++;
if (count > max) max = count; // 3 elements in a row means that max must be 3-1
} else
count = 0;
} while (i < n - 2 && max != 3 - 1);
return (max == 2 ? index : -1);// -1 indicates no result
}
int main(void) {
int array[] = {1,10,1,4,4,8,8,8,7,8,8,9,9,2}, index3;
index3 = three_in_a_row(array, ELEMENT);
printf("%d\n", index3);
return (0);
}
I have written a small piece of code that would perform Run length encoding kind of stuff on 1-D array but still far from desired result.
main()
{
int a[8]={2,0,0,0,3,0,0,9};
int i,temp,ct=0,flag,m;
int found[90]={0};
for(i=0;i<=7;i++)
{
if(!a[i])
{
ct++;
if(!found[a[i]])
{
flag=i;
found[a[i]]=1;
}
}
}
a[flag]=ct;
m=ct;
for(i=0;i<m;i++)
{
printf("%d",a[i]);
}
}/* end of main*/
Now for above array i would like to have output something below
2 5 0 3 9
But with my piece of code am getting
2 5 0 0 3
Can I have any suggestion on that?
Shouldn't run length encoding turn 2,0,0,0,3,0,0,9 into 2 1 0 3 3 1 2 0 9 1?
1) The first thing I see is wrong is that you aren't looking at the entire array. You're using < to stop before 8, but also stopping at 7, so you only evaluate array items 0 - 6.
2) If ct stands for count it's never reset (ct=0 only on declaration). Also it's assignment is this: a[flag]= ct; which overwrites your original data. It basically tracks the value of i.
This is my version I've just put together:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int runningCount = 1; //because we start at array index 1 and not zero
for (i = 1; i <= SZ; i++) {
if (a[i - 1] == a[i]) //value same as one before it...
runningCount++;
else { // new value found. print last one, and the count of the last one.
printf("%d %d ", a[i - 1], runningCount);
runningCount = 1; //reset for next loop
}
}
return 0;
}
The output is 2 1 0 3 3 1 0 2 9 1
Ok based on the comment left below, your algorithm would actually look like this:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int zero_count = 0; //target zeros specifically...
for (i = 0; i < SZ; i++) {
if (a[i] == 0)
zero_count++;
}
//now write it out in a bizarre, unparsable format again...
for (i = 0; i < SZ; i++) {
if (a[i] != 0) //write out all non zero values
printf("%d ", a[i]);
if (i == 0) { //this says put the zero count after the first number was printed
printf("%d 0 ", zero_count); //inserting it into a strange place in the array
}
}
return 0;
}
which outputs: 2 5 0 3 9
You need a <= in your for loop:
for(i=0;i<=7;i++)
instead of
for(i=0;i< 7;i++)
Otherwise you miss the last element.
All you appear to be doing is (a) counting the number of times 0 occurs in the array, and (b) replacing the first occurrence of 0 with that count. It's not clear how this is meant to be a useful encoding.
In any case, you're not getting your desired result, at least in part, because you're only modifying one element of the array. I suspect what you want, or at least think you want, is to shift the non-zero elements of the array to the left as you encounter them.
What is the utility of compressing the array in the way you propose? Is some other piece of code going to have to reconstruct the original, and if so how do you expect to do so from your desired result?
I recently came across a question somewhere:
Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once. Describe an algorithm to find the repeated number. If you used auxiliary storage in your algorithm, can you find an algorithm that does not require it?
What I am interested in to know is the second part, i.e., without using auxiliary storage. Do you have any idea?
Just add them all up, and subtract the total you would expect if only 1001 numbers were used from that.
Eg:
Input: 1,2,3,2,4 => 12
Expected: 1,2,3,4 => 10
Input - Expected => 2
Update 2: Some people think that using XOR to find the duplicate number is a hack or trick. To which my official response is: "I am not looking for a duplicate number, I am looking for a duplicate pattern in an array of bit sets. And XOR is definitely suited better than ADD to manipulate bit sets". :-)
Update: Just for fun before I go to bed, here's "one-line" alternative solution that requires zero additional storage (not even a loop counter), touches each array element only once, is non-destructive and does not scale at all :-)
printf("Answer : %d\n",
array[0] ^
array[1] ^
array[2] ^
// continue typing...
array[999] ^
array[1000] ^
1 ^
2 ^
// continue typing...
999^
1000
);
Note that the compiler will actually calculate the second half of that expression at compile time, so the "algorithm" will execute in exactly 1002 operations.
And if the array element values are know at compile time as well, the compiler will optimize the whole statement to a constant. :-)
Original solution: Which does not meet the strict requirements of the questions, even though it works to find the correct answer. It uses one additional integer to keep the loop counter, and it accesses each array element three times - twice to read it and write it at the current iteration and once to read it for the next iteration.
Well, you need at least one additional variable (or a CPU register) to store the index of the current element as you go through the array.
Aside from that one though, here's a destructive algorithm that can safely scale for any N up to MAX_INT.
for (int i = 1; i < 1001; i++)
{
array[i] = array[i] ^ array[i-1] ^ i;
}
printf("Answer : %d\n", array[1000]);
I will leave the exercise of figuring out why this works to you, with a simple hint :-):
a ^ a = 0
0 ^ a = a
A non destructive version of solution by Franci Penov.
This can be done by making use of the XOR operator.
Lets say we have an array of size 5: 4, 3, 1, 2, 2
Which are at the index: 0, 1, 2, 3, 4
Now do an XOR of all the elements and all the indices. We get 2, which is the duplicate element. This happens because, 0 plays no role in the XORing. The remaining n-1 indices pair with same n-1 elements in the array and the only unpaired element in the array will be the duplicate.
int i;
int dupe = 0;
for(i = 0; i < N; i++) {
dupe = dupe ^ arr[i] ^ i;
}
// dupe has the duplicate.
The best feature of this solution is that it does not suffer from overflow problems that is seen in the addition based solution.
Since this is an interview question, it would be best to start with the addition based solution, identify the overflow limitation and then give the XOR based solution :)
This makes use of an additional variable so does not meet the requirements in the question completely.
Add all the numbers together. The final sum will be the 1+2+...+1000+duplicate number.
To paraphrase Francis Penov's solution.
The (usual) problem is: given an array of integers of arbitrary length that contain only elements repeated an even times of times except for one value which is repeated an odd times of times, find out this value.
The solution is:
acc = 0
for i in array: acc = acc ^ i
Your current problem is an adaptation. The trick is that you are to find the element that is repeated twice so you need to adapt solution to compensate for this quirk.
acc = 0
for i in len(array): acc = acc ^ i ^ array[i]
Which is what Francis' solution does in the end, although it destroys the whole array (by the way, it could only destroy the first or last element...)
But since you need extra-storage for the index, I think you'll be forgiven if you also use an extra integer... The restriction is most probably because they want to prevent you from using an array.
It would have been phrased more accurately if they had required O(1) space (1000 can be seen as N since it's arbitrary here).
Add all numbers. The sum of integers 1..1000 is (1000*1001)/2. The difference from what you get is your number.
One line solution in Python
arr = [1,3,2,4,2]
print reduce(lambda acc, (i, x): acc ^ i ^ x, enumerate(arr), 0)
# -> 2
Explanation on why it works is in #Matthieu M.'s answer.
If you know that we have the exact numbers 1-1000, you can add up the results and subtract 500500 (sum(1, 1000)) from the total. This will give the repeated number because sum(array) = sum(1, 1000) + repeated number.
Well, there is a very simple way to do this... each of the numbers between 1 and 1000 occurs exactly once except for the number that is repeated.... so, the sum from 1....1000 is 500500. So, the algorithm is:
sum = 0
for each element of the array:
sum += that element of the array
number_that_occurred_twice = sum - 500500
n = 1000
s = sum(GivenList)
r = str(n/2)
duplicate = int( r + r ) - s
public static void main(String[] args) {
int start = 1;
int end = 10;
int arr[] = {1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10};
System.out.println(findDuplicate(arr, start, end));
}
static int findDuplicate(int arr[], int start, int end) {
int sumAll = 0;
for(int i = start; i <= end; i++) {
sumAll += i;
}
System.out.println(sumAll);
int sumArrElem = 0;
for(int e : arr) {
sumArrElem += e;
}
System.out.println(sumArrElem);
return sumArrElem - sumAll;
}
No extra storage requirement (apart from loop variable).
int length = (sizeof array) / (sizeof array[0]);
for(int i = 1; i < length; i++) {
array[0] += array[i];
}
printf(
"Answer : %d\n",
( array[0] - (length * (length + 1)) / 2 )
);
Do arguments and callstacks count as auxiliary storage?
int sumRemaining(int* remaining, int count) {
if (!count) {
return 0;
}
return remaining[0] + sumRemaining(remaining + 1, count - 1);
}
printf("duplicate is %d", sumRemaining(array, 1001) - 500500);
Edit: tail call version
int sumRemaining(int* remaining, int count, int sumSoFar) {
if (!count) {
return sumSoFar;
}
return sumRemaining(remaining + 1, count - 1, sumSoFar + remaining[0]);
}
printf("duplicate is %d", sumRemaining(array, 1001, 0) - 500500);
public int duplicateNumber(int[] A) {
int count = 0;
for(int k = 0; k < A.Length; k++)
count += A[k];
return count - (A.Length * (A.Length - 1) >> 1);
}
A triangle number T(n) is the sum of the n natural numbers from 1 to n. It can be represented as n(n+1)/2. Thus, knowing that among given 1001 natural numbers, one and only one number is duplicated, you can easily sum all given numbers and subtract T(1000). The result will contain this duplicate.
For a triangular number T(n), if n is any power of 10, there is also beautiful method finding this T(n), based on base-10 representation:
n = 1000
s = sum(GivenList)
r = str(n/2)
duplicate = int( r + r ) - s
I support the addition of all the elements and then subtracting from it the sum of all the indices but this won't work if the number of elements is very large. I.e. It will cause an integer overflow! So I have devised this algorithm which may be will reduce the chances of an integer overflow to a large extent.
for i=0 to n-1
begin:
diff = a[i]-i;
dup = dup + diff;
end
// where dup is the duplicate element..
But by this method I won't be able to find out the index at which the duplicate element is present!
For that I need to traverse the array another time which is not desirable.
Improvement of Fraci's answer based on the property of XORing consecutive values:
int result = xor_sum(N);
for (i = 0; i < N+1; i++)
{
result = result ^ array[i];
}
Where:
// Compute (((1 xor 2) xor 3) .. xor value)
int xor_sum(int value)
{
int modulo = x % 4;
if (modulo == 0)
return value;
else if (modulo == 1)
return 1;
else if (modulo == 2)
return i + 1;
else
return 0;
}
Or in pseudocode/math lang f(n) defined as (optimized):
if n mod 4 = 0 then X = n
if n mod 4 = 1 then X = 1
if n mod 4 = 2 then X = n+1
if n mod 4 = 3 then X = 0
And in canonical form f(n) is:
f(0) = 0
f(n) = f(n-1) xor n
My answer to question 2:
Find the sum and product of numbers from 1 -(to) N, say SUM, PROD.
Find the sum and product of Numbers from 1 - N- x -y, (assume x, y missing), say mySum, myProd,
Thus:
SUM = mySum + x + y;
PROD = myProd* x*y;
Thus:
x*y = PROD/myProd; x+y = SUM - mySum;
We can find x,y if solve this equation.
In the aux version, you first set all the values to -1 and as you iterate check if you have already inserted the value to the aux array. If not (value must be -1 then), insert. If you have a duplicate, here is your solution!
In the one without aux, you retrieve an element from the list and check if the rest of the list contains that value. If it contains, here you've found it.
private static int findDuplicated(int[] array) {
if (array == null || array.length < 2) {
System.out.println("invalid");
return -1;
}
int[] checker = new int[array.length];
Arrays.fill(checker, -1);
for (int i = 0; i < array.length; i++) {
int value = array[i];
int checked = checker[value];
if (checked == -1) {
checker[value] = value;
} else {
return value;
}
}
return -1;
}
private static int findDuplicatedWithoutAux(int[] array) {
if (array == null || array.length < 2) {
System.out.println("invalid");
return -1;
}
for (int i = 0; i < array.length; i++) {
int value = array[i];
for (int j = i + 1; j < array.length; j++) {
int toCompare = array[j];
if (value == toCompare) {
return array[i];
}
}
}
return -1;
}