Test 2 stings for same words (C) - c

i am working on algorithm, that would compare two sentences and find if they are made of same words.
Basically the input of this function are 2 strings, i cut them by " " and each word put in array. So i have something like this:
sent1[0]="one"
sent1[1]="two"
sent1[2]="three"
sent1[3]="four"
sent2[0]="four"
sent2[1]="two"
sent2[2]="one"
sent2[3]="three"
My algoritm works that for each word from sent1 it compares each word in sent2.If the sentences are equal it return 1 , otherwise it return 0;
//n = max index of sent1 and m is max index of sent2
int equal =0;
for (i = 0; i < (n); i++){
for (x = 0; x < (m); x++){
if(strcmp(sent1[i],sent2[x])==0){
equal =1;
}
}
if(equal==0){
return 0;
}
equal=0;
}
return 1;
Problem of this algoritm is that it is very uneffective and slow, in order to make it faster it would be best to somehow delete index of array i already found equal word so every time i do new search it would search smaller array.Problem is that i dont know how to implement this in C because my every try ended with memory problems.
Thank you for any help.

In C language you cannot delete an element from its array, because an C array a contiguous block of memory.
But you can shift left the elements after you "delete" an item.
However an another suggestion about your code:
When the 'strcmp' function success (found equal) then you no need continue the actual loop, you can terminate that with 'break' command.
int equal =0;
for (i = 0; i < (n); i++){
for (x = 0; x < (m); x++){
if(strcmp(sent1[i],sent2[x])==0){
equal =1;
break;
}
}
if(equal==0){
return 0;
}
equal=0;
}
return 1;

Related

Finding Number of duplicates in an array

I have an array, say 1,3,3,1,2 The output of the code must be 4(2 repetitions of 1 + 2 repetitions of 3=4). How can I do this in C? Here's my attempt.
#include <stdio.h>
int main(){
int n,i,j,temp;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++){
int min = i;
for(j=i+1;j<n;j++){
if(arr[j]<arr[min]) min=j;
}
temp= arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
int count=1;
for(i=0;i<n;i++){
if(arr[i]==arr[i+1])count++;
else continue;
}
printf("%d",count);
}
What you need is to change this for loop.
int count=1;
for(i=0;i<n;i++){
if(arr[i]==arr[i+1])count++;
else continue;
}
It can look for example the following way
int count = 0;
for ( i = 0; i < n; )
{
int j = i;
while ( ++i < n && arr[i-1] == arr[i] );
if ( !( i - j < 2 ) ) count += i - j;
}
It looks like your loop has a couple of problems.
It indexes past the end of the array, which is undefined behavior
It doesn't understand when to count the first item in a group of duplicates
Regarding #1 it's nice to just start your loop at 1 instead of 0 and then check index i-1 against i.
Regarding #2 your code works but only when there's only one number that has duplicates. This is because you started the count at 1. However, when you encounter another group, that assumption breaks down. The simplest way is to just record whether you're starting a new group.
Let's put this all together:
int count = 0;
int first = 1;
for(i = 1; i < n; i++) {
if (arr[i-1] == arr[i]) {
count += first + 1;
first = 0;
} else {
first = 1;
}
}
As for the sorting step, it's using a wildly inefficient algorithm. This is fine for small datasets, but you'll have a problem if you have a very large number of inputs. It would be wise to use something like qsort instead. There are many examples out there for how to do this.
So, your runtime right now is O(N^2). With quicksort it becomes O(N.logN).
You can probably reduce runtime further with something like a hash table that simply stores how many of each value you've found, which you update as they arrive.
If your data ranges are well-defined and small enough, you might also benefit from using a large array instead of a hash table and store a single bit for each possible number representing when a number is seen. Actually for your case you'd need two of these because of the "first in the group" problem. Now, each number that arrives sets the "seen" bit. If it was already seen, set the "duplicate" bit and increment the count. If the "duplicate" bit is not set, increment the count. Now you pretty much guarantee blazing-fast O(N) runtime where testing for and counting a duplicate value is O(1).

A function that returns 0 or 1 if all elements in the range 0 to n-1 exist in the array, run time O(n)

