Minimise the Size CodeChef Solution

Problem -Minimise the Size CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Minimise the Size CodeChef Solution in C++17

 #include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main() 
{
    int t,c,p;
    cin >> t;
    while(t--)
    {
        vector <int> ans;
        cin >> c;
        if (c == 0){
            cout << 2 << endl;
            cout << 1 << " " << 1 << endl;
            continue;
        }
        while(1)
        {
            p = log2(c) + 1;
            p = pow(2,p);
            p--;
            ans.push_back(p);
            if (p == c)
            {
                break;
            }
            c = p - c;
        }
        cout << ans.size() << endl;
        for(auto a : ans) cout << a << " ";
        cout << endl;
    }
    return 0;
}
// check the constratins for edge cases. 

Minimise the Size CodeChef Solution in C++14

#include <bits/stdc++.h>
using namespace std;
int main() 
{
int t;
    cin>>t;
    while(t--)
    {
        long long int C;
        cin>>C;
        vector<long long int> result;
        long long int curr = 0;
        for(int i=60; i>=0;i--){
            if( (curr & (1LL<<i)) != (C & (1LL<<i)) )
            {
                long long int b = (1LL<<(i+1)) -1LL;
                result.push_back(b);
                curr =curr^b;
            }
        }
        if(result.size()==0){
            result.push_back(1);
            result.push_back(1);
        }
        std::cout << result.size() << std::endl;
        for(auto x : result)
            cout<< x <<" ";
        cout<<endl;
        
    }
    
	return 0;
}

Minimise the Size CodeChef Solution in PYTH 3

t = int(input())

def solve():
    C = int(input())
    if C == 0:
        print(2)
        print(1, 1)
        return
    xor = 0
    ans = []
    for i in range(60,-1,-1):
        if C & (1<<i):
            if xor & (1<<i):
                continue
            else:
                ans.append((1<<(i+1)) - 1)
                xor ^= ((1<<(i+1))-1)
        else:
            if not xor & (1<<i):
                continue
            else:
                ans.append((1<<(i+1))-1)
                xor ^= ((1<<(i+1))-1)
    print(len(ans))
    if ans:
        print(*ans)
    else:
        print(0)

while t:
    solve()
    t -= 1

Minimise the Size CodeChef Solution in C

#include <stdio.h>
#include <math.h>
#include <string.h>

void solve();
void getBinary(char[], long int);
void increaseByOne(int[], int, int);

void solve()
{
    long int c; //c=17
    scanf("%ld", &c);

    if (c == 0)
        printf("2\n1 1");
    else
    {
        int len = log2(c) + 1; //len = 5

        char binary[len]; //binary = 10001
        getBinary(binary, c);

        int ones[len], ans[len], k = 0;

        for (int i = 0; i < len; i++)
            ones[i] = 0;

        for (int i = 0; i < len; i++)
        {
            if (binary[i] == '1')
            {
                if (ones[i] % 2 != 1)
                {
                    increaseByOne(ones, i, len);
                    ans[k] = len - i;
                    k++;
                }
            }
            else
            {
                if (ones[i] % 2 == 1)
                {
                    increaseByOne(ones, i, len);
                    ans[k] = len - i;
                    k++;
                }
            }
        }

        printf("%d\n", k);
        for (int i = k - 1; i >= 0; i--)
        {
            long int final = pow(2, ans[i]);
            final--;
            printf("%ld ", final);
        }
    }
    printf("\n");
}

void getBinary(char arr[], long int num)
{
    if (num == 0)
    {
        arr[0] = '0';
        return;
    }

    int last = log2(num);

    while (num)
    {
        int rem = num % 2;
        arr[last--] = rem ? '1' : '0';
        num /= 2;
    }
}

void increaseByOne(int arr[], int start, int end)
{
    for (int i = start; i < end; i++)
    {
        arr[i]++;
    }
}

int main()
{
    int t;
    scanf("%d", &t);

    while (t--)
    {
        solve();
    }
}

Minimise the Size CodeChef Solution in JAVA

 
import java.io.*;
import java.util.*;
 

class FastReader{
	
   BufferedReader br;
   StringTokenizer st;

   public FastReader()
   {
       br = new BufferedReader(
           new InputStreamReader(System.in));
   }
   /* find distance of one postion wrt to another in circular arrray..
     for(int i = 0; i< 2*n; i++)
		time[(i+1)%n] = Math.min(time[(i+1)%n], time[i%n]+1); //checking with bacward position
	for(int i = 2*n-1; i>= 0; i--)
          time[(i+n-1)%n] = Math.min(time[(i+n-1)%n], time[i%n]+1);				  
    */
   String next()
   {
       while (st == null || !st.hasMoreElements()) {
           try {
               st = new StringTokenizer(br.readLine());
           }
           catch (IOException e) {
               e.printStackTrace();
           }
       }
       return st.nextToken();
   }

   int nextInt() { return Integer.parseInt(next()); }

   long nextLong() { return Long.parseLong(next()); }

   double nextDouble()
   {
       return Double.parseDouble(next());
   }

