sandeep.bvb
BAN USER- 1of 1 vote
AnswersWrite a program to reverse contents of a file in place..for example if file has "abcde" after executing program the contents should be "edcba". The program should be efficient.. You can use fwrite & fread apis...
- sandeep.bvb in United States
I wrote using fread and fwrite, by reading 1 char at a time and replacing with one at the other end.. He seemed not happy as the I/O was high...| Report Duplicate | Flag | PURGE
Amazon Software Engineer / Developer C#
C++ Implementation:
#include <iostream>
using namespace std;
int main() {
int m,n;
int a[100] [100];
bool found = false;
cout << " m & n :" ;
cin>>m >>n;
cout<<" Enter matrix elements : ";
for (int i = 0; i < m; i++) {
for (int j = 0 ; j < n ; j++) {
cin>>a[i][j];
}
}
int xiter [] = { 1, 0 , -1 ,0 };
int yiter [] = { 0, 1 , 0 ,-1 };
int island_count = 0;
// Count number of islands -
for (int i =0 ; i < m; i++)
for (int j = 0; j < n; j++) {
if (a[i][j] == 1) {
bool island = true;
a[i][j] = 2;
for (int k = 0; k < 4; k++) {
int x = i+ xiter[k];
int y = j + yiter[k];
if (x < 0 || y < 0 || x >=m || y>= n)
continue;
if (a[x][y] == 1) {
a[x][y] = 2;
} else if (a[x][y] == 2) {
island = false;
}
}
if (island)
island_count++;
}
}
cout << " Number of islands : " <<island_count <<endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <unordered_map>
using namespace std;
int main()
{
char str[100];
char pattern[100];
unordered_map<char, int> positions;
int farthest_pos;
cout<<"Enter main string: ";
cin >> str;
cout<<"string is : "<<str<<endl;
cout<<"pattern : ";
cin>>pattern;
cout<<"pattern is : "<<pattern<<endl;
for (int i=0; i < strlen(str); i++) {
positions[str[i]] = i;
}
farthest_pos = positions[pattern[0]];
for (int j=0; j < strlen(pattern)-1; j++)
{
if (positions[pattern[j]] > positions[pattern[j+1]]) {
cout<<"Problemm is: "<< pattern[j] << " pos "<< positions[pattern[j]] << " AND : " << pattern[j+1] << " pos "<< positions[pattern[j+1]]<<endl;
cout <<"false"<<endl;
return 0;
}
}
cout <<"true"<<endl;
}
I doubt if this kind of questions are asked in Facebook. There is not much algo required in this. may be fake.
- sandeep.bvb March 04, 2017