Can anybody please say why my code is wrong… for the question in link
A link that people can click on:
https://www.codechef.com/viewsolution/44031864
Problem link:
Edit:
The compiler warnings contain a hint:
[simon@simon-laptop][19:38:12]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling shoyam-ZCO14001.cpp
+ g++ -std=c++14 shoyam-ZCO14001.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
shoyam-ZCO14001.cpp: In function ‘int main()’:
shoyam-ZCO14001.cpp:34:10: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(c==1&y!=0){
~^~~
shoyam-ZCO14001.cpp:40:10: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(c==2 & y!=n-1){
~^~~
shoyam-ZCO14001.cpp:46:10: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(c==3&m!=0&r!=1){
~^~~
shoyam-ZCO14001.cpp:46:20: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(c==3&m!=0&r!=1){
~^~~
shoyam-ZCO14001.cpp:52:10: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(c==4 & m<h&r!=0){
~^~~
shoyam-ZCO14001.cpp:52:21: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
if(c==4 & m<h&r!=0){
~^~~
+ set +x
Successful
Edit2:
Although there are deeper issues than that: consider the test input:
11 12
12 7 11 5 11 10 12 7 9 7 3
3 3 1 1 4 2 2 1 3 2 1 2 0
2 Likes
thnx bro , i am new to coding just started learning in march… , your help is invaluable to me
1 Like
CodeChef: Practical coding for everyone can you help me with this even … why is it still showing error
Edit: Whoops - you’ve switched to a different problem XD
1 Like
Just copied your code and pasted it here.
#include<iostream>
using namespace std;
int n;
int y=0;
int main() {
int t;
cin>>t;
int c[1000000];
while(t--){
cin>>n;
int x=0;
int i=0;
while(n){
c[i] = n%10;
n/=10;
i++;
x++;
}
int a[10];
int y=0;
for(int i=0;i<x;i++){
if(c[i]!=c[i-1]){ // What is this doing? Is it even required?
a[y]=c[i];
y++;
}
}
int number = 0;
for (int i = 0; i < y; i++) { // And what is this loop for?
number *= 10;
number += a[i];
}
cout << number;
cout<<endl;
}
return 0;
}
A simple iterative solution for small numbers would be:
#include <bits/stdc++.h>
using namespace std;
int reverse(int n) {
int ans = 0;
while(n > 0) {
ans *= 10;
ans += n % 10;
n /= 10;
}
return ans;
}
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
cout << reverse(n) << endl;
}
return 0;
}
thnx