Today I got a irritating question in a coding contest. I creamed through the first round but in the second round the following question got me in a trap.
Question: Input N = 4
Output:
1
0 1
1 0 1
0 1 0 1
I tried many things but every time I failed.
Apart from this stupid wrong solution I tried many fancy stuff and failed in the end.
What part of my C knowledge is weak?
If you were given this question how would you solve it?
Seems pretty trivial to me:
int main(int argc, char *argv[])
{
int n = strtol(argv[1], NULL, 10);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("%d ", i % 2 ? j % 2 : 1 - j % 2);
}
printf("\n");
}
return 0;
}
You panicked. Since the values you print depend on the row and column, use both. And name your variables better.
void printBinaryTriangle(const unsigned int rows) {
for(int row = 1; row <= rows; ++row) {
for (int column = 0; column < row; ++column) {
printf("%d ", (row + column) % 2);
}
putchar('\n');
}
}
This did the job for me.
int i,j,flag;
int num=4;
flag=1;
for(i=0;i<num;i++)
{
for(j=0;j<i+1;j++)
{
printf("%d",(j+flag)%2);
}
if(flag)
{
flag=0;
}
else
{
flag=1;
}
printf("\n");
}
Is this what you want?
for(int k = 0; k < N; k++){
for(int i = 0; i < k+1; i++){
if(i % 2 == k % 2)
printf("1 ");
else
printf("0 ");
};
printf("\n");
}
num = 12
new_string = ''
new_list = ''
while num:
for i in range(1,num+1):
strings_of_ones = new_string+ '1' * i
new_list = list(strings_of_ones)
for j in range(1,len(new_list),2):
new_list[j]='0'
print(' '.join(new_list[::-1]))
num -= 1
Answer in Java:-
public class practice {
public static void main(String args[]) {
int n=4;
for (int i=1 ;i<=n ; i++) {
for (int j=1 ;j<=i ;j++) {
int a=i+j;
if (a % 2==0) {
System.out.print("1"+" ");
} else {
System.out.print("0"+" ");
}
}
System.out.println();
}
}
}
Related
I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}
I want to take integers like that
1 2 3
4 5 6
Than I should sum them with same line like
1+4=5
2+5=7
3+6=9
And I should print 5 7 9.
To do this I tried to take values from user in this way but only first scanf is running and the other is not.What is the reason for that and how can I fix it or is there any other useful way to do that?(How many value will user enter is unknown.)
char s1[21];
scanf("%[^\n]", s1);
char s2[21];
scanf("%[^\n]", s2);
for(int i =0; i<b1; i++)
printf("%d ",s1[i]+s2[i]);
You should read into integer arrays, not strings. scanf() will skip over the whitespace between the numbers.
int numbers1[b1], numbers2[b1];
for (int i = 0; i < b1; i++) {
scanf("%d", &numbers1[i]);
}
for (int i = 0; i < b1; i++) {
scanf("%d", &numbers2[i]);
}
for (int i = 0; i < b1; i++) {
printf("%d ", numbers1[i] + number2[i]);
}
printf("\n");
The following program doesn't work. I should debug it, but I am too lazy for that. However take a look. It might help giving you some idea.
#include <stdio.h>
#define SIZE 50
int main() {
int n[SIZE][SIZE], row, col, sum, i, j;
int numCol; // column of first row
int yeah = 1; // tells wether all the rows have the same number of columns
char c = 'c'; // to check for the newline
for(row = 0; row < SIZE; row++) {
for(col = 0; c != '\n' && col < SIZE; col++) {
if( scanf("%d%c", &n[row][col], &c) < 2 && row > 0 ) {
if( col != numCol - 1) {
yeah = 0;
}
goto outside_pls; // the only way to exit the outer loop;
}
} // end inner for
if( row == 0 ) {
numCol = col;
}
else if( col != numCol ) {
yeah = 0;
}
} // end outer for
outside_pls:
if( yeah ) {
printf("%d %d\n", row, col);
for(j = 0; j < col; j++) {
sum = 0;
for(i = 0; i < row; i++) {
sum += n[i][j];
}
printf("%d ", sum);
}
}
else {
puts("Number of columns doesn't match");
}
return 0;
}
You should take the index you are in and get all of your variables in the same index in all of your arrays.
Code should look like this
for(int i = 0; i< array1.length(); i++){
sum = array1[i]+array2[i]+.......;
}
Can someone help me to figure out why my code is unable to accurately find the duplicate of elements?
#include <stdio.h>
int main() {
int array[10];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
if (array[i] == array[j]) {
count++;
break;
}
}
}
printf("The duplicates are : %d ", count);
}
I'm a beginner at this language so any advice and suggestions to help me solve this exercise will be much appreciated.
First of all the first loop runs 10 times even if the user enters less numbers. You can fix that by doing:
for (int i = 0; scanf_s("%d", &array[i]) == 1 && i < 10; i++);
Then the logic of the other two loops is wrong. I initially got wrong what you meant. I thought you wanted to know how many times a number is duplicated. So I wrote the wrong program and then modified it for your purposes. Here is your program:
#include <stdio.h>
int main() {
int n[10];
int dupes[5], d = 0;
int flag = 1, omg;
for ( omg = 0; scanf("%d", &n[omg]) == 1 && omg < 10; omg++);
for (int i = 0; i < omg; i++) {
for (int j = i+1; j < 10; j++) {
if( n[i] == n[j] ) {
if( d > 0 ) {
for(int k = 0; k < d; k++) {
if( n[i] == dupes[k] ) {
flag = 0;
break;
}
}
}
if( flag ) {
dupes[d] = n[i];
++d;
break;
}
else {
flag = 1;
break;
}
} // end outer if
}
}
printf("There are %d numbers that have at least one dupe\n", d);
return 0;
}
I named a variable omg out of desperation, writing this program was a nightmare. (Because it came from the ashes of a previous program)
Your code correctly determines the number of duplicate entries in the array.
If instead you want to determine the number of duplicated values, you must modify the algorithm:
#include <stdio.h>
int main() {
int array[10] = { 0 };
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < 10; i++) {
scanf_s("%d", &array[i]);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (array[i] == array[j]) {
if (i < j)
count++;
if (i != j)
break;
}
}
}
printf("There are %d duplicate values\n", count);
return 0;
}
I use a structure 'Number' which contains the number and its duplicate, then I fill the array and I put it in ascending order then I calculate the number of duplicate of each number and I fill in the strecture like this :
my code:
#include <stdio.h>
#define size 10
typedef struct Number
{
int number;
int duplicate;
}Number;
int main()
{
int array[size];
Number array2[size];
int count = 0;
printf("Enter a maximum of 10 values to store in an array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
int temp=size;
int temppppp=0;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(array[i]>array[j])
{
temppppp=array[i];
array[i]=array[j];
array[j]=temppppp;
}
}
}
printf("\n\n");
for (int i = 0; i < size; i++)
{
printf("[%d]",array[i]);
}
printf("\n\n");
int i=0;
int j=0;
while(i<size)
{count=1;
while(i<(size-1)&&array[i]==array[i+1])
{
count++;
i++;
}
if(count>=2)
{
array2[j].number=array[i-1];
array2[j].duplicate=count;
j++;
}
i++;
}
int p=0;
while(p<j)
{
printf("\n[%d] has duplicated %d times !\n",array2[p].number,array2[p].duplicate);
p=p+1;
}
printf("\n\n");
printf("\nThere are %d duplicate values\n", j);
}
I'm confused to print this type of pattern given below:
* *
* *
*
* *
* *
I've tried this:
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of base\n>>> ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j + n; j++)
{
if (// ??? )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
but I don't know what is the condition of if statement
Help Me to solve this please
The pattern consists of exactly n * 2 - 1 rows and columns. So you can run an outer loop to iterate through rows with structure for(i=1; i<= count; i++); where count = n * 2 - 1. Each row contains exactly n * 2 - 1 columns. So, run inner loop as for(j=1; j<=count; j++). And inside this loop we need stars to be printed when row and column number both are equal (i.e. print star whenever if(i == j)) and if(j == count - i + 1). For example:
#include <stdio.h>
int main() {
int i, j, n;
int count;
printf("Enter n: ");
scanf("%d", &n);
count = n * 2 - 1;
for (i = 1; i <= count; i++) {
for (j = 1; j <= count; j++) {
if (j == i || (j == count - i + 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
To not change any code and answer your question "what goes in the if"
n - 1 - i == j || j == i
this does
This will work fine !! :)
#include<iostream>
using namespace std;
int main()
{
int n,m=0;
int m2=0;
cin>>n;
int f=-1,l=n;
for(int i=0;i<n;i++)
{
f=f+1;
l=l-1;
for(int j=0;j<n;j++)
{
if(j==f || j==l) cout<<"*";
else cout<<" ";
}
cout<<endl;
}
}
I'm trying to solve a problem, The objective is to print the last step can be achieved, the inputs are N as testcases, and A as the integers of the testcases. For example :
N = 10
A = 1 2 1 1 2 3 4 1 2 3 <- 10 integers
So, this is what I have done.
#include <stdio.h>
int main()
{
int N,i,j;
scanf("%d",&N);
int A[N+1];
for(int i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
A[N] = 1;
for(int i=0;i<N;i++)
{
if(A[i] > A[i+1])
{
printf("%d-",A[i]);
}
else if(A[i] == A[i+1])
{
printf("%d-",A[i]);
}
}
printf("\n");
return 0;
}
The expected output is :
2-1-4-3
but the output I get is :
2-1-4-3-
How do I exclude the minus symbol after the last output? Thanks in advance.
Personally, my preference is to print the separator character before. The simple reason is that generally this happens in loops that begin at zero, so I can do a zero-test as follows:
for(int i = 0; i < N; i++)
{
if (i > 0)
putc('-', stdout);
printf("%d", A[i]);
}
BUT, in your case, your loop won't always print a value. So you actually need to be smarter. There are many ways to achieve this, but for simplicity, why not just use a flag:
int has_output = 0;
for(int i = 0; i < N; i++)
{
if(A[i] >= A[i+1])
{
if (has_output)
putc('-', stdout);
else
has_output = 1;
printf("%d", A[i]);
}
}
Notice that the preference is still to print the separator just in time. In other words, only when you determine that you need to print something.
Going a bit more crazy:
const char* fmt[2] = { "%d", "-%d" };
int has_output = 0;
for(int i = 0; i < N; i++)
{
if(A[i] >= A[i+1])
{
printf(fmt[has_output], A[i]);
has_output = 1;
}
}
#include<stdio.h>
int main()
{
int N,i,j;
scanf("%d",&N);
int A[N+1];
for(int i=0;i<N;i++)
{
scanf("%d",&A[i]);
}
A[N] = 1;
int flag = 0; //added a new variable here
for(int i=0;i<N;i++)
{
if(A[i] >= A[i+1])
{
if(flag == 0){ //check if its printing for the first time
printf("%d",A[i]); // then print without dash
flag = 1; //change the flag so that this block never executes again
}else{
printf("-%d",A[i] ); //else print a dash and then the number
}
}
}
printf("\n");
return 0;
}
As "-" is a separator only used after the first printed value, consider changing the separator.
const char *separator = "";
for(int i=0;i<N;i++) {
if(A[i] >= A[i+1]) {
printf("%s%d", separator, A[i]);
separator = "-";
}
}
printf("\n");