We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Changing Bits
Changing Bits
+ 0 comments Here is changing bits problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-changing-bits-problem-solution.html
+ 0 comments set_a ind x can i assume that x will be either''0' or '1'?
+ 0 comments Python 3
def changeBits(a, b, queries): a, b = int(a, 2), int(b, 2) for q in queries: q = q.rstrip().split() i = int(q[1]) if q[0] == 'set_a': if q[2] == '1': a |= 1<<i else: a &= ~(1<<i) elif q[0] == 'set_b': if q[2] == '1': b |= 1<<i else: b &= ~(1<<i) else: c = a + b if not c & (1<<i): print('0', end="") else: print('1', end="")
+ 0 comments This is Java solution:
import java.io.File; import java.io.IOException; import java.util.Scanner; /** * @author Oleg Cherednik * @since 17.06.2020 */ public class Solution { public static void main(String[] args) throws IOException { try (Scanner scan = new Scanner(new File("e:/input01.txt"))) { int ab_size = scan.nextInt(); int queries_size = scan.nextInt(); scan.nextLine(); int size = (ab_size + Result.BITS_PER_INDEX) / Result.BITS_PER_INDEX; long[] a = strToNum(new long[size], scan.nextLine()); long[] b = strToNum(new long[size], scan.nextLine()); Result.changeBits(a, b, queries_size, scan); } } private static long[] strToNum(long[] arr, String str) { int i = 0; int j = str.length(); for (; j >= Result.BITS_PER_INDEX; j -= Result.BITS_PER_INDEX) arr[i++] = Long.parseLong(str.substring(j - Result.BITS_PER_INDEX, j), 2); if (j > 0) arr[i] = Long.parseLong(str.substring(0, j), 2); return arr; } } class Result { /* * Complete the 'changeBits' function below. * * The function accepts following parameters: * 1. STRING a * 2. STRING b * 3. STRING_ARRAY queries */ public static final byte BITS_PER_INDEX = 63; public static void changeBits(long[] a, long[] b, int queries_size, Scanner scan) throws IOException { long[] sum = new long[a.length]; boolean dirty = true; StringBuffer buf = new StringBuffer(queries_size); while (queries_size > 0) { String command = scan.next(); int idx = scan.nextInt(); if ("get_c".equals(command)) { if (dirty) { add(a, b, sum); dirty = false; } buf.append(get(sum, idx)); } else { byte x = scan.nextByte(); set("set_a".equals(command) ? a : b, idx, x); dirty = true; } queries_size--; } System.out.print(buf); } private static void add(long[] one, long[] two, long[] sum) { byte remainder = 0; for (int i = 0; i < sum.length; i++) { sum[i] = one[i] + two[i] + remainder; remainder = (byte)((sum[i] >> BITS_PER_INDEX) & 1L); } } private static byte get(long[] ar, int bit) { int i = bit / BITS_PER_INDEX; bit %= BITS_PER_INDEX; return (byte)((ar[i] >> bit) & 1L); } private static void set(long[] arr, int bit, byte val) { int i = bit / BITS_PER_INDEX; bit %= BITS_PER_INDEX; arr[i] = (val == 0) ? arr[i] & ~(1L << bit) : arr[i] | (1L << bit); } }
+ 0 comments anybody please help something. wrong in this code
include
include
include
include
using namespace std;
int main() { long int m,n,i,l=0,l2=0,j; int temp,c=0; string st; cin>>m>>n;
char u[n][m+10],a[m],b[m],t; cin>>a>>b; for(i=0;i<n;i++) { cin>>u[i]; } stringstream s; for(i=0;i<n;i++) { long int si=strlen(u[i]); for(j=0;j<5;j++) { st+=t; } if(st=="set_a") { for(j=6;u[i][j]<' ';j++) { t=u[i][j]; s<<t; s>>temp; l=l*10+temp; } while(j<si) { t=u[i][j]; s<<t; s>>temp; l2=l2*10+temp; } a[si-l-1]=l2; } if(st=="set_b") { for(j=6;u[i][j]<' ';j++) { t=u[i][j]; s<<t; s>>temp; l=l*10+temp; } while(j<si) { t=u[i][j]; s<<t; s>>temp; l2=l2*10+temp; } b[si-l-1]=l2; } if(st=="get_c") { vector<int> y; for(j=n;j>=0;j--) { if(a[j]=='1'&&b[j]=='1'&&c==1) { y.push_back(1); } if(a[j]=='1'&&b[j]=='1'&&c==0) { y.push_back(0); c=1; } if((a[j]=='1' && b[j]=='0' && c==1)||(a[j]=='0' && b[j]=='1' && c==1)) { y.push_back(0); } if((a[j]=='1' && b[j]=='0' && c==0 )||(a[j]=='0' && b[j]=='1' && c==0)||(a[j]=='0' && b[j]=='0' && c==1)) { y.push_back(1); c=0; } if((a[j]=='0' && b[j]=='0' && c==0 )) { y.push_back(0); } } for(j=6;u[i][j]<' ';j++) { t=u[i][j]; s<<t; s>>temp; l=l*10+temp; } cout<<y[si-l-1]; }
}
}
Load more conversations
Sort 33 Discussions, By:
Please Login in order to post a comment