EDIT:
I forgot to mention that I do not want to allocate another temporarily array.
I am trying to solve a problem in C, which is:
Suppose you were given an array a and it's size N. You know that all of the elements in the array are between 0 to n-1. The function is supposed to return 0 if there is a missing number in the range (0 to n-1). Otherwise, it returns 1. As you can understand, duplicates are possible. The thing is that its supposed to run on O(n) runtime.
I think I managed to do it but i'm not sure. From looking at older posts here, it seems almost impossible and the algorithm seems much more complicated then the algorithm I have. Therefore, something feels wrong to me.
I could not find an input that returns the wrong output yet thou.
In any case, I'd appreciate your feedback- or if you can think of an input that this might not work for. Here's the code:
int missingVal(int* a, int size)
{
int i, zero = 0;
for (i = 0; i < size; i++)
//We multiply the element of corresponding index by -1
a[abs(a[i])] *= -1;
for (i = 0; i < size; i++)
{
//If the element inside the corresponding index is positive it means it never got multiplied by -1
//hence doesn't exist in the array
if (a[i] > 0)
return 0;
//to handle the cases for zeros, we will count them
if (a[i] == 0)
zero++;
}
if (zero != 1)
return 0;
return 1;
}
Just copy the values to another array placing each value in its ordinal position. Then walk the copy to see if anything is missing.
your program works and it is in O(N), but it is quite complicated and worst it modify the initial array
can be just that :
int check(int* a, int size)
{
int * b = calloc(size, sizeof(int));
int i;
for (i = 0; i != size; ++i) {
b[a[i]] = 1;
}
for (i = 0; i != size; ++i) {
if (b[i] == 0) {
free(b);
return 0;
}
}
free(b);
return 1;
}
This problem is the same as finding out if your array has duplicates. Here's why
All the numbers in the array are between 0 and n-1
The array has a size of n
If there's a missing number in that range, that can only mean that another number took its place. Which means that the array must have a duplicate number
An algorithm in O(n) time & O(1) space
Iterate through your array
If the sign of the current number is positive, then make it negative
If you found a negative this means that you have a duplicate. Since all items are originally greater (or equal) than 0
Implementation
int missingVal(int arr[], int size)
{
// Increment all the numbers to avoid an array with only 0s
for (int i = 0; i < size; i++) arr[i]++;
for (int i = 0; i < size; i++)
{
if (arr[abs(arr[i])] >= 0)
arr[abs(arr[i])] = -arr[abs(arr[i])];
else
return 0;
}
return 1;
}
Edit
As Bruno mentioned if we have an array with all zeros, we could have run into a problem. This is why I included in this edit an incrementation of all the numbers.
While this add another "pass" into the algorithm, the solution is still in O(n) time & O(1) space
Edit #2
Another great suggestion from Bruno which optimizes this, is to look if there's more than one zero instead of incrementing the array.
If there's 2 or more, we can directly return 0 since we have found a duplicate (and by the same token that not all the numbers in the range are in the array)
To overcome the requirement that excludes any extra memory consumption, the posted algorithm changes the values inside the array by simply negating their value, but that would leave index 0 unchanged.
I propose a different mapping: from [0, size) to (-1 - size, -1], so that e.g. {0, 1, 2, 3, 4, ...} becomes {-1, -2, -3, -4, -5, ...}. Note that, for a two's complement representation of integers, INT_MIN = -INT_MAX - 1.
// The following assumes that every value inside the array is in [0, size-1)
int missingVal(int* a, int size) // OT: I find the name misleading
{
int i = 0;
for (; i < size; i++)
{
int *pos = a[i] < 0
? a + (-a[i] - 1) // A value can already have been changed...
: a + a[i];
if ( *pos < 0 ) // but if the pointed one is negative, there's a duplicate
break;
*pos = -1 - *pos;
}
return i == size; // Returns 1 if there are no duplicates
}
If needed, the original values could be restored, before returning, with a simple loop
if ( i != size ) {
for (int j = 0; j < size; ++j) {
if ( a[j] < 0 )
a[j] = -a[j] - 1;
}
} else { // I already know that ALL the values are changed
for (int j = 0; j < size; ++j)
a[j] = -a[j] - 1;
}

checking if a array has numbers in it from 0 to length -1 in C

I have got an assignment and i'll be glad if you can help me with one question
in this assignment, i have a question that goes like this:
write a function that receives an array and it's length.
the purpose of the function is to check if the array has all numbers from 0 to length-1, if it does the function will return 1 or 0 otherwise.The function can go through the array only one.
you cant sort the array or use a counting array in the function
i wrote the function that calculate the sum and the product of the array's values and indexes
int All_Num_Check(int *arr, int n)
{
int i, index_sum = 0, arr_sum = 0, index_multi = 1, arr_multi = 1;
for (i = 0; i < n; i++)
{
if (i != 0)
index_multi *= i;
if (arr[i] != 0)
arr_multi *= arr[i];
index_sum += i;
arr_sum += arr[i];
}
if ((index_sum == arr_sum) && (index_multi == arr_multi))
return 1;
return 0;
}
i.e: length = 5, arr={0,3,4,2,1} - that's a proper array
length = 5 , arr={0,3,3,4,2} - that's not proper array
unfortunately, this function doesnt work properly in all different cases of number variations.
i.e: length = 5 , {1,2,2,2,3}
thank you your help.
Checking the sum and product is not enough, as your counter-example demonstrates.
A simple solution would be to just sort the array and then check that at every position i, a[i] == i.
Edit: The original question was edited such that sorting is also prohibited. Assuming all the numbers are positive, the following solution "marks" numbers in the required range by negating the corresponding index.
If any array cell already contains a marked number, it means we have a duplicate.
int All_Num_Check(int *arr, int n) {
int i, j;
for (i = 0; i < n; i++) {
j = abs(arr[i]);
if ((j >= n) || (arr[j] < 0)) return 0;
arr[j] = -arr[j];
}
return 1;
}
I thought for a while, and then i realized that it is a highly contrained problem.
Things that are not allowed:
Use of counting array.
Use of sorting.
Use of more than one pass to the original array.
Hence, i came up with this approach of using XOR operation to determine the results.
a ^ a = 0
a^b^c = a^c^b.
Try this:
int main(int argc, char const *argv[])
{
int arr[5], i, n , temp = 0;
for(i=0;i<n; i++){
if( i == 0){
temp = arr[i]^i;
}
else{
temp = temp^(i^arr[i]);
}
}
if(temp == 0){
return 1;
}
else{
return 0;
}
}
To satisfy the condition mentioned in the problem, every number has to occour excatly once.
Now, as the number lies in the range [0,.. n-1], the looping variable will also have the same possible range.
Variable temp , is originally set to 0.
Now, if all the numbers appear in this way, then each number will appear excatly twice.
And XORing the same number twice results in 0.
So, if in the end, when the whole array is traversed and a zero is obtained, this means that the array contains all the numbers excatly once.
Otherwise, multiple copies of a number is present, hence, this won't evaluate to 0.

Minimum number of steps required to reach the last index

Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
I have built a dp[] array from left to right such that dp[i] indicates the minimum number of jumps needed to reach arr[i] from arr[0]. Finally, we return dp[n-1].
Worst case time complexity of my code is O(n^2).
Can this be done in a better time complexity.
This question is copied from leetcode.
int jump(vector<int>& a) {
int i,j,k,n,jumps,ladder,stairs;
n = a.size();
if(n==0 || n==1)return 0;
jumps = 1, ladder = stairs = a[0];
for(i = 1; i<n; i++){
if(i + a[i] > ladder)
ladder = i+a[i];
stairs --;
if(stairs + i >= n-1)
return jumps;
if(stairs == 0){
jumps++;
stairs = ladder - i;
}
}
return jumps;
}
You can use a range-minimum segment tree to solve this problem. A segment tree is a data structure which allows you to maintain an array of values and also query aggregate operations on subsegments of the array. More information can be found here: https://cses.fi/book/book.pdf (section 9.3)
You will store values d[i] in the segment tree, d[i] is the minimum number of steps needed to reach the last index if you start from index i. Clearly, d[n-1] = 0. In general:
d[i] = 1 + min(d[i+1], ..., d[min(n-1, i+a[i])]),
so you can find all the values in d by computing them backwards, updating the segment tree after each step. The final solution is d[0]. Since both updates and queries on segment trees work in O(log n), the whole algorithm works in O(n log n).
I think, you can boost computing the dynamic with these technique:
You spend O(N) for compute current d[i]. But you can keep a set with d[j],
where j = 0..i - 1. And now all you need to use binary search to find:
such d[j], that is minimum among all(0..i-1) and from j position i-pos is reachable.
It will be O(n * logn) solution
That is a simple excercise in dynamic programming. As you have tagged it already, I wonder why you're not trying to apply it.
Let V[k] be the minimum number of steps to get from position k to the end of the list a = (a[0], a[1], ...., a[n-1]).
Then obviously V[n-1]=0. Now loop backwards:
for(int k=n-2;k>=0;--k)
{
int minStep = n + 1;
for(int j=k+1;j<=std::min(n-1,k+a[k]);++j)
{
minStep = std::min(minStep, V[j])
}
V[k]= minStep + 1;
}
Demo in C++
After the loop, which takes O(a[0]+a[1]+...+a[n-1]) time, V[0] contains the minimum number of steps to reach the end of the list.
In order to find the way through the list, you can then choose the action greedily. That is, from position k you always go to an allowed position l where V[l] is minimal.
(Note that I've assumed positive entries of the list here, not non-negative ones. Possible zeros can easily be removed from the problem, as it is never optimal to go there.)
https://leetcode.com/problems/jump-game-ii
class Solution {
public int jump(int[] nums) {
int n = nums.length;
if(n < 2){
return 0;
}
int ans = 1;
int rightBoundaryCovered = nums[0];
for(int i=1;i<n;i++){
if(rightBoundaryCovered >= n-1){
return ans;
}
int currMax = i+ nums[i];
while(rightBoundaryCovered>=i){
currMax = Math.max(currMax, i+nums[i]);
i++;
}
//missed this decrement statement and faced multiple WA's
i--;
ans++;
if(currMax>rightBoundaryCovered){
rightBoundaryCovered = currMax;
}
}
return ans;
}
}
Java solution (From Elements of Programming Interviews):
public boolean canJump(int[] nums) {
int maximumReach = 0;
for(int i = 0; i < nums.length; i++) {
// Return false if you jump more.
if(i > maximumReach) { return false; }
// Logic is we need to keep checking every index the
// farthest we can travel
// Update the maxReach accordingly.
maximumReach = Math.max(i + nums[i], maximumReach);
}
return true;
}

Iterate ALL the elements of a circular 2D array exactly once given a random starting element

We are given a 2-dimensional array A[n,m] with n rows and m columns and an element of that array chosen at random R.
Think of the array as being circular in that when we visit A[n-1, m-1] the next element we visit would be A[0, 0].
Starting with element R, we want to visit each element exactly once and call function foo() before moving to the next element.
The following is my first implementation but there is a bug. The bug being that if we start at row x somewhere between 0 and n-1, we will not visit element from 0 to x-1 in that column.
// Init - pretend rand() always returns valid index in range
curr_row = rand();
curr_col = rand();
// Look at each column once
for (int i = 0; i < m; ++i)
{
for (; curr_row < n; ++curr_row)
{
foo(A[curr_row][curr_col]);
}
curr_row = 0;
curr_col = (curr_col + 1) % m;
}
What is a clean way to do this traversal such that we meet the above requirements?
Just move to the next index, and check whether you are back at the start, in which case, stop:
// should be something that guarantees in-range indices
curr_row = rand();
curr_col = rand();
int i = curr_row, j = curr_col;
do {
foo(A[i][j]);
++j;
if (j == n) {
j = 0;
++i;
if (i == m) {
i = 0;
}
}
}while(i != curr_row || j != curr_col);
This doesn't do what your implementation does, but what the question title asks for.
quite rusty with c , but it should be the same:
// Init - pretend rand() always returns valid index in range
curr_row = rand();
curr_col = rand();
//first row
for(int j=curr_col;j<m;++j)
foo(A[curr_row][j]);
//rest of the rows
for(int i=(curr_row+1)%n;i!=curr_row;i=(i+1)%n)
for(int j=0;j<m;++j)
foo(A[i][j]);
//first row , going over missed cells
for(int j=0;j<curr_col;++j)
foo(A[curr_row][j]);
if you care a lot about performance , you can also divide the second loop so that there won't be a "%" at all .
another alternative , since C has 2d arrays in a simple array:
// Init - pretend rand() always returns valid index in range
curr_row = rand();
curr_col = rand();
int start=curr_row*m+curr_col;
int maxCell=n*m;
int end=(start-1)%maxCell;
for(int i=start;i!=end;i=(i+1)%maxCell)
foo(A[i]);
foo(A[end]);
could have a tiny math bug here and there ,but the idea is ok.
A[curr_row, curr_col] is not the syntax used to access a member of a multidimensional array; instead, you want A[curr_row][curr_col], assuming the array was declared correctly. A[curr_row, curr_col] will invoke the comma operator, which effectively computes the first value, then throws it away and calculates the second value, then indexes the array with that value.

Resources