   String nextLine()
   {
       String str = "";
       try {
           str = br.readLine();
       }
       catch (IOException e) {
           e.printStackTrace();
       }
       return str;
   }

}
class Codechef {
	static class pair implements Comparable<pair> {
		int a;
		int b;
		pair(int x,int y){
			a= x;
			b= y;
		}
		
		public int compareTo(pair p) {
			return b-p.b;
		}
	}
	   static boolean factors(int num){
		    
		   for(int i=2;i*i<=num;i++) {
			     if(num%2==0) {
			    	 return false;
			     }
		   }
		   return true;
	   }
	   static long coPrimes(long n) {
		   long res = n;
		   for(int i=2;i*i<=n;i++) {
			   if(n%i==0) {
				   res = res/i;
				   res = res*(i-1);
				   
				   while(n%i==0) {
					   n = n/i;
				   }
			   }
		   }
		   if(n!=1) {
			   res = res/n;
			   res = res*(n-1);
		   }
		   return res;
	   } 
	   static long mod = 1000000007;
	    
	   
	   
	   static void solve(FastReader sc, PrintWriter out) {
           long c = sc.nextLong();
           ArrayList<Long> al = new ArrayList<>();
           long curr_xor= 0;
           
           for(int i=60;i>=0;i--) {
        	   long cBit = ((1l<<i) & c);
        	   long curr_xorBit = ((1l<<i)& curr_xor);
        	   
        	   if(cBit!=curr_xorBit) {
        		   al.add( (1l<<(i+1))-1);
        		   curr_xor = curr_xor^( (1l<<(i+1))-1);
        	   }
        	   
           }
           if(al.size()==0) {
        	   al.add(1l);
        	   al.add(1l);
           }
           out.println(al.size());
           for(int i=0;i<al.size();i++) {
        	   out.print( al.get(i)+" ");
           }
           out.println();
	   }
	   static long rightMostSetBit(long n){
			//will give only rmsb and all other will be zero.
			return n&(-n);
	}
	   static int largestPowerOfTwoInRange(long n) {
			int x = 0;
			while((1<<x)<=n) {
				x++;
			}
			return x-1;
		//log2 can also give right most set bit.	
		}
	   public static void main(String[] args)  throws IOException{
			 FastReader sc = new FastReader();
			 PrintWriter out = new PrintWriter(System.out);
			 int t = sc.nextInt();
			 while(t-->0){
				 solve(sc,out);
			 }
			 out.close();
		 }	
	     
	       

}
//Math.ceil(Math.log(x)/Math.log(2)) gives X which satisfy 2^X.

Minimise the Size CodeChef Solution in PYPY 3

t = int(input())

def solve():
    C = int(input())
    if C == 0:
        print(2)
        print(1, 1)
        return
    xor = 0
    ans = []
    for i in range(60,-1,-1):
        if C & (1<<i):
            if xor & (1<<i):
                continue
            else:
                ans.append((1<<(i+1)) - 1)
                xor ^= ((1<<(i+1))-1)
        else:
            if not xor & (1<<i):
                continue
            else:
                ans.append((1<<(i+1))-1)
                xor ^= ((1<<(i+1))-1)
    print(len(ans))
    if ans:
        print(*ans)
    else:
        print(0)

while t:
    solve()
    t -= 1

Minimise the Size CodeChef Solution in PYTH

L = [1]
v = 1
for k in range(59):
	v = v*2 +1
	L.append(v)
# endfor k
L.reverse()
t = int(raw_input())
for i in range(t):
	C = int(raw_input())
	if C == 0:
		print '2'
		print '1 1'
	else:
		B = []
		R = []
		for k in range(60):
			B.append(C%2)
			C = C/2
		# endfor k
		B.reverse()
		n = 0
		for k in range(60):
			if (B[k]+n)%2 == 1:
				R.append(L[k])
				n += 1
			# endif
		# endfor k
		print len(R)
		st = ''
		for x in R:
			st += str(x) + ' '
		# endfor x
		print st
	# endif
# endfor i

Minimise the Size CodeChef Solution in GO

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	tc := readNum(reader)

	var buf bytes.Buffer

	for tc > 0 {
		tc--
		var C uint64
		s, _ := reader.ReadBytes('\n')
		readUint64(s, 0, &C)
		res := solve(C)
		buf.WriteString(fmt.Sprintf("%d\n", len(res)))
		for i := 0; i < len(res); i++ {
			buf.WriteString(fmt.Sprintf("%d ", res[i]))
		}
		buf.WriteByte('\n')
	}

	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(C uint64) []uint64 {
	if C == 0 {
		return []uint64{1, 1}
	}
	var res []uint64

	for i := uint64(60); ; i-- {
		if (C>>i)&1 == 1 {
			if len(res)%2 == 0 {
				res = append(res, (1<<(i+1) - 1))
			}
		} else {
			if len(res)%2 == 1 {
				res = append(res, (1<<(i+1) - 1))
			}
		}

		if i == 0 {
			break
		}
	}

	return res
}
Minimise the Size CodeChef Solution Review:

In our experience, we suggest you solve this Minimise the Size CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimise the Size CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Minimise the Size CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *