Thursday, August 3, 2017

first hacker rank challenge :)

First hacker rank challenge:
left rotation operation on an array of size  shifts each of the array's elements  unit to the left. For example, if left rotations are performed on array , then the array would become .
Given an array of  integers and a number, , perform  left rotations on the array. Then print the updated array as a single line of space-separated integers.
Input Format
The first line contains two space-separated integers denoting the respective values of  (the number of integers) and  (the number of left rotations you must perform).
The second line contains  space-separated integers describing the respective elements of the array's initial state.
Constraints
Output Format
Print a single line of  space-separated integers denoting the final state of the array after performing  left rotations.
Sample Input
5 4
1 2 3 4 5
Sample Output
5 1 2 3 4
Explanation
When we perform  left rotations, the array undergoes the following sequence of changes:
Thus, we print the array's final state as a single line of space-separated values, which is 5 1 2 3 4.

My code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    int k; 
    int x,y;
    scanf("%d %d",&n,&k);
    int *a = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i < n; a_i++){
       scanf("%d",&a[a_i]);
    }
    int *k_arr = malloc(sizeof(int)*n);
    for(int k_i=0;k_i<k;k_i++) {
        k_arr[k_i]=a[k_i];
    }
    //move the original arrary from index 0 to x
    for(x=0,y=k ;y<n;x++,y++) {
        a[x]=a[y];
    }
    //append the k_arr value from x+1 position
    for(y=0;x<n;x++,y++) {
        a[x]=k_arr[y];
    }
    for(y=0;y<n;y++) {
        printf("%d ",a[y]);
    }
    return 0;
}
2. making anagrams:

Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not.
Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings,  and , that may or may not be of the same length, determine the minimum number of character deletions required to make  and  anagrams. Any characters can be deleted from either of the strings.
Input Format
The first line contains a single string, .
The second line contains a single string, .
Constraints
  • It is guaranteed that  and  consist of lowercase English alphabetic letters (i.e.,  through ).
Output Format
Print a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.
Sample Input
cde
abc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of each other:
  1. Remove d and e from cde to get c.
  2. Remove a and b from abc to get c.
We must delete  characters to make both strings anagrams, so we print  on a new line.



#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    char* a = (char *)malloc(512000 * sizeof(char));
    scanf("%s",a);
    char* b = (char *)malloc(512000 * sizeof(char));
    scanf("%s",b);
    int a_arr[26]={0},b_arr[26]={0},char_to_be_deleted=0;
    //making z=122,a-97. by subtraction we can make the index between 0 to 25
    for(int i=0;i<strlen(a);i++) {
        a_arr[a[i]-'a']++;
    }
    for(int i=0;i<strlen(b);i++) {
        b_arr[b[i]-'a']++;
    }
    for(int i=0;i<strlen(a);i++) {
        if(!b_arr[a[i]-'a']) {
            char_to_be_deleted++;
        }
    }
    for(int i=0;i<strlen(b);i++) {
        if(!a_arr[b[i]-'a']) {
            char_to_be_deleted++;
        }
    }
    printf("%d",char_to_be_deleted);
    return 0;
}

solution idea:http://techieme.in/make-anagrams-from-two-strings/


3. c program to split the strings into the character:
[tselva@bng-junos-d037 /b/tselva/bcopy/bk_copy_needed/copy-needed/work_tips_dir/sample_c_program]$ ./a.out
my
name
is
selva
kumar

[tselva@bng-junos-d037 /b/tselva/bcopy/bk_copy_needed/copy-needed/work_tips_dir/sample_c_program]$ gcc strtok.c
[tselva@bng-junos-d037 /b/tselva/bcopy/bk_copy_needed/copy-needed/work_tips_dir/sample_c_program]$ cat strtok.c
#include<stdio.h>
#include<string.h>
int main()
{
    char name[]="my name is selva kumar";
    char *ptr=strtok(name," ");
    while(ptr!=NULL) {
    printf("%s\n",ptr);
    ptr=strtok(NULL," ");
    }
    printf("\n");
    return 0;
}
[tselva@bng-junos-d037 /b/tselva/bcopy/bk_copy_needed/copy-needed/work_tips_dir/sample_c_program]$
http://ecomputernotes.com/what-is-c/puppetting-on-strings/separate-the-words-in-a-given-string
https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
http://see-programming.blogspot.in/2013/07/c-program-to-print-words-in-string_14.html
http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.neutrino_lib_ref%2Fs%2Fstrtok.html


No comments:

Post a Comment