UVa 482 Permutation Arrays Solution in C++

When the size of the array is not known before hand. The vector is used and character wise input is taken and integers and floats are created.

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
	int T; scanf("%d", &T);
	getchar();getchar();
	char c, int_s[12];
	int i = 0;
	vector<int> p;
	while(true){
		c = getchar();
		if(c==' '){
			int_s[i] = '\0'; i = 0;
			p.push_back(atoi(int_s));
		} else if(c=='\n'){
			int_s[i] = '\0'; i = 0;
			p.push_back(atoi(int_s));
			break;
		} else{
			int_s[i++] = c;
		}
	}
	float A[p.size()];
	float B[p.size()];
	for(int j = 0; j<p.size(); j++){
		scanf("%f", A+j);
	}
	for(int j = 0; j<p.size(); j++){
		B[p[j]-1] = A[j];
	}
	for(int j = 0; j<p.size(); j++){
		printf("%f\n", B[j]);
	}
	return 0;
}

Note: Verdict is not accepted, because of some stupid bug. The main thing to learn is to get the input in the required format.

HackerRank Pairs Solution in C++

Given N integers, count the number of pairs of integers whose difference is K.

First the array of integers is sorted and then binary search is applied for the value of +K of the given indexed value of the array. This will work because all the values in the array are distinct.

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
	int N, K;
	scanf("%d %d", &N, &K);
	int a[N];
	for(int n = 0; n<N; n++){
		scanf("%d", &a[n]);
	}
	sort(a, a+N);
	int count = 0;
	for(int n = 0; n<N; n++)
		if(binary_search(a, a+N, a[n]+K)) count++;
	printf("%d", count);
}

Sample Input:

10 1  
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793

Sample Output:

0

Runtime: 0.02s

HackerRank Missing Numbers Solution in C++

Simple frequency counting as done in counting sort and then subtracting the frequency of the given number in other list. Printing the number times the frequency for the remaining numbers.

Things to note: The range of the values of x.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

#define SIZE 10001
using namespace std;


int main() {
    int a[SIZE];
    for(int i = 0; i<SIZE;i++){
        a[i]=0;
    }
    int N, M;
    scanf("%d", &N);
    int temp;
    for(int n=0; n<N; n++){
        scanf("%d", &temp);
        a[temp]+=1;
    }
    scanf("%d", &M);
    for(int m =0; m<M; m++){
        scanf("%d", &temp);
        a[temp]-=1;
    }
    for(int i =0; i<SIZE; i++){
        if(a[i]<0) printf("%d ", i);
    }
    return 0;
}

Sample Input:

10
203 204 205 206 207 208 203 204 205 206
13
203 204 204 205 206 207 205 208 203 206 205 206 204

Sample Output:

204 205 206

Runtime: 0.03s

HackerRank Encryption Solution in C++

Simple indexing calculations…

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int main(){
    char s[1000];
	gets(s);
	int len = strlen(s);
	int width, height;
	int lo = floor(sqrt(len)), hi = ceil(sqrt(len));
	int i, j;
	bool found = false;
	for(i = lo; i<=hi; i++){
		for(j = i; j<=hi; j++){
			if(i*j>=len) {
				found = true;
				break;
			}
		}
		if(found) break;
	}
	width = j; height = i;
	int index = 0;
	for(i = 0; i<width; i++){
		for(j = 0; j<height; j++){
			index = j*width +i;
			if(index<len) putchar(s[index]);
		}
		putchar(' ');
	}
}

HackerRank Ice Cream Parlor Solution in C++

Nothing to learn

#include<cstdio>
#include<vector>

using namespace std;

int main(){
    int T;
    scanf("%d", &T);
	for(int t = 0; t<T; t++){
		int M, N;
		scanf("%d", &M);
		scanf("%d", &N);
		int c[N];
		for(int n = 0; n<N; n++){
			scanf("%d", &c[n]);
		}
		int i, j;
		bool found = false;
		for(i = 0; i<N; i++){
			for(j = i+1; j<N; j++){
				if(c[i]+c[j]==M){
					found = true;
					break;
				}
			}
			if(found) break;
		}
		printf("%d %d\n", i+1, j+1);
	}
}

HackerRank Sam and Subarray Solution in C++

Things to learn:

  • Memoization
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>

using namespace std;

#define MOD 1000000007

int main(){
	char N[200001];
	gets(N);
	long long c_p = 10;
	long long c_num = 0;
	long long c_pos = 1;
	long long sum_p = 1;
	long long total = 0;
	int len_s = strlen(N);
	c_p=1;
	int t = len_s;
	long long temp[t];
	temp[0] = 1;
	while(t--){
		c_num=(N[t]-'0');
		total= (total+(c_num%MOD * sum_p%MOD * (t+1)%MOD)%MOD)%MOD;
		temp[len_s-t]=(temp[len_s-t-1]*10)%MOD;
		sum_p = (sum_p+temp[len_s-t])%MOD;

	}
	printf("%lld\n", total);
	return 0;
}

Runtime: 0.02s